mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
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
This commit is contained in:
@@ -56,7 +56,7 @@ public class ClientSettings {
|
||||
private boolean printMessageData;
|
||||
|
||||
/**
|
||||
* Tiktok user name
|
||||
* Optional: Use it if you need to change tiktok live hostname in builder
|
||||
*/
|
||||
private String hostName;
|
||||
|
||||
@@ -73,5 +73,11 @@ public class ClientSettings {
|
||||
*/
|
||||
private String sessionId;
|
||||
|
||||
/*
|
||||
* Optional: By default roomID is fetched before connect to live, but you can set it manually
|
||||
*
|
||||
*/
|
||||
private String roomId;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.github.jwdeveloper.tiktok.live;
|
||||
public interface LiveRoomInfo
|
||||
{
|
||||
int getViewersCount();
|
||||
boolean isAgeRestricted();
|
||||
String getRoomId();
|
||||
String getUserName();
|
||||
ConnectionState getConnectionState();
|
||||
|
||||
@@ -6,4 +6,6 @@ import lombok.Data;
|
||||
public class LiveRoomMeta
|
||||
{
|
||||
private int status;
|
||||
|
||||
private boolean ageRestricted;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package io.github.jwdeveloper.tiktok.mappers;
|
||||
|
||||
public interface Mapper<SOURCE,TARGET>
|
||||
{
|
||||
TARGET mapFrom(SOURCE source);
|
||||
}
|
||||
@@ -89,8 +89,20 @@ public class TikTokLiveClient implements LiveClient {
|
||||
|
||||
|
||||
apiService.updateSessionId();
|
||||
|
||||
if(clientSettings.getRoomId() != null)
|
||||
{
|
||||
|
||||
liveRoomInfo.setRoomId(clientSettings.getRoomId());
|
||||
logger.info("Using roomID from settings: "+clientSettings.getRoomId());
|
||||
}
|
||||
else
|
||||
{
|
||||
var roomId = apiService.fetchRoomId(liveRoomInfo.getUserName());
|
||||
liveRoomInfo.setRoomId(roomId);
|
||||
}
|
||||
|
||||
|
||||
var roomData = apiService.fetchRoomInfo();
|
||||
if (roomData.getStatus() == 0 || roomData.getStatus() == 4) {
|
||||
throw new TikTokLiveOfflineHostException("LiveStream for HostID could not be found. Is the Host online?");
|
||||
|
||||
@@ -11,7 +11,10 @@ public class TikTokRoomInfo implements LiveRoomInfo
|
||||
|
||||
private String roomId;
|
||||
|
||||
private boolean ageRestricted;
|
||||
|
||||
private String userName;
|
||||
|
||||
private ConnectionState connectionState = ConnectionState.DISCONNECTED;
|
||||
|
||||
public boolean hasConnectionState(ConnectionState state)
|
||||
|
||||
@@ -6,6 +6,7 @@ import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveOfflineHostException;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException;
|
||||
import io.github.jwdeveloper.tiktok.live.LiveRoomMeta;
|
||||
import io.github.jwdeveloper.tiktok.mappers.LiveRoomMetaMapper;
|
||||
import io.github.jwdeveloper.tiktok.messages.WebcastResponse;
|
||||
import io.github.jwdeveloper.tiktok.models.gifts.TikTokGiftInfo;
|
||||
|
||||
@@ -100,22 +101,10 @@ public class TikTokApiService {
|
||||
logger.info("Fetch RoomInfo");
|
||||
try {
|
||||
var response = tiktokHttpClient.getJObjectFromWebcastAPI("room/info/", clientSettings.getClientParameters());
|
||||
if (!response.has("data")) {
|
||||
return new LiveRoomMeta();
|
||||
}
|
||||
|
||||
var data = response.getAsJsonObject("data");
|
||||
if (!data.has("status")) {
|
||||
return new LiveRoomMeta();
|
||||
}
|
||||
|
||||
var status = data.get("status");
|
||||
|
||||
var info = new LiveRoomMeta();
|
||||
info.setStatus(status.getAsInt());
|
||||
|
||||
logger.info("RoomInfo status -> " + info.getStatus());
|
||||
return info;
|
||||
var mapper = new LiveRoomMetaMapper();
|
||||
var liveRoomMeta = mapper.mapFrom(response);
|
||||
logger.info("RoomInfo status -> " + liveRoomMeta.getStatus());
|
||||
return liveRoomMeta;
|
||||
} catch (Exception e) {
|
||||
throw new TikTokLiveRequestException("Failed to fetch room info from WebCast, see stacktrace for more info.", e);
|
||||
}
|
||||
|
||||
@@ -19,10 +19,9 @@ public class TikTokListenersManager implements ListenersManager {
|
||||
|
||||
public TikTokListenersManager(List<TikTokEventListener> listeners, TikTokEventObserver tikTokEventHandler) {
|
||||
this.eventObserver = tikTokEventHandler;
|
||||
this.bindingModels = listeners.stream().map(this::bindToEvents).toList();
|
||||
this.bindingModels = new ArrayList<>(listeners.stream().map(this::bindToEvents).toList());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<TikTokEventListener> getListeners() {
|
||||
return bindingModels.stream().map(ListenerBindingModel::getListener).toList();
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package io.github.jwdeveloper.tiktok.mappers;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import io.github.jwdeveloper.tiktok.live.LiveRoomMeta;
|
||||
|
||||
public class LiveRoomMetaMapper implements Mapper<JsonObject, LiveRoomMeta>
|
||||
{
|
||||
@Override
|
||||
public LiveRoomMeta mapFrom(JsonObject input) {
|
||||
var liveRoomMeta = new LiveRoomMeta();
|
||||
|
||||
if (!input.has("data")) {
|
||||
return liveRoomMeta;
|
||||
}
|
||||
var data = input.getAsJsonObject("data");
|
||||
if (data.has("status")) {
|
||||
var status = data.get("status");
|
||||
liveRoomMeta.setStatus(status.getAsInt());
|
||||
}
|
||||
|
||||
|
||||
if(data.has("age_restricted"))
|
||||
{
|
||||
var element = data.getAsJsonObject("age_restricted");
|
||||
var restricted= element.get("restricted").getAsBoolean();
|
||||
liveRoomMeta.setAgeRestricted(restricted);
|
||||
}
|
||||
return liveRoomMeta;
|
||||
}
|
||||
}
|
||||
13
README.md
13
README.md
@@ -7,7 +7,7 @@ A Java library based on [TikTokLive](https://github.com/isaackogan/TikTokLive) a
|
||||
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
|
||||
|
||||
Do you prefer other programming languages?
|
||||
- **Node** orginal: [TikTok-Live-Connector](https://github.com/zerodytrash/TikTok-Live-Connector) by [@zerodytrash](https://github.com/zerodytrash)
|
||||
- **Node** orginal: [TikTok-Live-Connector](https://github.com/isaackogan/TikTok-Live-Connector) by [@zerodytrash](https://github.com/zerodytrash)
|
||||
- **Python** rewrite: [TikTokLive](https://github.com/isaackogan/TikTokLive) by [@isaackogan](https://github.com/isaackogan)
|
||||
- **Go** rewrite: [GoTikTokLive](https://github.com/Davincible/gotiktoklive) by [@Davincible](https://github.com/Davincible)
|
||||
- **C#** rewrite: [TikTokLiveSharp](https://github.com/frankvHoof93/TikTokLiveSharp) by [@frankvHoof93](https://github.com/frankvHoof93)
|
||||
@@ -37,7 +37,7 @@ Do you prefer other programming languages?
|
||||
<dependency>
|
||||
<groupId>com.github.jwdeveloper.TikTok-Live-Java</groupId>
|
||||
<artifactId>Client</artifactId>
|
||||
<version>0.0.22-Release</version>
|
||||
<version>0.0.23-Release</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -119,9 +119,12 @@ public class ConfigurationExample {
|
||||
clientSettings.setRetryOnConnectionFailure(true); // Reconnecting if TikTok user is offline
|
||||
clientSettings.setRetryConnectionTimeout(Duration.ofSeconds(1)); // Timeout before next reconnection
|
||||
|
||||
// Optional: Sometimes not every messages from chat are send to TikTokLiveJava to fix this issue you can set sessionId
|
||||
//Optional: Sometimes not every message from chat are send to TikTokLiveJava to fix this issue you can set sessionId
|
||||
// documentation how to obtain sessionId https://github.com/isaackogan/TikTok-Live-Connector#send-chat-messages
|
||||
clientSettings.setSessionId("86c3c8bf4b17ebb2d74bb7fa66fd0000");
|
||||
|
||||
//Optional:
|
||||
clientSettings.setRoomId("XXXXXXXXXXXXXXXXX");
|
||||
})
|
||||
.buildAndRun();
|
||||
System.in.read();
|
||||
@@ -131,8 +134,6 @@ public class ConfigurationExample {
|
||||
```
|
||||
## Listener Example
|
||||
|
||||
Listener is optional approach for handling events
|
||||
|
||||
```java
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
@@ -154,7 +155,7 @@ public class ListenerExample
|
||||
CustomListener customListener = new CustomListener();
|
||||
|
||||
// set tiktok username
|
||||
TikTokLive.newClient(Main.TEST_TIKTOK_USER)
|
||||
var client = TikTokLive.newClient(Main.TEST_TIKTOK_USER)
|
||||
.addListener(customListener)
|
||||
.buildAndRun();
|
||||
|
||||
|
||||
@@ -21,9 +21,12 @@ public class ConfigurationExample {
|
||||
clientSettings.setRetryOnConnectionFailure(true); // Reconnecting if TikTok user is offline
|
||||
clientSettings.setRetryConnectionTimeout(Duration.ofSeconds(1)); // Timeout before next reconnection
|
||||
|
||||
//Optional: Sometimes not every messages from chat are send to TikTokLiveJava to fix this issue you can set sessionId
|
||||
//Optional: Sometimes not every message from chat are send to TikTokLiveJava to fix this issue you can set sessionId
|
||||
// documentation how to obtain sessionId https://github.com/isaackogan/TikTok-Live-Connector#send-chat-messages
|
||||
clientSettings.setSessionId("86c3c8bf4b17ebb2d74bb7fa66fd0000");
|
||||
|
||||
//Optional:
|
||||
clientSettings.setRoomId("XXXXXXXXXXXXXXXXX");
|
||||
})
|
||||
.buildAndRun();
|
||||
System.in.read();
|
||||
|
||||
@@ -18,7 +18,7 @@ public class ListenerExample
|
||||
CustomListener customListener = new CustomListener();
|
||||
|
||||
// set tiktok username
|
||||
TikTokLive.newClient(Main.TEST_TIKTOK_USER)
|
||||
var client = TikTokLive.newClient(Main.TEST_TIKTOK_USER)
|
||||
.addListener(customListener)
|
||||
.buildAndRun();
|
||||
|
||||
|
||||
@@ -35,5 +35,11 @@
|
||||
<version>0.0.7-Release</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.reflections</groupId>
|
||||
<artifactId>reflections</artifactId>
|
||||
<version>0.9.12</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
Reference in New Issue
Block a user