mirror of
https://github.com/czp3009/bilibili-api.git
synced 2024-12-21 20:30:28 +08:00
将 CaptchaService 从 PassportService 中单独分出来
This commit is contained in:
parent
984cf8801d
commit
919167d273
@ -4,6 +4,7 @@ import com.hiczp.bilibili.api.cookie.SimpleCookieJar;
|
||||
import com.hiczp.bilibili.api.interceptor.*;
|
||||
import com.hiczp.bilibili.api.live.LiveService;
|
||||
import com.hiczp.bilibili.api.live.socket.LiveClient;
|
||||
import com.hiczp.bilibili.api.passport.CaptchaService;
|
||||
import com.hiczp.bilibili.api.passport.PassportService;
|
||||
import com.hiczp.bilibili.api.passport.SsoService;
|
||||
import com.hiczp.bilibili.api.passport.entity.InfoEntity;
|
||||
@ -11,6 +12,9 @@ import com.hiczp.bilibili.api.passport.entity.LoginResponseEntity;
|
||||
import com.hiczp.bilibili.api.passport.entity.LogoutResponseEntity;
|
||||
import com.hiczp.bilibili.api.passport.entity.RefreshTokenResponseEntity;
|
||||
import com.hiczp.bilibili.api.passport.exception.CaptchaMismatchException;
|
||||
import com.hiczp.bilibili.api.provider.BilibiliCaptchaProvider;
|
||||
import com.hiczp.bilibili.api.provider.BilibiliServiceProvider;
|
||||
import com.hiczp.bilibili.api.provider.BilibiliSsoProvider;
|
||||
import okhttp3.*;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
import org.slf4j.Logger;
|
||||
@ -29,7 +33,7 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class BilibiliAPI implements BilibiliServiceProvider, BilibiliSsoProvider, LiveClientProvider {
|
||||
public class BilibiliAPI implements BilibiliServiceProvider, BilibiliCaptchaProvider, BilibiliSsoProvider, LiveClientProvider {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(BilibiliAPI.class);
|
||||
|
||||
private final Long apiInitTime = Instant.now().getEpochSecond(); //记录当前类被实例化的时间
|
||||
@ -41,6 +45,7 @@ public class BilibiliAPI implements BilibiliServiceProvider, BilibiliSsoProvider
|
||||
private String invalidRefreshToken;
|
||||
|
||||
private PassportService passportService;
|
||||
private CaptchaService captchaService;
|
||||
private LiveService liveService;
|
||||
|
||||
public BilibiliAPI() {
|
||||
@ -162,6 +167,26 @@ public class BilibiliAPI implements BilibiliServiceProvider, BilibiliSsoProvider
|
||||
.create(LiveService.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CaptchaService getCaptchaService() {
|
||||
if (captchaService == null) {
|
||||
captchaService = getCaptchaService(Collections.emptyList(), HttpLoggingInterceptor.Level.BASIC);
|
||||
}
|
||||
return captchaService;
|
||||
}
|
||||
|
||||
public CaptchaService getCaptchaService(@Nonnull List<Interceptor> interceptors, @Nonnull HttpLoggingInterceptor.Level logLevel) {
|
||||
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
|
||||
interceptors.forEach(okHttpClientBuilder::addInterceptor);
|
||||
okHttpClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(logLevel));
|
||||
|
||||
return new Retrofit.Builder()
|
||||
.baseUrl(BaseUrlDefinition.PASSPORT)
|
||||
.client(okHttpClientBuilder.build())
|
||||
.build()
|
||||
.create(CaptchaService.class);
|
||||
}
|
||||
|
||||
public SsoService getSsoService() {
|
||||
return getSsoService(new SimpleCookieJar());
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import com.hiczp.bilibili.api.passport.entity.KeyEntity;
|
||||
import com.hiczp.bilibili.api.passport.entity.LoginResponseEntity;
|
||||
import com.hiczp.bilibili.api.passport.entity.LogoutResponseEntity;
|
||||
import com.hiczp.bilibili.api.passport.entity.RefreshTokenResponseEntity;
|
||||
import com.hiczp.bilibili.api.provider.BilibiliServiceProvider;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.hiczp.bilibili.api.live.bulletScreen;
|
||||
|
||||
import com.hiczp.bilibili.api.BilibiliServiceProvider;
|
||||
import com.hiczp.bilibili.api.live.entity.BulletScreenEntity;
|
||||
import com.hiczp.bilibili.api.provider.BilibiliServiceProvider;
|
||||
|
||||
public class BulletScreenSendingTask {
|
||||
private BilibiliServiceProvider bilibiliServiceProvider;
|
||||
|
@ -2,7 +2,6 @@ package com.hiczp.bilibili.api.live.socket;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import com.hiczp.bilibili.api.BilibiliServiceProvider;
|
||||
import com.hiczp.bilibili.api.live.bulletScreen.BulletScreenConstDefinition;
|
||||
import com.hiczp.bilibili.api.live.entity.BulletScreenEntity;
|
||||
import com.hiczp.bilibili.api.live.entity.LiveRoomInfoEntity;
|
||||
@ -11,6 +10,7 @@ import com.hiczp.bilibili.api.live.socket.codec.PackageDecoder;
|
||||
import com.hiczp.bilibili.api.live.socket.codec.PackageEncoder;
|
||||
import com.hiczp.bilibili.api.live.socket.event.ConnectionCloseEvent;
|
||||
import com.hiczp.bilibili.api.live.socket.handler.LiveClientHandler;
|
||||
import com.hiczp.bilibili.api.provider.BilibiliServiceProvider;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
|
@ -0,0 +1,21 @@
|
||||
package com.hiczp.bilibili.api.passport;
|
||||
|
||||
import okhttp3.ResponseBody;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.Header;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public interface CaptchaService {
|
||||
@GET("captcha")
|
||||
Call<ResponseBody> getCaptcha(@Header("Cookie") String cookies);
|
||||
|
||||
default InputStream getCaptchaAsStream(String cookies) throws IOException {
|
||||
return getCaptcha(cookies)
|
||||
.execute()
|
||||
.body()
|
||||
.byteStream();
|
||||
}
|
||||
}
|
@ -1,36 +1,13 @@
|
||||
package com.hiczp.bilibili.api.passport;
|
||||
|
||||
import com.hiczp.bilibili.api.BaseUrlDefinition;
|
||||
import com.hiczp.bilibili.api.passport.entity.*;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.Header;
|
||||
import retrofit2.http.POST;
|
||||
import retrofit2.http.Query;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public interface PassportService {
|
||||
//获取验证码
|
||||
default okhttp3.Call getCaptcha(@Nonnull String cookies) {
|
||||
return getCaptcha(cookies, HttpLoggingInterceptor.Level.BASIC);
|
||||
}
|
||||
|
||||
default okhttp3.Call getCaptcha(@Nonnull String cookies, @Nonnull HttpLoggingInterceptor.Level logLevel) {
|
||||
return new OkHttpClient.Builder()
|
||||
.addInterceptor(new HttpLoggingInterceptor().setLevel(logLevel))
|
||||
.build()
|
||||
.newCall(
|
||||
new Request.Builder()
|
||||
.url(BaseUrlDefinition.PASSPORT + "captcha")
|
||||
.header("Cookie", cookies)
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
@POST("api/oauth2/getKey")
|
||||
Call<KeyEntity> getKey();
|
||||
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.hiczp.bilibili.api.provider;
|
||||
|
||||
import com.hiczp.bilibili.api.passport.CaptchaService;
|
||||
|
||||
public interface BilibiliCaptchaProvider {
|
||||
CaptchaService getCaptchaService();
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.hiczp.bilibili.api;
|
||||
package com.hiczp.bilibili.api.provider;
|
||||
|
||||
import com.hiczp.bilibili.api.live.LiveService;
|
||||
import com.hiczp.bilibili.api.passport.PassportService;
|
@ -1,4 +1,4 @@
|
||||
package com.hiczp.bilibili.api;
|
||||
package com.hiczp.bilibili.api.provider;
|
||||
|
||||
import com.hiczp.bilibili.api.passport.SsoService;
|
||||
import okhttp3.Cookie;
|
@ -1,7 +1,6 @@
|
||||
package com.hiczp.bilibili.api.test;
|
||||
|
||||
import com.hiczp.bilibili.api.BilibiliAPI;
|
||||
import okhttp3.Response;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
@ -47,17 +46,12 @@ public class CaptchaInputDialog extends JDialog {
|
||||
|
||||
private void createUIComponents() {
|
||||
try {
|
||||
cookie = new BilibiliAPI().getPassportService().getKey()
|
||||
BilibiliAPI bilibiliAPI = new BilibiliAPI();
|
||||
cookie = bilibiliAPI.getPassportService().getKey()
|
||||
.execute()
|
||||
.headers()
|
||||
.get("Set-cookie");
|
||||
Response response = Config.getBilibiliAPI().getPassportService()
|
||||
.getCaptcha(cookie)
|
||||
.execute();
|
||||
if (response.code() != 200) {
|
||||
throw new IOException(response.message());
|
||||
}
|
||||
label = new JLabel(new ImageIcon(ImageIO.read(response.body().byteStream())));
|
||||
label = new JLabel(new ImageIcon(ImageIO.read(bilibiliAPI.getCaptchaService().getCaptchaAsStream(cookie))));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -3,14 +3,11 @@ package com.hiczp.bilibili.api.test;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.hiczp.bilibili.api.BilibiliAPI;
|
||||
import com.hiczp.bilibili.api.ServerErrorCode;
|
||||
import com.hiczp.bilibili.api.passport.entity.InfoEntity;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.naming.AuthenticationException;
|
||||
|
||||
public class UserInfoTest {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(UserInfoTest.class);
|
||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||
@ -18,14 +15,7 @@ public class UserInfoTest {
|
||||
|
||||
@Test
|
||||
public void getUserInfo() throws Exception {
|
||||
InfoEntity infoEntity = BILIBILI_API.getPassportService()
|
||||
.getInfo(BILIBILI_API.getBilibiliAccount().getAccessToken())
|
||||
.execute()
|
||||
.body();
|
||||
if (infoEntity.getCode() == ServerErrorCode.Common.OK) {
|
||||
LOGGER.info("UserInfo below: \n{}", GSON.toJson(infoEntity));
|
||||
} else {
|
||||
throw new AuthenticationException(infoEntity.getMessage());
|
||||
}
|
||||
InfoEntity infoEntity = BILIBILI_API.getAccountInfo();
|
||||
LOGGER.info("UserInfo below: \n{}", GSON.toJson(infoEntity));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user