diff --git a/src/main/java/com/hiczp/bilibili/api/BilibiliAPI.java b/src/main/java/com/hiczp/bilibili/api/BilibiliAPI.java index 14bb0a7..42fe04d 100644 --- a/src/main/java/com/hiczp/bilibili/api/BilibiliAPI.java +++ b/src/main/java/com/hiczp/bilibili/api/BilibiliAPI.java @@ -124,39 +124,43 @@ public class BilibiliAPI implements BilibiliServiceProvider, LiveClientProvider } public LoginResponseEntity login(String username, String password) throws IOException, LoginException { - LOGGER.debug("Login attempting with username '{}'", username); + LOGGER.info("Login attempting with username '{}'", username); LoginResponseEntity loginResponseEntity = BilibiliSecurityHelper.login( this, username, password ); bilibiliAccount.copyFrom(loginResponseEntity.toBilibiliAccount()); + LOGGER.info("Login succeed with username: {}", username); return loginResponseEntity; } public RefreshTokenResponseEntity refreshToken() throws IOException, LoginException { - LOGGER.debug("RefreshToken attempting with userId '{}'", bilibiliAccount.getUserId()); + LOGGER.info("RefreshToken attempting with userId '{}'", bilibiliAccount.getUserId()); RefreshTokenResponseEntity refreshTokenResponseEntity = BilibiliSecurityHelper.refreshToken( this, bilibiliAccount.getAccessToken(), bilibiliAccount.getRefreshToken() ); bilibiliAccount.copyFrom(refreshTokenResponseEntity.toBilibiliAccount()); + LOGGER.info("RefreshToken succeed with userId: {}", bilibiliAccount.getUserId()); return refreshTokenResponseEntity; } public LogoutResponseEntity logout() throws IOException, LoginException { - LOGGER.debug("Logout attempting with userId '{}'", bilibiliAccount.getUserId()); + LOGGER.info("Logout attempting with userId '{}'", bilibiliAccount.getUserId()); Long userId = bilibiliAccount.getUserId(); LogoutResponseEntity logoutResponseEntity = BilibiliSecurityHelper.logout(this, bilibiliAccount.getAccessToken()); bilibiliAccount.reset(); - LOGGER.debug("Logout succeed with userId: {}", userId); + LOGGER.info("Logout succeed with userId: {}", userId); return logoutResponseEntity; } @Override public LiveClient getLiveClient(long showRoomId) { - return bilibiliAccount.getUserId() == null ? new LiveClient(showRoomId) : new LiveClient(showRoomId, bilibiliAccount.getUserId()); + return bilibiliAccount.getUserId() == null ? + new LiveClient(this, showRoomId) : + new LiveClient(this, showRoomId, bilibiliAccount.getUserId()); } public BilibiliClientProperties getBilibiliClientProperties() { 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 0886703..0a907d7 100644 --- a/src/main/java/com/hiczp/bilibili/api/live/LiveService.java +++ b/src/main/java/com/hiczp/bilibili/api/live/LiveService.java @@ -14,13 +14,13 @@ public interface LiveService { Call getBulletScreenConfig(@Query("type") String type); @GET("AppRoom/msg") - Call getHistoryBulletScreens(@Query("room_id") int roomId); + Call getHistoryBulletScreens(@Query("room_id") long roomId); @GET("AppRoom/index") - Call getRoomInfo(@Query("room_id") int roomId); + Call getRoomInfo(@Query("room_id") long roomId); @POST("feed/v1/feed/isFollowed") - Call isFollowed(@Query("follow") int hostUserId); + Call isFollowed(@Query("follow") long hostUserId); //该 API 意义不明 @GET("AppBag/sendDaily") @@ -39,7 +39,7 @@ public interface LiveService { //这个 API 不是很明确, 所有房间都一样 @GET("SpecialGift/room/{roomId}") - Call getSpecialGift(@Path("roomId") int roomId); + Call getSpecialGift(@Path("roomId") long roomId); @GET("mobile/getUser") Call getUserInfo(); @@ -63,11 +63,11 @@ public interface LiveService { // 3 // @GET("api/playurl") - Call getPlayUrl(@Query("cid") int cid, @Query("otype") String outputType); + Call getPlayUrl(@Query("cid") long cid, @Query("otype") String outputType); @POST("mobile/userOnlineHeart") @FormUrlEncoded - Call sendOnlineHeart(@Field("room_id") int roomId, @Field("scale") String scale); + Call sendOnlineHeart(@Field("room_id") long roomId, @Field("scale") String scale); @POST("api/sendmsg") @FormUrlEncoded @@ -107,16 +107,16 @@ public interface LiveService { Call getPlayerBag(); @GET("AppRoom/activityGift") - Call getActivityGifts(@Query("room_id") int roomId); + Call getActivityGifts(@Query("room_id") long roomId); @POST("AppBag/send") @FormUrlEncoded - Call sendGift(@Field("giftId") int giftId, - @Field("num") int number, - @Field("ruid") int roomUserId, - @Field("roomid") int roomId, + Call sendGift(@Field("giftId") long giftId, + @Field("num") long number, + @Field("ruid") long roomUserId, + @Field("roomid") long roomId, @Field("timestamp") long timeStamp, - @Field("bag_id") int bagId, + @Field("bag_id") long bagId, @Field("rnd") long random); default Call sendGift(GiftEntity giftEntity) { diff --git a/src/main/java/com/hiczp/bilibili/api/live/entity/GiftEntity.java b/src/main/java/com/hiczp/bilibili/api/live/entity/GiftEntity.java index 33dc264..19f15c1 100644 --- a/src/main/java/com/hiczp/bilibili/api/live/entity/GiftEntity.java +++ b/src/main/java/com/hiczp/bilibili/api/live/entity/GiftEntity.java @@ -6,19 +6,19 @@ import java.time.Instant; public class GiftEntity { @SerializedName("giftId") - private int giftId; + private long giftId; @SerializedName("bag_id") - private int bagId; + private long bagId; @SerializedName("num") - private int number; + private long number; @SerializedName("roomid") - private int roomId; + private long roomId; @SerializedName("ruid") - private int roomUserId; + private long roomUserId; @SerializedName("timestamp") private long timeStamp = Instant.now().getEpochSecond(); @@ -27,7 +27,7 @@ public class GiftEntity { @SerializedName("rnd") private long random = (long) (Math.random() * 9999999999L); - public GiftEntity(int giftId, int bagId, int number, int roomId, int roomUserId) { + public GiftEntity(long giftId, long bagId, long number, long roomId, long roomUserId) { this.giftId = giftId; this.bagId = bagId; this.number = number; @@ -35,59 +35,59 @@ public class GiftEntity { this.roomUserId = roomUserId; } - public GiftEntity(int giftId, int bagId, int number, LiveRoomInfoEntity.LiveRoomEntity liveRoomEntity) { + public GiftEntity(long giftId, long bagId, long number, LiveRoomInfoEntity.LiveRoomEntity liveRoomEntity) { this(giftId, bagId, number, liveRoomEntity.getRoomId(), liveRoomEntity.getMid()); } - public GiftEntity(PlayerBagEntity.BagGiftEntity bagGiftEntity, int number, int roomId, int roomUserId) { + public GiftEntity(PlayerBagEntity.BagGiftEntity bagGiftEntity, long number, long roomId, long roomUserId) { this(bagGiftEntity.getGiftId(), bagGiftEntity.getId(), number, roomId, roomUserId); } - public GiftEntity(PlayerBagEntity.BagGiftEntity bagGiftEntity, int number, LiveRoomInfoEntity.LiveRoomEntity liveRoomEntity) { + public GiftEntity(PlayerBagEntity.BagGiftEntity bagGiftEntity, long number, LiveRoomInfoEntity.LiveRoomEntity liveRoomEntity) { this(bagGiftEntity.getGiftId(), bagGiftEntity.getId(), number, liveRoomEntity.getRoomId(), liveRoomEntity.getMid()); } - public int getGiftId() { + public long getGiftId() { return giftId; } - public GiftEntity setGiftId(int giftId) { + public GiftEntity setGiftId(long giftId) { this.giftId = giftId; return this; } - public int getBagId() { + public long getBagId() { return bagId; } - public GiftEntity setBagId(int bagId) { + public GiftEntity setBagId(long bagId) { this.bagId = bagId; return this; } - public int getNumber() { + public long getNumber() { return number; } - public GiftEntity setNumber(int number) { + public GiftEntity setNumber(long number) { this.number = number; return this; } - public int getRoomId() { + public long getRoomId() { return roomId; } - public GiftEntity setRoomId(int roomId) { + public GiftEntity setRoomId(long roomId) { this.roomId = roomId; return this; } - public int getRoomUserId() { + public long getRoomUserId() { return roomUserId; } - public GiftEntity setRoomUserId(int roomUserId) { + public GiftEntity setRoomUserId(long roomUserId) { this.roomUserId = roomUserId; return this; } diff --git a/src/main/java/com/hiczp/bilibili/api/live/entity/LiveRoomInfoEntity.java b/src/main/java/com/hiczp/bilibili/api/live/entity/LiveRoomInfoEntity.java index 6c3cf2b..763e6c9 100644 --- a/src/main/java/com/hiczp/bilibili/api/live/entity/LiveRoomInfoEntity.java +++ b/src/main/java/com/hiczp/bilibili/api/live/entity/LiveRoomInfoEntity.java @@ -97,13 +97,13 @@ public class LiveRoomInfoEntity { */ @SerializedName("room_id") - private int roomId; + private long roomId; @SerializedName("title") private String title; @SerializedName("cover") private String cover; @SerializedName("mid") - private int mid; + private long mid; @SerializedName("uname") private String uname; @SerializedName("face") @@ -197,11 +197,11 @@ public class LiveRoomInfoEntity { @SerializedName("event_corner") private List eventCorner; - public int getRoomId() { + public long getRoomId() { return roomId; } - public void setRoomId(int roomId) { + public void setRoomId(long roomId) { this.roomId = roomId; } @@ -221,11 +221,11 @@ public class LiveRoomInfoEntity { this.cover = cover; } - public int getMid() { + public long getMid() { return mid; } - public void setMid(int mid) { + public void setMid(long mid) { this.mid = mid; } @@ -615,15 +615,15 @@ public class LiveRoomInfoEntity { */ @SerializedName("cid") - private int cid; + private long cid; @SerializedName("sch_id") private int schId; @SerializedName("title") private String title; @SerializedName("mid") - private int mid; + private long mid; @SerializedName("start") - private int start; + private long start; @SerializedName("start_at") private String startAt; @SerializedName("aid") @@ -641,11 +641,11 @@ public class LiveRoomInfoEntity { @SerializedName("manager") private List manager; - public int getCid() { + public long getCid() { return cid; } - public void setCid(int cid) { + public void setCid(long cid) { this.cid = cid; } @@ -665,19 +665,19 @@ public class LiveRoomInfoEntity { this.title = title; } - public int getMid() { + public long getMid() { return mid; } - public void setMid(int mid) { + public void setMid(long mid) { this.mid = mid; } - public int getStart() { + public long getStart() { return start; } - public void setStart(int start) { + public void setStart(long start) { this.start = start; } @@ -1035,7 +1035,7 @@ public class LiveRoomInfoEntity { */ @SerializedName("id") - private int id; + private long id; @SerializedName("name") private String name; @SerializedName("price") @@ -1057,11 +1057,11 @@ public class LiveRoomInfoEntity { @SerializedName("count_map") private Map countMap; - public int getId() { + public long getId() { return id; } - public void setId(int id) { + public void setId(long id) { this.id = id; } @@ -1170,23 +1170,23 @@ public class LiveRoomInfoEntity { */ @SerializedName("id") - private int id; + private long id; @SerializedName("num") - private int num; + private long num; - public int getId() { + public long getId() { return id; } - public void setId(int id) { + public void setId(long id) { this.id = id; } - public int getNum() { + public long getNum() { return num; } - public void setNum(int num) { + public void setNum(long num) { this.num = num; } } diff --git a/src/main/java/com/hiczp/bilibili/api/live/entity/PlayerBagEntity.java b/src/main/java/com/hiczp/bilibili/api/live/entity/PlayerBagEntity.java index cdabd41..7137aed 100644 --- a/src/main/java/com/hiczp/bilibili/api/live/entity/PlayerBagEntity.java +++ b/src/main/java/com/hiczp/bilibili/api/live/entity/PlayerBagEntity.java @@ -61,17 +61,17 @@ public class PlayerBagEntity { */ @SerializedName("id") - private int id; + private long id; @SerializedName("uid") - private int uid; + private long uid; @SerializedName("gift_id") - private int giftId; + private long giftId; @SerializedName("gift_num") - private int giftNum; + private long giftNum; @SerializedName("expireat") - private int expireat; + private long expireat; @SerializedName("gift_type") - private int giftType; + private long giftType; @SerializedName("gift_name") private String giftName; @SerializedName("gift_price") @@ -81,57 +81,57 @@ public class PlayerBagEntity { @SerializedName("count_set") private String countSet; @SerializedName("combo_num") - private int comboNum; + private long comboNum; @SerializedName("super_num") - private int superNum; + private long superNum; @SerializedName("count_map") private Map countMap; - public int getId() { + public long getId() { return id; } - public void setId(int id) { + public void setId(long id) { this.id = id; } - public int getUid() { + public long getUid() { return uid; } - public void setUid(int uid) { + public void setUid(long uid) { this.uid = uid; } - public int getGiftId() { + public long getGiftId() { return giftId; } - public void setGiftId(int giftId) { + public void setGiftId(long giftId) { this.giftId = giftId; } - public int getGiftNum() { + public long getGiftNum() { return giftNum; } - public void setGiftNum(int giftNum) { + public void setGiftNum(long giftNum) { this.giftNum = giftNum; } - public int getExpireat() { + public long getExpireat() { return expireat; } - public void setExpireat(int expireat) { + public void setExpireat(long expireat) { this.expireat = expireat; } - public int getGiftType() { + public long getGiftType() { return giftType; } - public void setGiftType(int giftType) { + public void setGiftType(long giftType) { this.giftType = giftType; } @@ -167,19 +167,19 @@ public class PlayerBagEntity { this.countSet = countSet; } - public int getComboNum() { + public long getComboNum() { return comboNum; } - public void setComboNum(int comboNum) { + public void setComboNum(long comboNum) { this.comboNum = comboNum; } - public int getSuperNum() { + public long getSuperNum() { return superNum; } - public void setSuperNum(int superNum) { + public void setSuperNum(long superNum) { this.superNum = superNum; } diff --git a/src/main/java/com/hiczp/bilibili/api/live/socket/LiveClient.java b/src/main/java/com/hiczp/bilibili/api/live/socket/LiveClient.java index 59f95c6..f284d8a 100644 --- a/src/main/java/com/hiczp/bilibili/api/live/socket/LiveClient.java +++ b/src/main/java/com/hiczp/bilibili/api/live/socket/LiveClient.java @@ -1,25 +1,68 @@ package com.hiczp.bilibili.api.live.socket; +import com.google.common.eventbus.EventBus; +import com.hiczp.bilibili.api.BilibiliServiceProvider; +import com.hiczp.bilibili.api.live.entity.LiveRoomInfoEntity; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.util.Optional; + public class LiveClient { private static final Logger LOGGER = LoggerFactory.getLogger(LiveClient.class); + private final EventBus eventBus = new EventBus("BilibiliLiveClient"); + private final BilibiliServiceProvider bilibiliServiceProvider; private final long showRoomId; private final long userId; - public LiveClient(long showRoomId) { + private LiveRoomInfoEntity.LiveRoomEntity liveRoomEntity; + + public LiveClient(BilibiliServiceProvider bilibiliServiceProvider, long showRoomId) { + this.bilibiliServiceProvider = bilibiliServiceProvider; this.showRoomId = showRoomId; this.userId = 0; } - public LiveClient(long showRoomId, long userId) { + public LiveClient(BilibiliServiceProvider bilibiliServiceProvider, long showRoomId, long userId) { + this.bilibiliServiceProvider = bilibiliServiceProvider; this.showRoomId = showRoomId; this.userId = userId; } - public LiveClient connect() { + public synchronized LiveRoomInfoEntity.LiveRoomEntity fetchRoomInfo() throws IOException { + liveRoomEntity = bilibiliServiceProvider.getLiveService() + .getRoomInfo(showRoomId) + .execute() + .body() + .getData(); + return liveRoomEntity; + } + + public LiveClient connect() throws IOException { + LOGGER.info("Try to connect to live room {}", showRoomId); + fetchRoomInfo(); + LOGGER.info("Get actual room id {}", liveRoomEntity.getRoomId()); + + LOGGER.debug("Init NioEventLoop"); + throw new UnsupportedOperationException(); } + + public EventBus getEventBus() { + return eventBus; + } + + public long getShowRoomId() { + return showRoomId; + } + + public long getUserId() { + return userId; + } + + public Optional getLiveRoomEntity() { + return Optional.of(liveRoomEntity); + } }