为 BilibiliWebAPI 增加缓存

This commit is contained in:
czp 2018-03-15 23:50:12 +08:00
parent 8b4d016b00
commit 29affaa438
3 changed files with 27 additions and 11 deletions

View File

@ -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<Interceptor> 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;
}

View File

@ -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;
}

View File

@ -52,8 +52,8 @@ public interface LiveService {
@POST("feed/v1/feed/isFollowed")
Call<IsFollowedResponseEntity> isFollowed(@Query("follow") long hostUserId);
//TODO 每日背包任务
// API 意义不明(似乎跟什么 每日背包任务 有关)
//TODO sendDaily
// API 意义不明
@GET("AppBag/sendDaily")
Call<SendDailyResponseEntity> sendDaily();
@ -110,10 +110,11 @@ public interface LiveService {
@GET("appUser/getTitle")
Call<TitlesEntity> getTitle();
//TODO 节奏风暴
//TODO 查看房间里是否有节奏风暴
@GET("SpecialGift/room/{roomId}")
Call<SpecialGiftEntity> 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 开始, 只有老爷才能通过观看直播获得经验