Skip to content

Commit b2305b7

Browse files
authored
Merge pull request #39 from kohlerpop1/kohlerpop1-fixes-updates
Added startTime to LiveRoomInfo to determine when the stream started!
2 parents e44cb71 + 7b91183 commit b2305b7

File tree

8 files changed

+30
-62
lines changed

8 files changed

+30
-62
lines changed

API/src/main/java/io/github/jwdeveloper/tiktok/data/dto/TikTokUserInfo.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ public class TikTokUserInfo
3333

3434
String roomId;
3535

36+
long startTime;
37+
3638
public enum UserStatus
3739
{
3840
NotFound,
3941
Offline,
4042
LivePaused,
4143
Live
4244
}
43-
}
45+
}

API/src/main/java/io/github/jwdeveloper/tiktok/live/LiveRoomInfo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ public interface LiveRoomInfo
4242
*/
4343
int getTotalViewersCount();
4444
int getLikesCount();
45+
long getStartTime();
4546
boolean isAgeRestricted();
4647
String getRoomId();
4748
String getHostName();
4849
String getTitle();
4950
User getHostUser();
5051
List<RankingUser> getUsersRanking();
5152
ConnectionState getConnectionState();
52-
}
53+
}

Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClient.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
package io.github.jwdeveloper.tiktok;
2424

25+
import io.github.jwdeveloper.tiktok.data.dto.TikTokUserInfo;
2526
import io.github.jwdeveloper.tiktok.data.events.TikTokDisconnectedEvent;
2627
import io.github.jwdeveloper.tiktok.data.events.TikTokErrorEvent;
2728
import io.github.jwdeveloper.tiktok.data.events.TikTokReconnectingEvent;
@@ -135,13 +136,15 @@ public void tryConnect() {
135136

136137
apiService.updateSessionId();
137138

139+
TikTokUserInfo info = apiService.fetchUserInfoFromTikTokApi(liveRoomInfo.getHostName());
140+
liveRoomInfo.setStartTime(info.getStartTime());
138141
if (clientSettings.getRoomId() != null) {
139142
liveRoomInfo.setRoomId(clientSettings.getRoomId());
140143
logger.info("Using roomID from settings: " + clientSettings.getRoomId());
141144
} else {
142-
var roomId = apiService.fetchRoomId(liveRoomInfo.getHostName());
143-
liveRoomInfo.setRoomId(roomId);
145+
liveRoomInfo.setRoomId(info.getRoomId());
144146
}
147+
apiService.updateRoomId(liveRoomInfo.getRoomId());
145148

146149

147150
var liveRoomMeta = apiService.fetchRoomInfo();
@@ -194,4 +197,4 @@ public void publishEvent(TikTokEvent event)
194197
{
195198
tikTokEventHandler.publish(this, event);
196199
}
197-
}
200+
}

Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClientBuilder.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ protected void validate() {
113113
clientSettings.setTimeout(Duration.ofSeconds(Constants.DEFAULT_TIMEOUT));
114114
}
115115

116-
if (clientSettings.getClientLanguage() == null || clientSettings.getClientLanguage().equals("")) {
116+
if (clientSettings.getClientLanguage() == null || clientSettings.getClientLanguage().isEmpty()) {
117117
clientSettings.setClientLanguage(Constants.DefaultClientSettings().getClientLanguage());
118118
}
119119

120120

121-
if (clientSettings.getHostName() == null || clientSettings.getHostName().equals("")) {
121+
if (clientSettings.getHostName() == null || clientSettings.getHostName().isEmpty()) {
122122
throw new TikTokLiveException("HostName can not be null");
123123
}
124124

@@ -552,11 +552,4 @@ public TikTokLiveClientBuilder onReconnecting(EventConsumer<TikTokReconnectingEv
552552
tikTokEventHandler.subscribe(TikTokReconnectingEvent.class, event);
553553
return this;
554554
}
555-
}
556-
557-
558-
559-
560-
561-
562-
555+
}

Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokRoomInfo.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@
2222
*/
2323
package io.github.jwdeveloper.tiktok;
2424

