From 29affaa4381d175db541510249a52cb7a5683ca7 Mon Sep 17 00:00:00 2001 From: czp Date: Thu, 15 Mar 2018 23:50:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=20BilibiliWebAPI=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/hiczp/bilibili/api/BilibiliAPI.java | 21 +++++++++++++++---- .../AutoRefreshTokenInterceptor.java | 6 ++---- .../hiczp/bilibili/api/live/LiveService.java | 11 +++++++--- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/hiczp/bilibili/api/BilibiliAPI.java b/src/main/java/com/hiczp/bilibili/api/BilibiliAPI.java index 7b59074..0289deb 100644 --- a/src/main/java/com/hiczp/bilibili/api/BilibiliAPI.java +++ b/src/main/java/com/hiczp/bilibili/api/BilibiliAPI.java @@ -51,6 +51,8 @@ public class BilibiliAPI implements BilibiliServiceProvider, BilibiliCaptchaProv private CaptchaService captchaService; private LiveService liveService; + private BilibiliWebAPI bilibiliWebAPI; + public BilibiliAPI() { this.bilibiliClientProperties = BilibiliClientProperties.defaultSetting(); this.bilibiliAccount = BilibiliAccount.emptyInstance(); @@ -71,7 +73,6 @@ public class BilibiliAPI implements BilibiliServiceProvider, BilibiliCaptchaProv this.bilibiliAccount = new BilibiliAccount(bilibiliAccount); } - //TODO 不明确客户端访问 passport.bilibili.com 时使用的 UA @Override public PassportService getPassportService() { if (passportService == null) { @@ -83,7 +84,15 @@ public class BilibiliAPI implements BilibiliServiceProvider, BilibiliCaptchaProv public PassportService getPassportService(@Nonnull List interceptors, @Nonnull HttpLoggingInterceptor.Level logLevel) { OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder(); + //TODO 不明确客户端访问 passport.bilibili.com 时使用的 UA okHttpClientBuilder + .addInterceptor(new AddFixedHeadersInterceptor( + "Buvid", bilibiliClientProperties.getBuvId(), + "User-Agent", "bili-universal/6560 CFNetwork/894 Darwin/17.4.0" //这是 IOS 的 UA + )) + .addInterceptor(new AddDynamicHeadersInterceptor( + () -> "Display-ID", () -> String.format("%s-%d", bilibiliAccount.getUserId() == null ? bilibiliClientProperties.getBuvId() : bilibiliAccount.getUserId(), apiInitTime) + )) .addInterceptor(new AddFixedParamsInterceptor( "build", bilibiliClientProperties.getBuild(), "mobi_app", "android", @@ -148,7 +157,6 @@ public class BilibiliAPI implements BilibiliServiceProvider, BilibiliCaptchaProv .addInterceptor(new AddAppKeyInterceptor(bilibiliClientProperties)) .addInterceptor(new AutoRefreshTokenInterceptor( this, - autoRefreshToken, ServerErrorCode.Common.UNAUTHORIZED, ServerErrorCode.Live.USER_NO_LOGIN, ServerErrorCode.Live.PLEASE_LOGIN, @@ -258,11 +266,14 @@ public class BilibiliAPI implements BilibiliServiceProvider, BilibiliCaptchaProv @Override public BilibiliWebAPI getBilibiliWebAPI() throws IOException { - return new BilibiliWebAPI(toCookies()); + return getBilibiliWebAPI(BrowserProperties.defaultSetting()); } public BilibiliWebAPI getBilibiliWebAPI(BrowserProperties browserProperties) throws IOException { - return new BilibiliWebAPI(browserProperties, toCookies()); + if (bilibiliWebAPI == null) { + bilibiliWebAPI = new BilibiliWebAPI(browserProperties, toCookies()); + } + return bilibiliWebAPI; } public LoginResponseEntity login(@Nonnull String username, @Nonnull String password) throws IOException, LoginException, CaptchaMismatchException { @@ -301,6 +312,7 @@ public class BilibiliAPI implements BilibiliServiceProvider, BilibiliCaptchaProv } } bilibiliAccount.copyFrom(loginResponseEntity.toBilibiliAccount()); + bilibiliWebAPI = null; LOGGER.info("Login succeed with username: {}", username); return loginResponseEntity; } @@ -334,6 +346,7 @@ public class BilibiliAPI implements BilibiliServiceProvider, BilibiliCaptchaProv } } bilibiliAccount.copyFrom(refreshTokenResponseEntity.toBilibiliAccount()); + bilibiliWebAPI = null; LOGGER.info("RefreshToken succeed with userId: {}", bilibiliAccount.getUserId()); return refreshTokenResponseEntity; } diff --git a/src/main/java/com/hiczp/bilibili/api/interceptor/AutoRefreshTokenInterceptor.java b/src/main/java/com/hiczp/bilibili/api/interceptor/AutoRefreshTokenInterceptor.java index f08e0a5..ff6279d 100644 --- a/src/main/java/com/hiczp/bilibili/api/interceptor/AutoRefreshTokenInterceptor.java +++ b/src/main/java/com/hiczp/bilibili/api/interceptor/AutoRefreshTokenInterceptor.java @@ -21,12 +21,10 @@ public class AutoRefreshTokenInterceptor implements Interceptor { private static final Logger LOGGER = LoggerFactory.getLogger(AutoRefreshTokenInterceptor.class); private BilibiliAPI bilibiliAPI; - private Boolean autoRefreshToken; private int[] codes; - public AutoRefreshTokenInterceptor(BilibiliAPI bilibiliAPI, Boolean autoRefreshToken, int... codes) { + public AutoRefreshTokenInterceptor(BilibiliAPI bilibiliAPI, int... codes) { this.bilibiliAPI = bilibiliAPI; - this.autoRefreshToken = autoRefreshToken; this.codes = codes; } @@ -34,7 +32,7 @@ public class AutoRefreshTokenInterceptor implements Interceptor { public Response intercept(Chain chain) throws IOException { Response response = chain.proceed(chain.request()); - if (!autoRefreshToken) { + if (!bilibiliAPI.isAutoRefreshToken()) { return response; } diff --git a/src/main/java/com/hiczp/bilibili/api/live/LiveService.java b/src/main/java/com/hiczp/bilibili/api/live/LiveService.java index fe8cb14..1c9741d 100644 --- a/src/main/java/com/hiczp/bilibili/api/live/LiveService.java +++ b/src/main/java/com/hiczp/bilibili/api/live/LiveService.java @@ -52,8 +52,8 @@ public interface LiveService { @POST("feed/v1/feed/isFollowed") Call isFollowed(@Query("follow") long hostUserId); - //TODO 每日背包任务 - //该 API 意义不明(似乎跟什么 每日背包任务 有关) + //TODO sendDaily + //该 API 意义不明 @GET("AppBag/sendDaily") Call sendDaily(); @@ -110,10 +110,11 @@ public interface LiveService { @GET("appUser/getTitle") Call getTitle(); - //TODO 节奏风暴 + //TODO 查看房间里是否有节奏风暴 @GET("SpecialGift/room/{roomId}") Call getSpecialGift(@Path("roomId") long roomId); //TODO 参与节奏风暴抽奖 + //TODO 查看节奏风暴奖励 /** * 获取自己的用户信息(live 站的个人信息, 非总站) @@ -173,6 +174,10 @@ public interface LiveService { return receiveUserTaskAward("double_watch_task"); } + //TODO 查看一个房间是否有活动抽奖 + //TODO 参与活动抽奖 + //TODO 查看活动抽奖奖励 + /** * 发送一个 Restful 心跳包, 五分钟一次. 这被用于统计观看直播的时间, 可以提升观众等级 * 2018-03-06 开始, 只有老爷才能通过观看直播获得经验