Skip to content

Commit 84bbfa9

Browse files
committed
Fixed bugs:
- addListeners() was throwing exception - 404 response code was return while connecting to tiktok for some users - configure.setRoomId() method for people that want to set roomId manually - client.roomInfo().isAgeRestricted() check if live has age restriction
1 parent a2da516 commit 84bbfa9

File tree

13 files changed

+106
-48
lines changed

13 files changed

+106
-48
lines changed

API/src/main/java/io/github/jwdeveloper/tiktok/ClientSettings.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class ClientSettings {
5656
private boolean printMessageData;
5757

5858
/**
59-
* Tiktok user name
59+
* Optional: Use it if you need to change tiktok live hostname in builder
6060
*/
6161
private String hostName;
6262

@@ -68,10 +68,16 @@ public class ClientSettings {
6868

6969

7070
/*
71-
* Optional: Sometimes not every messages from chat are send to TikTokLiveJava to fix this issue you can set sessionId
71+
* Optional: Sometimes not every messages from chat are send to TikTokLiveJava to fix this issue you can set sessionId
7272
* documentation how to obtain sessionId https://github.com/isaackogan/TikTok-Live-Connector#send-chat-messages
7373
*/
7474
private String sessionId;
7575

76+
/*
77+
* Optional: By default roomID is fetched before connect to live, but you can set it manually
78+
*
79+
*/
80+
private String roomId;
81+
7682
}
7783

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public interface LiveRoomInfo
44
{
55
int getViewersCount();
6+
boolean isAgeRestricted();
67
String getRoomId();
78
String getUserName();
89
ConnectionState getConnectionState();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
public class LiveRoomMeta
77
{
88
private int status;
9+
10+
private boolean ageRestricted;
911
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package io.github.jwdeveloper.tiktok.mappers;
2+
3+
public interface Mapper<SOURCE,TARGET>
4+
{
5+
TARGET mapFrom(SOURCE source);
6+
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,20 @@ public void tryConnect() {
8989

9090

9191
apiService.updateSessionId();
92-
var roomId = apiService.fetchRoomId(liveRoomInfo.getUserName());
93-
liveRoomInfo.setRoomId(roomId);
92+
93+
if(clientSettings.getRoomId() != null)
94+
{
95+
96+
liveRoomInfo.setRoomId(clientSettings.getRoomId());
97+
logger.info("Using roomID from settings: "+clientSettings.getRoomId());
98+
}
99+
else
100+
{
101+
var roomId = apiService.fetchRoomId(liveRoomInfo.getUserName());
102+
liveRoomInfo.setRoomId(roomId);
103+
}
104+
105+
94106
var roomData = apiService.fetchRoomInfo();
95107
if (roomData.getStatus() == 0 || roomData.getStatus() == 4) {
96108
throw new TikTokLiveOfflineHostException("LiveStream for HostID could not be found. Is the Host online?");

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ public class TikTokRoomInfo implements LiveRoomInfo
1111

1212
private String roomId;
1313

14+
private boolean ageRestricted;
15+
1416
private String userName;
17+
1518
private ConnectionState connectionState = ConnectionState.DISCONNECTED;
1619

1720
public boolean hasConnectionState(ConnectionState state)

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveOfflineHostException;
77
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException;
88
import io.github.jwdeveloper.tiktok.live.LiveRoomMeta;
9+
import io.github.jwdeveloper.tiktok.mappers.LiveRoomMetaMapper;
910
import io.github.jwdeveloper.tiktok.messages.WebcastResponse;
1011
import io.github.jwdeveloper.tiktok.models.gifts.TikTokGiftInfo;
1112

@@ -100,22 +101,10 @@ public LiveRoomMeta fetchRoomInfo() {
100101
logger.info("Fetch RoomInfo");
101102
try {
102103
var response = tiktokHttpClient.getJObjectFromWebcastAPI("room/info/", clientSettings.getClientParameters());
103-
if (!response.has("data")) {
104-
return new LiveRoomMeta();
105-
}
106-
107-
var data = response.getAsJsonObject("data");
108-
if (!data.has("status")) {
109-
return new LiveRoomMeta();
110-
}
111-
112-
var status = data.get("status");
113-
114-
var info = new LiveRoomMeta();
115-
info.setStatus(status.getAsInt());
116-
117-
logger.info("RoomInfo status -> " + info.getStatus());
118-
return info;
104+
var mapper = new LiveRoomMetaMapper();
105+
var liveRoomMeta = mapper.mapFrom(response);
106+
logger.info("RoomInfo status -> " + liveRoomMeta.getStatus());
107+
return liveRoomMeta;
119108
} catch (Exception e) {
120109
throw new TikTokLiveRequestException("Failed to fetch room info from WebCast, see stacktrace for more info.", e);
121110
}

Client/src/main/java/io/github/jwdeveloper/tiktok/listener/TikTokListenersManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ public class TikTokListenersManager implements ListenersManager {
1919

2020
public TikTokListenersManager(List<TikTokEventListener> listeners, TikTokEventObserver tikTokEventHandler) {
2121
this.eventObserver = tikTokEventHandler;
22-
this.bindingModels = listeners.stream().map(this::bindToEvents).toList();
22+
this.bindingModels = new ArrayList<>(listeners.stream().map(this::bindToEvents).toList());
2323
}
2424

25-
2625
@Override
2726
public List<TikTokEventListener> getListeners() {
2827
return bindingModels.stream().map(ListenerBindingModel::getListener).toList();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.github.jwdeveloper.tiktok.mappers;
2+
3+
import com.google.gson.JsonObject;
4+
import io.github.jwdeveloper.tiktok.live.LiveRoomMeta;
5+
6+
public class LiveRoomMetaMapper implements Mapper<JsonObject, LiveRoomMeta>
7+
{
8+
@Override
9+
public LiveRoomMeta mapFrom(JsonObject input) {
10+
var liveRoomMeta = new LiveRoomMeta();
11+
12+
if (!input.has("data")) {
13+
return liveRoomMeta;
14+
}
15+
var data = input.getAsJsonObject("data");
16+
if (data.has("status")) {
17+
var status = data.get("status");
18+
liveRoomMeta.setStatus(status.getAsInt());
19+
}
20+
21+
22+
if(data.has("age_restricted"))
23+
{
24+
var element = data.getAsJsonObject("age_restricted");
25+
var restricted= element.get("restricted").getAsBoolean();
26+
liveRoomMeta.setAgeRestricted(restricted);
27+
}
28+
return liveRoomMeta;
29+
}
30+
}

README.md

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ A Java library based on [TikTokLive](https://github.com/isaackogan/TikTokLive) a
77
Join the support [discord](https://discord.gg/e2XwPNTBBr) and visit the `#java-support` channel for questions, contributions and ideas. Feel free to make pull requests with missing/new features, fixes, etc
88

99
Do you prefer other programming languages?
10-
- **Node** orginal: [TikTok-Live-Connector](https://github.com/zerodytrash/TikTok-Live-Connector) by [@zerodytrash](https://github.com/zerodytrash)
10+
- **Node** orginal: [TikTok-Live-Connector](https://github.com/isaackogan/TikTok-Live-Connector) by [@zerodytrash](https://github.com/zerodytrash)
1111
- **Python** rewrite: [TikTokLive](https://github.com/isaackogan/TikTokLive) by [@isaackogan](https://github.com/isaackogan)
1212
- **Go** rewrite: [GoTikTokLive](https://github.com/Davincible/gotiktoklive) by [@Davincible](https://github.com/Davincible)
1313
- **C#** rewrite: [TikTokLiveSharp](https://github.com/frankvHoof93/TikTokLiveSharp) by [@frankvHoof93](https://github.com/frankvHoof93)
@@ -27,25 +27,25 @@ Do you prefer other programming languages?
2727

2828
```xml
2929
<repositories>
30-
<repository>
31-
<id>jitpack.io</id>
32-
<url>https://jitpack.io</url>
33-
</repository>
34-
</repositories>
35-
36-
<dependencies>
37-
<dependency>
38-
<groupId>com.github.jwdeveloper.TikTok-Live-Java</groupId>
39-
<artifactId>Client</artifactId>
40-
<version>0.0.22-Release</version>
41-
<scope>compile</scope>
42-
</dependency>
43-
<dependency>
44-
<groupId>com.google.code.gson</groupId>
45-
<artifactId>gson</artifactId>
46-
<version>2.10.1</version>
47-
</dependency>
48-
</dependencies>
30+
<repository>
31+
<id>jitpack.io</id>
32+
<url>https://jitpack.io</url>
33+
</repository>
34+
</repositories>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>com.github.jwdeveloper.TikTok-Live-Java</groupId>
39+
<artifactId>Client</artifactId>
40+
<version>0.0.23-Release</version>
41+
<scope>compile</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>com.google.code.gson</groupId>
45+
<artifactId>gson</artifactId>
46+
<version>2.10.1</version>
47+
</dependency>
48+
</dependencies>
4949
```
5050

5151
2. Create your first chat connection
@@ -119,9 +119,12 @@ public class ConfigurationExample {
119119
clientSettings.setRetryOnConnectionFailure(true); // Reconnecting if TikTok user is offline
120120
clientSettings.setRetryConnectionTimeout(Duration.ofSeconds(1)); // Timeout before next reconnection
121121

122-
// Optional: Sometimes not every messages from chat are send to TikTokLiveJava to fix this issue you can set sessionId
122+
//Optional: Sometimes not every message from chat are send to TikTokLiveJava to fix this issue you can set sessionId
123123
// documentation how to obtain sessionId https://github.com/isaackogan/TikTok-Live-Connector#send-chat-messages
124124
clientSettings.setSessionId("86c3c8bf4b17ebb2d74bb7fa66fd0000");
125+
126+
//Optional:
127+
clientSettings.setRoomId("XXXXXXXXXXXXXXXXX");
125128
})
126129
.buildAndRun();
127130
System.in.read();
@@ -131,8 +134,6 @@ public class ConfigurationExample {
131134
```
132135
## Listener Example
133136

134-
Listener is optional approach for handling events
135-
136137
```java
137138
package io.github.jwdeveloper.tiktok;
138139

@@ -154,7 +155,7 @@ public class ListenerExample
154155
CustomListener customListener = new CustomListener();
155156

156157
// set tiktok username
157-
TikTokLive.newClient(Main.TEST_TIKTOK_USER)
158+
var client = TikTokLive.newClient(Main.TEST_TIKTOK_USER)
158159
.addListener(customListener)
159160
.buildAndRun();
160161

0 commit comments

Comments
 (0)