25-
import io.github.jwdeveloper.tiktok.data.models.Picture;
2625
import io.github.jwdeveloper.tiktok.data.models.RankingUser;
2726
import io.github.jwdeveloper.tiktok.data.models.users.User;
28-
import io.github.jwdeveloper.tiktok.models.ConnectionState;
2927
import io.github.jwdeveloper.tiktok.live.LiveRoomInfo;
28+
import io.github.jwdeveloper.tiktok.models.ConnectionState;
3029
import lombok.Data;
3130

3231
import java.util.LinkedList;
@@ -42,6 +41,8 @@ public class TikTokRoomInfo implements LiveRoomInfo {
4241

4342
private int totalViewersCount;
4443

44+
private long startTime;
45+
4546
private boolean ageRestricted;
4647

4748
private User host;
@@ -69,5 +70,4 @@ public void updateRanking(List<RankingUser> rankingUsers) {
6970
usersRanking.clear();
7071
usersRanking.addAll(rankingUsers);
7172
}
72-
73-
}
73+
}

Client/src/main/java/io/github/jwdeveloper/tiktok/http/TikTokApiService.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,16 @@
2222
*/
2323
package io.github.jwdeveloper.tiktok.http;
2424

25-
import com.google.gson.GsonBuilder;
26-
import com.google.gson.JsonObject;
27-
import com.google.gson.JsonParser;
25+
import com.google.gson.*;
2826
import io.github.jwdeveloper.tiktok.ClientSettings;
2927
import io.github.jwdeveloper.tiktok.data.dto.TikTokUserInfo;
30-
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveOfflineHostException;
3128
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException;
3229
import io.github.jwdeveloper.tiktok.live.LiveRoomMeta;
3330
import io.github.jwdeveloper.tiktok.mappers.LiveRoomMetaMapper;
3431
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse;
3532

3633
import java.util.HashMap;
3734
import java.util.logging.Logger;
38-
import java.util.regex.Pattern;
3935

