mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-28 17:29:39 -05:00
Compare commits
21 Commits
develop-1.
...
1.5.2-Rele
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92fde03f2b | ||
|
|
e058290118 | ||
|
|
d25741b229 | ||
|
|
560a8d7c3b | ||
|
|
6178bc25cf | ||
|
|
48d1138754 | ||
|
|
a5320db820 | ||
|
|
4e1ab35a60 | ||
|
|
cef4972f37 | ||
|
|
713c90a271 | ||
|
|
71853db5cc | ||
|
|
ef90d4cd58 | ||
|
|
dad4048bc0 | ||
|
|
9ba049d37a | ||
|
|
f7d657371b | ||
|
|
29631ac468 | ||
|
|
15c642297c | ||
|
|
ead954dd27 | ||
|
|
e37b30ff12 | ||
|
|
7a5c00d99a | ||
|
|
407f51fa73 |
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>TikTokLiveJava</artifactId>
|
<artifactId>TikTokLiveJava</artifactId>
|
||||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||||
<version>1.3.0-Release</version>
|
<version>1.5.1-Release</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>API</artifactId>
|
<artifactId>API</artifactId>
|
||||||
|
|||||||
@@ -86,9 +86,15 @@ public class LiveClientSettings {
|
|||||||
private HttpClientSettings httpSettings;
|
private HttpClientSettings httpSettings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Optional: Sometimes not every messages from chat are send to TikTokLiveJava to fix this issue you can set sessionId
|
* Interval of time in milliseconds between pings to TikTok
|
||||||
* documentation how to obtain sessionId https://github.com/isaackogan/TikTok-Live-Connector#send-chat-messages
|
* @apiNote Min: 250 (0.25 seconds), Default: 5000 (5 seconds)
|
||||||
*/
|
*/
|
||||||
|
private long pingInterval = 5000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optional: Sometimes not every messages from chat are send to TikTokLiveJava to fix this issue you can set sessionId
|
||||||
|
* @see <a href="https://github.com/isaackogan/TikTok-Live-Connector#send-chat-messages">Documentation: How to obtain sessionId</a>
|
||||||
|
*/
|
||||||
private String sessionId;
|
private String sessionId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>TikTokLiveJava</artifactId>
|
<artifactId>TikTokLiveJava</artifactId>
|
||||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||||
<version>1.3.0-Release</version>
|
<version>1.5.1-Release</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ import io.github.jwdeveloper.tiktok.http.LiveHttpClient;
|
|||||||
import io.github.jwdeveloper.tiktok.live.GiftsManager;
|
import io.github.jwdeveloper.tiktok.live.GiftsManager;
|
||||||
import io.github.jwdeveloper.tiktok.live.builder.LiveClientBuilder;
|
import io.github.jwdeveloper.tiktok.live.builder.LiveClientBuilder;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
public class TikTokLive {
|
public class TikTokLive {
|
||||||
@@ -102,14 +101,9 @@ public class TikTokLive {
|
|||||||
* @return GiftsManager
|
* @return GiftsManager
|
||||||
*/
|
*/
|
||||||
public static GiftsManager gifts() {
|
public static GiftsManager gifts() {
|
||||||
if (giftsManager != null) {
|
if (giftsManager == null) {
|
||||||
return giftsManager;
|
synchronized (GiftsManager.class) {
|
||||||
}
|
giftsManager = new TikTokGiftsManager(requests().fetchGiftsData().getGifts());
|
||||||
synchronized (GiftsManager.class)
|
|
||||||
{
|
|
||||||
if (giftsManager == null)
|
|
||||||
{
|
|
||||||
return new TikTokGiftsManager(requests().fetchGiftsData().getGifts());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return giftsManager;
|
return giftsManager;
|
||||||
|
|||||||
@@ -96,6 +96,9 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
|
|||||||
if (clientSettings.getHostName().startsWith("@"))
|
if (clientSettings.getHostName().startsWith("@"))
|
||||||
clientSettings.setHostName(clientSettings.getHostName().substring(1));
|
clientSettings.setHostName(clientSettings.getHostName().substring(1));
|
||||||
|
|
||||||
|
if (clientSettings.getPingInterval() < 250)
|
||||||
|
throw new TikTokLiveException("Minimum allowed ping interval is 250 millseconds");
|
||||||
|
|
||||||
var httpSettings = clientSettings.getHttpSettings();
|
var httpSettings = clientSettings.getHttpSettings();
|
||||||
httpSettings.getParams().put("app_language", clientSettings.getClientLanguage());
|
httpSettings.getParams().put("app_language", clientSettings.getClientLanguage());
|
||||||
httpSettings.getParams().put("webcast_language", clientSettings.getClientLanguage());
|
httpSettings.getParams().put("webcast_language", clientSettings.getClientLanguage());
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
|
|||||||
.toJsonResponse();
|
.toJsonResponse();
|
||||||
|
|
||||||
if (result.isFailure())
|
if (result.isFailure())
|
||||||
throw new TikTokLiveRequestException("Unable to fetch gifts information's"+result.toStack());
|
throw new TikTokLiveRequestException("Unable to fetch gifts information's - "+result);
|
||||||
|
|
||||||
var json = result.getContent();
|
var json = result.getContent();
|
||||||
return giftsDataMapper.map(json);
|
return giftsDataMapper.map(json);
|
||||||
@@ -111,10 +111,10 @@ public class TikTokLiveHttpClient implements LiveHttpClient
|
|||||||
.toJsonResponse();
|
.toJsonResponse();
|
||||||
|
|
||||||
if (result.isFailure())
|
if (result.isFailure())
|
||||||
throw new TikTokLiveRequestException("Unable to get information's about user"+result.toStack());
|
throw new TikTokLiveRequestException("Unable to get information's about user - "+result);
|
||||||
|
|
||||||
var json = result.getContent();
|
var json = result.getContent();
|
||||||
return liveUserDataMapper.map(json);
|
return liveUserDataMapper.map(json, logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -138,7 +138,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
|
|||||||
.toJsonResponse();
|
.toJsonResponse();
|
||||||
|
|
||||||
if (result.isFailure())
|
if (result.isFailure())
|
||||||
throw new TikTokLiveRequestException("Unable to get info about live room"+result.toStack());
|
throw new TikTokLiveRequestException("Unable to get info about live room - "+result);
|
||||||
|
|
||||||
var json = result.getContent();
|
var json = result.getContent();
|
||||||
return liveDataMapper.map(json);
|
return liveDataMapper.map(json);
|
||||||
@@ -153,7 +153,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
|
|||||||
var resultHeader = ActionResult.of(credentialsResponse.headers().firstValue("x-set-tt-cookie"));
|
var resultHeader = ActionResult.of(credentialsResponse.headers().firstValue("x-set-tt-cookie"));
|
||||||
if (resultHeader.isFailure()) {
|
if (resultHeader.isFailure()) {
|
||||||
logger.warning("SignServer Headers: "+request.getRoomId()+" - "+credentialsResponse.headers().map());
|
logger.warning("SignServer Headers: "+request.getRoomId()+" - "+credentialsResponse.headers().map());
|
||||||
throw new TikTokSignServerException("Sign server did not return the x-set-tt-cookie header"+result.toStack());
|
throw new TikTokSignServerException("Sign server did not return the x-set-tt-cookie header - "+result);
|
||||||
}
|
}
|
||||||
var websocketCookie = resultHeader.getContent();
|
var websocketCookie = resultHeader.getContent();
|
||||||
var webcastResponse = WebcastResponse.parseFrom(credentialsResponse.body());
|
var webcastResponse = WebcastResponse.parseFrom(credentialsResponse.body());
|
||||||
@@ -169,7 +169,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
|
|||||||
|
|
||||||
return new LiveConnectionData.Response(websocketCookie, webSocketUrl, webcastResponse);
|
return new LiveConnectionData.Response(websocketCookie, webSocketUrl, webcastResponse);
|
||||||
} catch (InvalidProtocolBufferException e) {
|
} catch (InvalidProtocolBufferException e) {
|
||||||
throw new TikTokSignServerException("Unable to parse websocket credentials response to WebcastResponse"+result.toStack());
|
throw new TikTokSignServerException("Unable to parse websocket credentials response to WebcastResponse - "+result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,7 +197,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
|
|||||||
var result = builder.build().toResponse();
|
var result = builder.build().toResponse();
|
||||||
|
|
||||||
if (result.isFailure())
|
if (result.isFailure())
|
||||||
throw new TikTokSignServerException("Unable to get websocket connection credentials"+result.toStack());
|
throw new TikTokSignServerException("Unable to get websocket connection credentials - "+result);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,27 @@
|
|||||||
package io.github.jwdeveloper.tiktok.common;
|
package io.github.jwdeveloper.tiktok.common;
|
||||||
|
|
||||||
|
import com.google.gson.*;
|
||||||
|
import io.github.jwdeveloper.tiktok.http.mappers.*;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
import java.net.http.*;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class ActionResult<T> {
|
public class ActionResult<T> {
|
||||||
|
|
||||||
|
private static final Gson gson = new Gson().newBuilder().disableHtmlEscaping()
|
||||||
|
.registerTypeHierarchyAdapter(HttpResponse.class, new HttpResponseJsonMapper())
|
||||||
|
.registerTypeHierarchyAdapter(HttpRequest.class, new HttpRequestJsonMapper())
|
||||||
|
.setPrettyPrinting().create();
|
||||||
|
|
||||||
private boolean success = true;
|
private boolean success = true;
|
||||||
private T content;
|
private T content;
|
||||||
private String message;
|
private String message;
|
||||||
|
@Accessors(chain = true, fluent = true)
|
||||||
|
private ActionResult<?> previous;
|
||||||
|
|
||||||
protected ActionResult(T object) {
|
protected ActionResult(T object) {
|
||||||
this.content = object;
|
this.content = object;
|
||||||
@@ -41,8 +52,9 @@ public class ActionResult<T> {
|
|||||||
public boolean hasMessage() {
|
public boolean hasMessage() {
|
||||||
return message != null;
|
return message != null;
|
||||||
}
|
}
|
||||||
public String toStack() {
|
|
||||||
return hasMessage() ? " - "+message : "";
|
public boolean hasPrevious() {
|
||||||
|
return previous != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasContent() {
|
public boolean hasContent() {
|
||||||
@@ -84,4 +96,18 @@ public class ActionResult<T> {
|
|||||||
public static <T> ActionResult<T> failure() {
|
public static <T> ActionResult<T> failure() {
|
||||||
return failure(null);
|
return failure(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JsonObject toJson() {
|
||||||
|
JsonObject map = new JsonObject();
|
||||||
|
map.addProperty("success", success);
|
||||||
|
map.add("content", gson.toJsonTree(content));
|
||||||
|
map.addProperty("message", message);
|
||||||
|
map.add("previous", hasPrevious() ? previous.toJson() : null);
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ActionResult: "+gson.toJson(toJson());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
package io.github.jwdeveloper.tiktok.common;
|
package io.github.jwdeveloper.tiktok.common;
|
||||||
|
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -7,6 +10,8 @@ public class ActionResultBuilder<T>
|
|||||||
{
|
{
|
||||||
private final T content;
|
private final T content;
|
||||||
private String message;
|
private String message;
|
||||||
|
@Setter @Accessors(fluent = true, chain = true)
|
||||||
|
private ActionResult<?> previous;
|
||||||
|
|
||||||
public ActionResultBuilder(T content) {
|
public ActionResultBuilder(T content) {
|
||||||
this.content = content;
|
this.content = content;
|
||||||
@@ -18,10 +23,10 @@ public class ActionResultBuilder<T>
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ActionResult<T> success() {
|
public ActionResult<T> success() {
|
||||||
return ActionResult.success(content, message);
|
return ActionResult.success(content, message).previous(previous);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ActionResult<T> failure() {
|
public ActionResult<T> failure() {
|
||||||
return ActionResult.success(content, message);
|
return ActionResult.success(content, message).previous(previous);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package io.github.jwdeveloper.tiktok.http.mappers;
|
||||||
|
|
||||||
|
import com.google.gson.*;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
|
||||||
|
public class HttpRequestJsonMapper implements JsonSerializer<HttpRequest>
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public JsonElement serialize(HttpRequest src, Type typeOfSrc, JsonSerializationContext context) {
|
||||||
|
JsonObject object = new JsonObject();
|
||||||
|
object.addProperty("method", src.method());
|
||||||
|
object.add("timeout", context.serialize(src.timeout().toString()));
|
||||||
|
object.addProperty("expectContinue", src.expectContinue());
|
||||||
|
object.add("uri", context.serialize(src.uri()));
|
||||||
|
object.add("version", context.serialize(src.version().toString()));
|
||||||
|
object.add("headers", context.serialize(src.headers().map()));
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package io.github.jwdeveloper.tiktok.http.mappers;
|
||||||
|
|
||||||
|
import com.google.gson.*;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
|
||||||
|
public class HttpResponseJsonMapper implements JsonSerializer<HttpResponse>
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public JsonElement serialize(HttpResponse src, Type typeOfSrc, JsonSerializationContext context) {
|
||||||
|
JsonObject object = new JsonObject();
|
||||||
|
object.addProperty("statusCode", src.statusCode());
|
||||||
|
object.add("request", context.serialize(src.request()));
|
||||||
|
object.add("headers", context.serialize(src.headers().map()));
|
||||||
|
object.add("body", context.serialize(src.body()));
|
||||||
|
object.add("uri", context.serialize(src.uri().toString()));
|
||||||
|
object.add("version", context.serialize(src.version().toString()));
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,45 +22,52 @@
|
|||||||
*/
|
*/
|
||||||
package io.github.jwdeveloper.tiktok.http.mappers;
|
package io.github.jwdeveloper.tiktok.http.mappers;
|
||||||
|
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.*;
|
||||||
import io.github.jwdeveloper.tiktok.data.requests.LiveUserData;
|
import io.github.jwdeveloper.tiktok.data.requests.LiveUserData;
|
||||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException;
|
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class LiveUserDataMapper
|
public class LiveUserDataMapper
|
||||||
{
|
{
|
||||||
public LiveUserData.Response map(String json) {
|
public LiveUserData.Response map(String json, Logger logger) {
|
||||||
var jsonObject = JsonParser.parseString(json).getAsJsonObject();
|
try {
|
||||||
|
var jsonObject = JsonParser.parseString(json).getAsJsonObject();
|
||||||
|
|
||||||
var message = jsonObject.get("message").getAsString();
|
var message = jsonObject.get("message").getAsString();
|
||||||
|
|
||||||
if (message.equals("params_error")) {
|
if (message.equals("params_error")) {
|
||||||
throw new TikTokLiveRequestException("fetchRoomIdFromTiktokApi -> Unable to fetch roomID, contact the developer");
|
throw new TikTokLiveRequestException("fetchRoomIdFromTiktokApi -> Unable to fetch roomID, contact the developer");
|
||||||
}
|
}
|
||||||
if (message.equals("user_not_found")) {
|
if (message.equals("user_not_found")) {
|
||||||
|
return new LiveUserData.Response(json, LiveUserData.UserStatus.NotFound, "", -1);
|
||||||
|
}
|
||||||
|
//live -> status 2
|
||||||
|
//live paused -> 3
|
||||||
|
//not live -> status 4
|
||||||
|
var element = jsonObject.get("data");
|
||||||
|
if (element.isJsonNull()) {
|
||||||
|
return new LiveUserData.Response(json, LiveUserData.UserStatus.NotFound, "", -1);
|
||||||
|
}
|
||||||
|
var data = element.getAsJsonObject();
|
||||||
|
var user = data.getAsJsonObject("user");
|
||||||
|
var roomId = user.get("roomId").getAsString();
|
||||||
|
var status = user.get("status").getAsInt();
|
||||||
|
|
||||||
|
var liveRoom = data.getAsJsonObject("liveRoom");
|
||||||
|
long startTime = liveRoom.get("startTime").getAsLong();
|
||||||
|
|
||||||
|
var statusEnum = switch (status) {
|
||||||
|
case 2 -> LiveUserData.UserStatus.Live;
|
||||||
|
case 3 -> LiveUserData.UserStatus.LivePaused;
|
||||||
|
case 4 -> LiveUserData.UserStatus.Offline;
|
||||||
|
default -> LiveUserData.UserStatus.NotFound;
|
||||||
|
};
|
||||||
|
|
||||||
|
return new LiveUserData.Response(json, statusEnum, roomId, startTime);
|
||||||
|
} catch (JsonSyntaxException | IllegalStateException e) {
|
||||||
|
logger.warning("Malformed Json: '"+json+"' - Error Message: "+e.getMessage());
|
||||||
return new LiveUserData.Response(json, LiveUserData.UserStatus.NotFound, "", -1);
|
return new LiveUserData.Response(json, LiveUserData.UserStatus.NotFound, "", -1);
|
||||||
}
|
}
|
||||||
//live -> status 2
|
|
||||||
//live paused -> 3
|
|
||||||
//not live -> status 4
|
|
||||||
var element = jsonObject.get("data");
|
|
||||||
if (element.isJsonNull()) {
|
|
||||||
return new LiveUserData.Response(json, LiveUserData.UserStatus.NotFound, "", -1);
|
|
||||||
}
|
|
||||||
var data = element.getAsJsonObject();
|
|
||||||
var user = data.getAsJsonObject("user");
|
|
||||||
var roomId = user.get("roomId").getAsString();
|
|
||||||
var status = user.get("status").getAsInt();
|
|
||||||
|
|
||||||
var liveRoom = data.getAsJsonObject("liveRoom");
|
|
||||||
long startTime = liveRoom.get("startTime").getAsLong();
|
|
||||||
|
|
||||||
var statusEnum = switch (status) {
|
|
||||||
case 2 -> LiveUserData.UserStatus.Live;
|
|
||||||
case 3 -> LiveUserData.UserStatus.LivePaused;
|
|
||||||
case 4 -> LiveUserData.UserStatus.Offline;
|
|
||||||
default -> LiveUserData.UserStatus.NotFound;
|
|
||||||
};
|
|
||||||
|
|
||||||
return new LiveUserData.Response(json, statusEnum, roomId, startTime);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,7 +82,7 @@ public class TikTokWebSocketClient implements SocketClient {
|
|||||||
private void connectDefault() {
|
private void connectDefault() {
|
||||||
try {
|
try {
|
||||||
webSocketClient.connect();
|
webSocketClient.connect();
|
||||||
pingingTask.run(webSocketClient);
|
pingingTask.run(webSocketClient, clientSettings.getPingInterval());
|
||||||
isConnected = true;
|
isConnected = true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
isConnected = false;
|
isConnected = false;
|
||||||
@@ -112,7 +112,7 @@ public class TikTokWebSocketClient implements SocketClient {
|
|||||||
proxySettings.remove();
|
proxySettings.remove();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
pingingTask.run(webSocketClient);
|
pingingTask.run(webSocketClient, clientSettings.getPingInterval());
|
||||||
isConnected = true;
|
isConnected = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,13 +8,13 @@ public class TikTokWebSocketPingingTask
|
|||||||
{
|
{
|
||||||
private Thread thread;
|
private Thread thread;
|
||||||
private boolean isRunning = false;
|
private boolean isRunning = false;
|
||||||
private final int MIN_TIMEOUT = 250;
|
private final int MAX_TIMEOUT = 250;
|
||||||
private final int MAX_TIMEOUT = 500;
|
private final int SLEEP_TIME = 500;
|
||||||
|
|
||||||
public void run(WebSocket webSocket)
|
public void run(WebSocket webSocket, long pingTaskTime)
|
||||||
{
|
{
|
||||||
stop();
|
stop();
|
||||||
thread = new Thread(() -> pingTask(webSocket));
|
thread = new Thread(() -> pingTask(webSocket, pingTaskTime));
|
||||||
isRunning = true;
|
isRunning = true;
|
||||||
thread.start();
|
thread.start();
|
||||||
}
|
}
|
||||||
@@ -26,20 +26,18 @@ public class TikTokWebSocketPingingTask
|
|||||||
isRunning = false;
|
isRunning = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void pingTask(WebSocket webSocket, long pingTaskTime)
|
||||||
private void pingTask(WebSocket webSocket)
|
|
||||||
{
|
{
|
||||||
var random = new Random();
|
var random = new Random();
|
||||||
while (isRunning) {
|
while (isRunning) {
|
||||||
try {
|
try {
|
||||||
if (!webSocket.isOpen()) {
|
if (!webSocket.isOpen()) {
|
||||||
Thread.sleep(100);
|
Thread.sleep(SLEEP_TIME);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
webSocket.sendPing();
|
webSocket.sendPing();
|
||||||
|
|
||||||
var timeout = random.nextInt(MAX_TIMEOUT)+MIN_TIMEOUT;
|
Thread.sleep(pingTaskTime+random.nextInt(MAX_TIMEOUT));
|
||||||
Thread.sleep(timeout);
|
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
isRunning = false;
|
isRunning = false;
|
||||||
|
|||||||
@@ -41,7 +41,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>TikTokLiveJava</artifactId>
|
<artifactId>TikTokLiveJava</artifactId>
|
||||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||||
<version>1.3.0-Release</version>
|
<version>1.5.1-Release</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
@@ -75,7 +75,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||||
<artifactId>extension-collector</artifactId>
|
<artifactId>extension-collector</artifactId>
|
||||||
<version>1.3.0-Release</version>
|
<version>1.5.1-Release</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@@ -25,24 +25,18 @@ package io.github.jwdeveloper.tiktok;
|
|||||||
|
|
||||||
import io.github.jwdeveloper.tiktok.extension.collector.TikTokLiveCollector;
|
import io.github.jwdeveloper.tiktok.extension.collector.TikTokLiveCollector;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class CollectorExample {
|
public class CollectorExample {
|
||||||
|
|
||||||
|
|
||||||
private static String mongoUser;
|
|
||||||
|
|
||||||
private static String mongoPassword;
|
|
||||||
|
|
||||||
private static String mongoDatabase;
|
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
|
|
||||||
var collector = TikTokLiveCollector.useMongo(settings ->
|
var path = "C:\\Users\\ja\\IdeaProjects\\TikTokLiveJava\\Examples\\src\\main\\resources";
|
||||||
|
var collector = TikTokLiveCollector.useFile(settings ->
|
||||||
{
|
{
|
||||||
settings.setConnectionUrl("mongodb+srv://" + mongoUser + ":" + mongoPassword + "@" + mongoDatabase + "/?retryWrites=true&w=majority");
|
settings.setParentFile(new File(path));
|
||||||
});
|
});
|
||||||
collector.connect();
|
collector.connect();
|
||||||
|
|
||||||
@@ -58,14 +52,7 @@ public class CollectorExample {
|
|||||||
{
|
{
|
||||||
event.getException().printStackTrace();
|
event.getException().printStackTrace();
|
||||||
})
|
})
|
||||||
.addListener(collector.newListener(additionalDataFields, document ->
|
.addListener(collector.newListener(additionalDataFields))
|
||||||
{
|
|
||||||
//filtering document data before it is inserted to database
|
|
||||||
if (document.get("dataType") == "message") {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}))
|
|
||||||
.buildAndConnectAsync();
|
.buildAndConnectAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ Maven
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.jwdeveloper.TikTok-Live-Java</groupId>
|
<groupId>com.github.jwdeveloper.TikTok-Live-Java</groupId>
|
||||||
<artifactId>Client</artifactId>
|
<artifactId>Client</artifactId>
|
||||||
<version>1.3.0-Release</version>
|
<version>1.5.0-Release</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
@@ -87,7 +87,7 @@ dependencyResolutionManagement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.github.jwdeveloper.TikTok-Live-Java:Client:1.1.0-Release'
|
implementation 'com.github.jwdeveloper.TikTok-Live-Java:Client:1.5.0-Release'
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>TikTokLiveJava</artifactId>
|
<artifactId>TikTokLiveJava</artifactId>
|
||||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||||
<version>1.3.0-Release</version>
|
<version>1.5.1-Release</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||||
<artifactId>TikTokLiveJava</artifactId>
|
<artifactId>TikTokLiveJava</artifactId>
|
||||||
<version>1.3.0-Release</version>
|
<version>1.5.1-Release</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||||
<artifactId>API</artifactId>
|
<artifactId>API</artifactId>
|
||||||
<version>1.3.0-Release</version>
|
<version>1.5.1-Release</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package io.github.jwdeveloper.tiktok.extension.collector.api;
|
||||||
|
|
||||||
|
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
||||||
|
import org.bson.Document;
|
||||||
|
|
||||||
|
public interface CollectorEvent {
|
||||||
|
boolean execute(LiveClient client, Document document);
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package io.github.jwdeveloper.tiktok.extension.collector.api.settings;
|
package io.github.jwdeveloper.tiktok.extension.collector.api.settings;
|
||||||
|
|
||||||
|
import io.github.jwdeveloper.tiktok.extension.collector.api.CollectorEvent;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
@@ -9,5 +10,5 @@ import java.util.function.Function;
|
|||||||
@Data
|
@Data
|
||||||
public class CollectorListenerSettings {
|
public class CollectorListenerSettings {
|
||||||
private Map<String, Object> extraFields;
|
private Map<String, Object> extraFields;
|
||||||
private Function<Document, Boolean> filter;
|
private CollectorEvent filter;
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,6 @@ import java.util.function.Consumer;
|
|||||||
@Data
|
@Data
|
||||||
public class MongoDataCollectorSettings {
|
public class MongoDataCollectorSettings {
|
||||||
|
|
||||||
@Setter
|
|
||||||
private String connectionUrl;
|
private String connectionUrl;
|
||||||
|
|
||||||
private String databaseName = "tiktok";
|
private String databaseName = "tiktok";
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
package io.github.jwdeveloper.tiktok.extension.collector.impl;
|
package io.github.jwdeveloper.tiktok.extension.collector.impl;
|
||||||
|
|
||||||
|
import io.github.jwdeveloper.tiktok.extension.collector.api.CollectorEvent;
|
||||||
import io.github.jwdeveloper.tiktok.extension.collector.api.Storage;
|
import io.github.jwdeveloper.tiktok.extension.collector.api.Storage;
|
||||||
import io.github.jwdeveloper.tiktok.extension.collector.api.settings.CollectorListenerSettings;
|
import io.github.jwdeveloper.tiktok.extension.collector.api.settings.CollectorListenerSettings;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
@@ -40,6 +41,7 @@ public class DataCollector {
|
|||||||
public void connect() {
|
public void connect() {
|
||||||
storage.connect();
|
storage.connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disconnect() {
|
public void disconnect() {
|
||||||
storage.disconnect();
|
storage.disconnect();
|
||||||
}
|
}
|
||||||
@@ -49,11 +51,11 @@ public class DataCollector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public DataCollectorListener newListener(Map<String, Object> additionalFields) {
|
public DataCollectorListener newListener(Map<String, Object> additionalFields) {
|
||||||
return newListener(additionalFields, (e) -> true);
|
return newListener(additionalFields, (live, document) -> true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DataCollectorListener newListener(Map<String, Object> additionalFields,
|
public DataCollectorListener newListener(Map<String, Object> additionalFields,
|
||||||
Function<Document, Boolean> filter) {
|
CollectorEvent filter) {
|
||||||
var settings = new CollectorListenerSettings();
|
var settings = new CollectorListenerSettings();
|
||||||
settings.setExtraFields(additionalFields);
|
settings.setExtraFields(additionalFields);
|
||||||
settings.setFilter(filter);
|
settings.setFilter(filter);
|
||||||
|
|||||||
@@ -17,13 +17,13 @@ import org.bson.Document;
|
|||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.UUID;
|
import java.util.Date;
|
||||||
|
|
||||||
public class DataCollectorListener implements LiveDataCollector {
|
public class DataCollectorListener implements LiveDataCollector {
|
||||||
|
|
||||||
private final Storage storage;
|
private final Storage storage;
|
||||||
private final CollectorListenerSettings settings;
|
private final CollectorListenerSettings settings;
|
||||||
private String sessionId;
|
private String roomId;
|
||||||
private String userName;
|
private String userName;
|
||||||
|
|
||||||
public DataCollectorListener(Storage collection, CollectorListenerSettings settings) {
|
public DataCollectorListener(Storage collection, CollectorListenerSettings settings) {
|
||||||
@@ -41,44 +41,42 @@ public class DataCollectorListener implements LiveDataCollector {
|
|||||||
@TikTokEventObserver
|
@TikTokEventObserver
|
||||||
private void onEvent(LiveClient liveClient, TikTokEvent event) {
|
private void onEvent(LiveClient liveClient, TikTokEvent event) {
|
||||||
if (event instanceof TikTokConnectingEvent) {
|
if (event instanceof TikTokConnectingEvent) {
|
||||||
sessionId = UUID.randomUUID().toString();
|
|
||||||
userName = liveClient.getRoomInfo().getHostName();
|
userName = liveClient.getRoomInfo().getHostName();
|
||||||
|
roomId = liveClient.getRoomInfo().getRoomId();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event instanceof TikTokErrorEvent) {
|
if (event instanceof TikTokErrorEvent) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
includeEvent(event);
|
includeEvent(liveClient, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
@TikTokEventObserver
|
@TikTokEventObserver
|
||||||
private void onError(LiveClient liveClient, TikTokErrorEvent event) {
|
private void onError(LiveClient liveClient, TikTokErrorEvent event) {
|
||||||
event.getException().printStackTrace();
|
event.getException().printStackTrace();
|
||||||
includeError(event);
|
includeError(liveClient, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void includeResponse(LiveClient liveClient, WebcastResponse message) {
|
private void includeResponse(LiveClient liveClient, WebcastResponse message) {
|
||||||
var messageContent = Base64.getEncoder().encodeToString(message.toByteArray());
|
var messageContent = Base64.getEncoder().encodeToString(message.toByteArray());
|
||||||
insertDocument(createDocument("response", "webcast", messageContent));
|
insertDocument(liveClient, createDocument("response", "webcast", messageContent));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void includeMessage(LiveClient liveClient, WebcastResponse.Message message) {
|
private void includeMessage(LiveClient liveClient, WebcastResponse.Message message) {
|
||||||
var method = message.getMethod();
|
var method = message.getMethod();
|
||||||
var messageContent = Base64.getEncoder().encodeToString(message.getPayload().toByteArray());
|
var messageContent = Base64.getEncoder().encodeToString(message.getPayload().toByteArray());
|
||||||
|
insertDocument(liveClient, createDocument("message", method, messageContent));
|
||||||
insertDocument(createDocument("message", method, messageContent));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void includeEvent(TikTokEvent event) {
|
private void includeEvent(LiveClient client, TikTokEvent event) {
|
||||||
var json = JsonUtil.toJson(event);
|
var json = JsonUtil.toJson(event);
|
||||||
var content = Base64.getEncoder().encodeToString(json.getBytes());
|
var content = Base64.getEncoder().encodeToString(json.getBytes());
|
||||||
var name = event.getClass().getSimpleName();
|
var name = event.getClass().getSimpleName();
|
||||||
insertDocument(createDocument("event", name, content));
|
insertDocument(client, createDocument("event", name, content));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void includeError(TikTokErrorEvent event) {
|
private void includeError(LiveClient client, TikTokErrorEvent event) {
|
||||||
var exception = event.getException();
|
var exception = event.getException();
|
||||||
var exceptionName = event.getException().getClass().getSimpleName();
|
var exceptionName = event.getException().getClass().getSimpleName();
|
||||||
|
|
||||||
@@ -86,18 +84,18 @@ public class DataCollectorListener implements LiveDataCollector {
|
|||||||
var pw = new PrintWriter(sw);
|
var pw = new PrintWriter(sw);
|
||||||
event.getException().printStackTrace(pw);
|
event.getException().printStackTrace(pw);
|
||||||
var content = sw.toString();
|
var content = sw.toString();
|
||||||
|
var contentBase64 = Base64.getEncoder().encodeToString(content.getBytes());
|
||||||
var doc = createDocument("error", exceptionName, content);
|
var doc = createDocument("error", exceptionName, contentBase64);
|
||||||
if (exception instanceof TikTokLiveMessageException ex) {
|
if (exception instanceof TikTokLiveMessageException ex) {
|
||||||
doc.append("message", ex.messageToBase64())
|
doc.append("message", ex.messageToBase64())
|
||||||
.append("response", ex.webcastResponseToBase64());
|
.append("response", ex.webcastResponseToBase64());
|
||||||
}
|
}
|
||||||
insertDocument(doc);
|
insertDocument(client, doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void insertDocument(Document document) {
|
private void insertDocument(LiveClient client, Document document) {
|
||||||
if (!settings.getFilter().apply(document)) {
|
if (!settings.getFilter().execute(client, document)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
storage.insert(document);
|
storage.insert(document);
|
||||||
@@ -106,7 +104,7 @@ public class DataCollectorListener implements LiveDataCollector {
|
|||||||
|
|
||||||
private Document createDocument(String dataType, String dataTypeName, String content) {
|
private Document createDocument(String dataType, String dataTypeName, String content) {
|
||||||
var doc = new Document();
|
var doc = new Document();
|
||||||
doc.append("session", sessionId);
|
doc.append("roomId", roomId);
|
||||||
for (var entry : settings.getExtraFields().entrySet()) {
|
for (var entry : settings.getExtraFields().entrySet()) {
|
||||||
doc.append(entry.getKey(), entry.getValue());
|
doc.append(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
@@ -114,6 +112,7 @@ public class DataCollectorListener implements LiveDataCollector {
|
|||||||
doc.append("dataType", dataType);
|
doc.append("dataType", dataType);
|
||||||
doc.append("dataTypeName", dataTypeName);
|
doc.append("dataTypeName", dataTypeName);
|
||||||
doc.append("content", content);
|
doc.append("content", content);
|
||||||
|
doc.append("createdAt", new Date());
|
||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,7 @@ package io.github.jwdeveloper.tiktok.extension.collector.impl.storages;
|
|||||||
import io.github.jwdeveloper.tiktok.extension.collector.api.Storage;
|
import io.github.jwdeveloper.tiktok.extension.collector.api.Storage;
|
||||||
import io.github.jwdeveloper.tiktok.extension.collector.api.settings.FileDataCollectorSettings;
|
import io.github.jwdeveloper.tiktok.extension.collector.api.settings.FileDataCollectorSettings;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
import org.bson.json.JsonWriterSettings;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -29,11 +30,11 @@ public class FileStorage implements Storage {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void insert(Document document) {
|
public void insert(Document document) {
|
||||||
var fileName = document.get("dataType") + ":" + document.get("dataTypeName") + ".json";
|
var fileName = document.get("dataType") + "_" + document.get("dataTypeName") + ".json";
|
||||||
try {
|
try {
|
||||||
var file = new File(settings.getParentFile(), fileName);
|
var file = new File(settings.getParentFile(), fileName);
|
||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
Files.writeString(file.toPath(), document.toJson(), StandardOpenOption.APPEND);
|
Files.writeString(file.toPath(), document.toJson(JsonWriterSettings.builder().indent(true).build()), StandardOpenOption.APPEND);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>TikTokLiveJava</artifactId>
|
<artifactId>TikTokLiveJava</artifactId>
|
||||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||||
<version>1.3.0-Release</version>
|
<version>1.5.1-Release</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>extension-recorder</artifactId>
|
<artifactId>extension-recorder</artifactId>
|
||||||
|
|||||||
2
pom.xml
2
pom.xml
@@ -7,7 +7,7 @@
|
|||||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||||
<artifactId>TikTokLiveJava</artifactId>
|
<artifactId>TikTokLiveJava</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<version>1.3.0-Release</version>
|
<version>1.5.1-Release</version>
|
||||||
<modules>
|
<modules>
|
||||||
<module>API</module>
|
<module>API</module>
|
||||||
<module>Client</module>
|
<module>Client</module>
|
||||||
|
|||||||
Reference in New Issue
Block a user