4036
public class TikTokApiService {
4137
private final TikTokHttpClient tiktokHttpClient;
@@ -48,7 +44,6 @@ public TikTokApiService(TikTokHttpClient apiClient, Logger logger, ClientSetting
4844
this.clientSettings = clientSettings;
4945
}
5046

51-
5247
public void updateSessionId() {
5348
if (clientSettings.getSessionId() == null) {
5449
return;
@@ -59,24 +54,19 @@ public void updateSessionId() {
5954
tiktokHttpClient.setSessionId(clientSettings.getSessionId());
6055
}
6156

62-
public String fetchRoomId(String userName) {
63-
var userInfo = fetchUserInfoFromTikTokApi(userName);
64-
clientSettings.getClientParameters().put("room_id", userInfo.getRoomId());
65-
logger.info("RoomID -> " + userInfo.getRoomId());
66-
return userInfo.getRoomId();
57+
public void updateRoomId(String roomId)
58+
{
59+
clientSettings.getClientParameters().put("room_id", roomId);
6760
}
6861

6962
public TikTokUserInfo fetchUserInfoFromTikTokApi(String userName) {
70-
7163
var params = new HashMap<>(clientSettings.getClientParameters());
7264
params.put("uniqueId", userName);
7365
params.put("sourceType", 54);
74-
JsonObject roomData = null;
66+
JsonObject roomData;
7567
try {
7668
roomData = tiktokHttpClient.getJsonFromTikTokApi("api-live/user/room/", params);
7769
} catch (Exception e) {
78-
79-
8070
throw new TikTokLiveRequestException("Failed to fetch pre connection room information, it happens when TikTok temporary blocks you. Try to connect again in few minutes");
8171
}
8272

@@ -86,7 +76,7 @@ public TikTokUserInfo fetchUserInfoFromTikTokApi(String userName) {
8676
throw new TikTokLiveRequestException("fetchRoomIdFromTiktokApi -> Unable to fetch roomID, contact with developer");
8777
}
8878
if (message.equals("user_not_found")) {
89-
return new TikTokUserInfo(TikTokUserInfo.UserStatus.NotFound, "");
79+
return new TikTokUserInfo(TikTokUserInfo.UserStatus.NotFound, "", -1);
9080
}
9181
//live -> status 2
9282
//live paused -> 3
@@ -96,14 +86,17 @@ public TikTokUserInfo fetchUserInfoFromTikTokApi(String userName) {
9686
var roomId = user.get("roomId").getAsString();
9787
var status = user.get("status").getAsInt();
9888

89+
var liveRoom = data.getAsJsonObject("liveRoom");
90+
long startTime = liveRoom.get("startTime").getAsLong();
91+
9992
var statusEnum = switch (status) {
10093
case 2 -> TikTokUserInfo.UserStatus.Live;
10194
case 3 -> TikTokUserInfo.UserStatus.LivePaused;
10295
case 4 -> TikTokUserInfo.UserStatus.Offline;
10396
default -> TikTokUserInfo.UserStatus.NotFound;
10497
};
10598

106-
return new TikTokUserInfo(statusEnum, roomId);
99+
return new TikTokUserInfo(statusEnum, roomId, startTime);
107100
}
108101

109102

@@ -138,4 +131,4 @@ public WebcastResponse fetchClientData() {
138131
throw new TikTokLiveRequestException("Failed to fetch live websocket connection data", e);
139132
}
140133
}
141-
}
134+
}

Client/src/main/java/io/github/jwdeveloper/tiktok/mappers/LiveRoomMetaMapper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
*/
2323
package io.github.jwdeveloper.tiktok.mappers;
2424

25-
import com.google.gson.JsonElement;
2625
import com.google.gson.JsonObject;
2726
import io.github.jwdeveloper.tiktok.data.models.Picture;
2827
import io.github.jwdeveloper.tiktok.data.models.users.User;
@@ -123,4 +122,4 @@ public User getUser(JsonObject jsonElement)
123122
user.addAttribute(UserAttribute.LiveHost);
124123
return user;
125124
}
126-
}
125+
}

Client/src/test/java/io/github/jwdeveloper/tiktok/http/TikTokApiServiceTest.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import com.google.gson.JsonObject;
2626
import io.github.jwdeveloper.tiktok.ClientSettings;
27-
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
2827
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException;
2928
import io.github.jwdeveloper.tiktok.live.LiveRoomMeta;
3029
import io.github.jwdeveloper.tiktok.mappers.LiveRoomMetaMapper;
@@ -84,28 +83,6 @@ void updateSessionId_ValidSessionId_SetsSessionId() {
8483
verify(tiktokHttpClient, times(1)).setSessionId("validSessionId");
8584
}
8685

87-
88-
// @Test
89-
void fetchRoomId_ValidResponse_ReturnsRoomId() throws Exception {
90-
String expectedRoomId = "123456";
91-
String htmlResponse = "room_id=" + expectedRoomId ;
92-
when(tiktokHttpClient.getLivestreamPage(anyString())).thenReturn(htmlResponse);
93-
94-
String roomId = tikTokApiService.fetchRoomId("username");
95-
96-
assertEquals(expectedRoomId, roomId);
97-
verify(clientSettings.getClientParameters()).put("room_id", expectedRoomId);
98-
}
99-
100-
// @Test
101-
void fetchRoomId_ExceptionThrown_ThrowsTikTokLiveRequestException() throws Exception {
102-
when(tiktokHttpClient.getLivestreamPage(anyString())).thenThrow(new Exception("some exception"));
103-
104-
assertThrows(TikTokLiveRequestException.class, () -> {
105-
tikTokApiService.fetchRoomId("username");
106-
});
107-
}
108-
10986
//@Test
11087
void fetchRoomInfo_ValidResponse_ReturnsLiveRoomMeta() throws Exception {
11188
HashMap<String, Object> clientParameters = new HashMap<>();

0 commit comments

Comments
 (0)