mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-28 09:19:40 -05:00
Compare commits
25 Commits
1.8.3-Rele
...
develop-1.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
358954984b | ||
|
|
acb13ea58a | ||
|
|
e8129862d1 | ||
|
|
88e36e7e90 | ||
|
|
eef9d43d01 | ||
|
|
75f3896a86 | ||
|
|
e679ff23b7 | ||
|
|
e6211e69c5 | ||
|
|
1b3ee24aea | ||
|
|
c9309ee2cf | ||
|
|
a28df44cee | ||
|
|
ee866762a3 | ||
|
|
3cb7c83ef4 | ||
|
|
bf755bcb89 | ||
|
|
20bb05e527 | ||
|
|
d5a3213ad8 | ||
|
|
7e496373ef | ||
|
|
1f3c1c7d41 | ||
|
|
c160259863 | ||
|
|
fb458e7e7d | ||
|
|
5f5ada312a | ||
|
|
cb20c3dd3a | ||
|
|
bf42f65b3d | ||
|
|
05e18ef8e0 | ||
|
|
0f6ee58d7f |
@@ -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.8.2-Release</version>
|
<version>1.8.7-Release</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>API</artifactId>
|
<artifactId>API</artifactId>
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
package io.github.jwdeveloper.tiktok.data.requests;
|
package io.github.jwdeveloper.tiktok.data.requests;
|
||||||
|
|
||||||
|
import io.github.jwdeveloper.tiktok.data.models.users.User;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
|
||||||
public class LiveUserData {
|
public class LiveUserData {
|
||||||
@@ -44,6 +45,7 @@ public class LiveUserData {
|
|||||||
private final UserStatus userStatus;
|
private final UserStatus userStatus;
|
||||||
private final String roomId;
|
private final String roomId;
|
||||||
private final long startTime;
|
private final long startTime;
|
||||||
|
private final User user;
|
||||||
|
|
||||||
public boolean isLiveOnline() {
|
public boolean isLiveOnline() {
|
||||||
return userStatus == LiveUserData.UserStatus.Live || userStatus == LiveUserData.UserStatus.LivePaused;
|
return userStatus == LiveUserData.UserStatus.Live || userStatus == LiveUserData.UserStatus.LivePaused;
|
||||||
|
|||||||
@@ -82,9 +82,9 @@ public class LiveClientSettings {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Interval of time in milliseconds between pings to TikTok
|
* Interval of time in milliseconds between pings to TikTok
|
||||||
* @apiNote Min: 250 (0.25 seconds), Default: 5000 (5 seconds)
|
* @apiNote Min: 250 (0.25 seconds), Default: 10000 (10 seconds - TikTok Default)
|
||||||
*/
|
*/
|
||||||
private long pingInterval = 5000;
|
private long pingInterval = 10000;
|
||||||
|
|
||||||
/** Throw an exception on 18+ Age Restriction */
|
/** Throw an exception on 18+ Age Restriction */
|
||||||
private boolean throwOnAgeRestriction;
|
private boolean throwOnAgeRestriction;
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ import io.github.jwdeveloper.tiktok.live.LiveClient;
|
|||||||
* <p>- 2 parameters of (LiveClient, Class extending TikTokEvent)
|
* <p>- 2 parameters of (LiveClient, Class extending TikTokEvent)
|
||||||
* <pre>
|
* <pre>
|
||||||
* {@code
|
* {@code
|
||||||
* public static class CustomListener implements TikTokEventListener
|
* public static class CustomListener
|
||||||
* {
|
* {
|
||||||
* @TikTokEventObserver
|
* @TikTokEventObserver
|
||||||
* public void onError(LiveClient liveClient, TikTokErrorEvent event)
|
* public void onError(LiveClient liveClient, TikTokErrorEvent event)
|
||||||
@@ -67,9 +67,7 @@ import io.github.jwdeveloper.tiktok.live.LiveClient;
|
|||||||
* }
|
* }
|
||||||
* </pre>
|
* </pre>
|
||||||
*/
|
*/
|
||||||
//TODO I think this interface can be removed, since we are using,
|
@Deprecated(forRemoval = true, since = "1.8.1 (This interface is not longer needed, please remove it from your class) | Removing in 1.9.0")
|
||||||
//annotation @TikTokEventHandler to check methods that are events
|
|
||||||
@Deprecated(forRemoval = true, since = "1.8.1 (This interface is not longer needed, please remove it from your class)")
|
|
||||||
public interface TikTokEventListener {
|
public interface TikTokEventListener {
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -35,7 +35,7 @@ import io.github.jwdeveloper.tiktok.data.events.social.*;
|
|||||||
import io.github.jwdeveloper.tiktok.data.events.websocket.*;
|
import io.github.jwdeveloper.tiktok.data.events.websocket.*;
|
||||||
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
||||||
|
|
||||||
public abstract class TikTokEventListenerBase implements TikTokEventListener
|
public abstract class TikTokEventListenerBase
|
||||||
{
|
{
|
||||||
public void onUnhandledSocial(LiveClient client, TikTokUnhandledSocialEvent event) {}
|
public void onUnhandledSocial(LiveClient client, TikTokUnhandledSocialEvent event) {}
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ package io.github.jwdeveloper.tiktok.live;
|
|||||||
|
|
||||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||||
import io.github.jwdeveloper.tiktok.listener.ListenersManager;
|
import io.github.jwdeveloper.tiktok.listener.ListenersManager;
|
||||||
import io.github.jwdeveloper.tiktok.listener.TikTokEventListener;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
@@ -81,9 +80,7 @@ public interface LiveClient {
|
|||||||
LiveRoomInfo getRoomInfo();
|
LiveRoomInfo getRoomInfo();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manage TikTokEventListener
|
* Manage TikTokEvent Listeners
|
||||||
*
|
|
||||||
* @see TikTokEventListener
|
|
||||||
*/
|
*/
|
||||||
ListenersManager getListenersManager();
|
ListenersManager getListenersManager();
|
||||||
|
|
||||||
|
|||||||
@@ -57,8 +57,7 @@ public interface LiveClientBuilder extends EventsBuilder<LiveClientBuilder> {
|
|||||||
LiveClientBuilder configure(Consumer<LiveClientSettings> onConfigure);
|
LiveClientBuilder configure(Consumer<LiveClientSettings> onConfigure);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adding events listener class, its fancy way to register events without using lamda method
|
* Adds events listener class, its fancy way to register events without using lamda method
|
||||||
* but actual method in class that implements TikTokEventListener
|
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -155,8 +155,9 @@ enum MemberMessageAction {
|
|||||||
enum ControlAction {
|
enum ControlAction {
|
||||||
ControlActionUNKNOWN = 0;
|
ControlActionUNKNOWN = 0;
|
||||||
STREAM_PAUSED = 1; // Stream Paused by Host
|
STREAM_PAUSED = 1; // Stream Paused by Host
|
||||||
STREAM_UNPAUSED = 2;
|
STREAM_UNPAUSED = 2; // Stream Unpaused by Host
|
||||||
STREAM_ENDED = 3; // Stream Ended by Host
|
STREAM_ENDED = 3; // Stream Ended by Host
|
||||||
|
STREAM_SUSPENDED = 4; // Stream Ended by TikTok
|
||||||
}
|
}
|
||||||
|
|
||||||
enum LinkLayerMessageType
|
enum LinkLayerMessageType
|
||||||
|
|||||||
@@ -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.8.2-Release</version>
|
<version>1.8.7-Release</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
|
|||||||
|
|
||||||
//networking
|
//networking
|
||||||
dependance.registerSingleton(HttpClientFactory.class);
|
dependance.registerSingleton(HttpClientFactory.class);
|
||||||
dependance.registerSingleton(TikTokWebSocketPingingTask.class);
|
dependance.registerSingleton(WebSocketHeartbeatTask.class);
|
||||||
if (clientSettings.isOffline()) {
|
if (clientSettings.isOffline()) {
|
||||||
dependance.registerSingleton(LiveSocketClient.class, TikTokWebSocketOfflineClient.class);
|
dependance.registerSingleton(LiveSocketClient.class, TikTokWebSocketOfflineClient.class);
|
||||||
dependance.registerSingleton(LiveHttpClient.class, TikTokLiveHttpOfflineClient.class);
|
dependance.registerSingleton(LiveHttpClient.class, TikTokLiveHttpOfflineClient.class);
|
||||||
|
|||||||
@@ -107,6 +107,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
|
|||||||
return getGiftsData();
|
return getGiftsData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated(since = "1.8.6", forRemoval = true)
|
||||||
public GiftsData.Response getGiftsData() {
|
public GiftsData.Response getGiftsData() {
|
||||||
var result = httpFactory.client(TIKTOK_GIFTS_URL)
|
var result = httpFactory.client(TIKTOK_GIFTS_URL)
|
||||||
.build()
|
.build()
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ public class TikTokLiveHttpOfflineClient implements LiveHttpClient {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LiveUserData.Response fetchLiveUserData(LiveUserData.Request request) {
|
public LiveUserData.Response fetchLiveUserData(LiveUserData.Request request) {
|
||||||
return new LiveUserData.Response("", LiveUserData.UserStatus.Live, "offline_room_id", 0);
|
return new LiveUserData.Response("", LiveUserData.UserStatus.Live, "offline_room_id", 0, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import io.github.jwdeveloper.tiktok.data.models.Picture;
|
|||||||
import io.github.jwdeveloper.tiktok.data.models.gifts.Gift;
|
import io.github.jwdeveloper.tiktok.data.models.gifts.Gift;
|
||||||
import io.github.jwdeveloper.tiktok.data.requests.GiftsData;
|
import io.github.jwdeveloper.tiktok.data.requests.GiftsData;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class GiftsDataMapper {
|
public class GiftsDataMapper {
|
||||||
@@ -56,9 +57,13 @@ public class GiftsDataMapper {
|
|||||||
var parsedJson = JsonParser.parseString(json);
|
var parsedJson = JsonParser.parseString(json);
|
||||||
var jsonObject = parsedJson.getAsJsonObject();
|
var jsonObject = parsedJson.getAsJsonObject();
|
||||||
if (jsonObject.get("data") instanceof JsonObject data && data.get("gifts") instanceof JsonArray giftArray) {
|
if (jsonObject.get("data") instanceof JsonObject data && data.get("gifts") instanceof JsonArray giftArray) {
|
||||||
var gifts = giftArray.asList().parallelStream()
|
var gifts = new ArrayList<Gift>();
|
||||||
.map(this::mapSingleRoomGift)
|
|
||||||
.toList();
|
for(int i = 0; i < giftArray.size(); i++) {
|
||||||
|
JsonElement element = giftArray.get(i);
|
||||||
|
Gift gift = mapSingleRoomGift(element);
|
||||||
|
gifts.add(gift);
|
||||||
|
}
|
||||||
|
|
||||||
return new GiftsData.Response(json, gifts);
|
return new GiftsData.Response(json, gifts);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ public class LiveDataMapper {
|
|||||||
var link = pictureElement.getAsJsonArray("url_list").get(1).getAsString();
|
var link = pictureElement.getAsJsonArray("url_list").get(1).getAsString();
|
||||||
var picture = new Picture(link);
|
var picture = new Picture(link);
|
||||||
|
|
||||||
var user = new User(id, name, profileName, picture, followers, followingCount, new ArrayList<>());
|
var user = new User(id, name, profileName, picture, followingCount, followers, new ArrayList<>());
|
||||||
user.addAttribute(UserAttribute.LiveHost);
|
user.addAttribute(UserAttribute.LiveHost);
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,9 +23,12 @@
|
|||||||
package io.github.jwdeveloper.tiktok.http.mappers;
|
package io.github.jwdeveloper.tiktok.http.mappers;
|
||||||
|
|
||||||
import com.google.gson.*;
|
import com.google.gson.*;
|
||||||
|
import io.github.jwdeveloper.tiktok.data.models.Picture;
|
||||||
|
import io.github.jwdeveloper.tiktok.data.models.users.User;
|
||||||
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.List;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class LiveUserDataMapper
|
public class LiveUserDataMapper
|
||||||
@@ -40,17 +43,18 @@ public class LiveUserDataMapper
|
|||||||
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);
|
return new LiveUserData.Response(json, LiveUserData.UserStatus.NotFound, "", -1, null);
|
||||||
}
|
}
|
||||||
//live -> status 2
|
//live -> status 2
|
||||||
//live paused -> 3
|
//live paused -> 3
|
||||||
//not live -> status 4
|
//not live -> status 4
|
||||||
var element = jsonObject.get("data");
|
var element = jsonObject.get("data");
|
||||||
if (element.isJsonNull()) {
|
if (element.isJsonNull()) {
|
||||||
return new LiveUserData.Response(json, LiveUserData.UserStatus.NotFound, "", -1);
|
return new LiveUserData.Response(json, LiveUserData.UserStatus.NotFound, "", -1, null);
|
||||||
}
|
}
|
||||||
var data = element.getAsJsonObject();
|
var data = element.getAsJsonObject();
|
||||||
var user = data.getAsJsonObject("user");
|
var user = data.getAsJsonObject("user");
|
||||||
|
var stats = data.getAsJsonObject("stats");
|
||||||
var roomId = user.get("roomId").getAsString();
|
var roomId = user.get("roomId").getAsString();
|
||||||
var status = user.get("status").getAsInt();
|
var status = user.get("status").getAsInt();
|
||||||
|
|
||||||
@@ -64,10 +68,19 @@ public class LiveUserDataMapper
|
|||||||
default -> LiveUserData.UserStatus.NotFound;
|
default -> LiveUserData.UserStatus.NotFound;
|
||||||
};
|
};
|
||||||
|
|
||||||
return new LiveUserData.Response(json, statusEnum, roomId, startTime);
|
User foundUser = new User(
|
||||||
|
Long.parseLong(user.get("id").getAsString()),
|
||||||
|
user.get("uniqueId").getAsString(),
|
||||||
|
user.get("nickname").getAsString(),
|
||||||
|
new Picture(user.get("avatarLarger").getAsString()),
|
||||||
|
stats.get("followingCount").getAsLong(),
|
||||||
|
stats.get("followerCount").getAsLong(),
|
||||||
|
List.of());
|
||||||
|
|
||||||
|
return new LiveUserData.Response(json, statusEnum, roomId, startTime, foundUser);
|
||||||
} catch (JsonSyntaxException | IllegalStateException e) {
|
} catch (JsonSyntaxException | IllegalStateException e) {
|
||||||
logger.warning("Malformed Json: '"+json+"' - Error Message: "+e.getMessage());
|
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, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -46,7 +46,7 @@ public class TikTokCommonEventHandler
|
|||||||
var message = WebcastControlMessage.parseFrom(msg);
|
var message = WebcastControlMessage.parseFrom(msg);
|
||||||
return switch (message.getAction()) {
|
return switch (message.getAction()) {
|
||||||
case STREAM_PAUSED -> new TikTokLivePausedEvent();
|
case STREAM_PAUSED -> new TikTokLivePausedEvent();
|
||||||
case STREAM_ENDED -> new TikTokLiveEndedEvent();
|
case STREAM_ENDED, STREAM_SUSPENDED -> new TikTokLiveEndedEvent();
|
||||||
case STREAM_UNPAUSED -> new TikTokLiveUnpausedEvent();
|
case STREAM_UNPAUSED -> new TikTokLiveUnpausedEvent();
|
||||||
default -> new TikTokUnhandledControlEvent(message);
|
default -> new TikTokUnhandledControlEvent(message);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ public class TikTokRoomInfoEventHandler {
|
|||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public TikTokEvent handleUserRanking(byte[] msg) {
|
public TikTokEvent handleUserRanking(byte[] msg) {
|
||||||
var message = WebcastRoomUserSeqMessage.parseFrom(msg);
|
var message = WebcastRoomUserSeqMessage.parseFrom(msg);
|
||||||
|
var currentViewers = (int) message.getTotal();
|
||||||
var totalUsers = message.getTotalUser();
|
var totalUsers = message.getTotalUser();
|
||||||
var userRanking = message.getRanksListList().stream().map(RankingUser::new)
|
var userRanking = message.getRanksListList().stream().map(RankingUser::new)
|
||||||
.sorted((ru1, ru2) -> Integer.compare(ru2.getScore(), ru1.getScore()))
|
.sorted((ru1, ru2) -> Integer.compare(ru2.getScore(), ru1.getScore()))
|
||||||
@@ -65,6 +66,7 @@ public class TikTokRoomInfoEventHandler {
|
|||||||
|
|
||||||
return handleRoomInfo(tikTokRoomInfo ->
|
return handleRoomInfo(tikTokRoomInfo ->
|
||||||
{
|
{
|
||||||
|
tikTokRoomInfo.setViewersCount(currentViewers);
|
||||||
tikTokRoomInfo.setTotalViewersCount(totalUsers);
|
tikTokRoomInfo.setTotalViewersCount(totalUsers);
|
||||||
tikTokRoomInfo.updateRanking(userRanking);
|
tikTokRoomInfo.updateRanking(userRanking);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ public class TikTokWebSocketClient implements LiveSocketClient {
|
|||||||
private final LiveClientSettings clientSettings;
|
private final LiveClientSettings clientSettings;
|
||||||
private final LiveMessagesHandler messageHandler;
|
private final LiveMessagesHandler messageHandler;
|
||||||
private final LiveEventsHandler tikTokEventHandler;
|
private final LiveEventsHandler tikTokEventHandler;
|
||||||
private final TikTokWebSocketPingingTask pingingTask;
|
private final WebSocketHeartbeatTask heartbeatTask;
|
||||||
private WebSocketClient webSocketClient;
|
private WebSocketClient webSocketClient;
|
||||||
private boolean isConnected;
|
private boolean isConnected;
|
||||||
|
|
||||||
@@ -48,12 +48,12 @@ public class TikTokWebSocketClient implements LiveSocketClient {
|
|||||||
LiveClientSettings clientSettings,
|
LiveClientSettings clientSettings,
|
||||||
LiveMessagesHandler messageHandler,
|
LiveMessagesHandler messageHandler,
|
||||||
LiveEventsHandler tikTokEventHandler,
|
LiveEventsHandler tikTokEventHandler,
|
||||||
TikTokWebSocketPingingTask pingingTask)
|
WebSocketHeartbeatTask heartbeatTask)
|
||||||
{
|
{
|
||||||
this.clientSettings = clientSettings;
|
this.clientSettings = clientSettings;
|
||||||
this.messageHandler = messageHandler;
|
this.messageHandler = messageHandler;
|
||||||
this.tikTokEventHandler = tikTokEventHandler;
|
this.tikTokEventHandler = tikTokEventHandler;
|
||||||
this.pingingTask = pingingTask;
|
this.heartbeatTask = heartbeatTask;
|
||||||
isConnected = false;
|
isConnected = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +84,7 @@ public class TikTokWebSocketClient implements LiveSocketClient {
|
|||||||
private void connectDefault() {
|
private void connectDefault() {
|
||||||
try {
|
try {
|
||||||
webSocketClient.connect();
|
webSocketClient.connect();
|
||||||
pingingTask.run(webSocketClient, clientSettings.getPingInterval());
|
heartbeatTask.run(webSocketClient, clientSettings.getPingInterval());
|
||||||
isConnected = true;
|
isConnected = true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
isConnected = false;
|
isConnected = false;
|
||||||
@@ -120,7 +120,7 @@ public class TikTokWebSocketClient implements LiveSocketClient {
|
|||||||
proxySettings.remove();
|
proxySettings.remove();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
pingingTask.run(webSocketClient, clientSettings.getPingInterval());
|
heartbeatTask.run(webSocketClient, clientSettings.getPingInterval());
|
||||||
isConnected = true;
|
isConnected = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -141,7 +141,7 @@ public class TikTokWebSocketClient implements LiveSocketClient {
|
|||||||
public void stop() {
|
public void stop() {
|
||||||
if (isConnected && webSocketClient != null && webSocketClient.isOpen()) {
|
if (isConnected && webSocketClient != null && webSocketClient.isOpen()) {
|
||||||
webSocketClient.closeConnection(0, "");
|
webSocketClient.closeConnection(0, "");
|
||||||
pingingTask.stop();
|
heartbeatTask.stop();
|
||||||
}
|
}
|
||||||
webSocketClient = null;
|
webSocketClient = null;
|
||||||
isConnected = false;
|
isConnected = false;
|
||||||
|
|||||||
@@ -22,18 +22,19 @@
|
|||||||
*/
|
*/
|
||||||
package io.github.jwdeveloper.tiktok.websocket;
|
package io.github.jwdeveloper.tiktok.websocket;
|
||||||
|
|
||||||
import io.github.jwdeveloper.tiktok.live.LiveEventsHandler;
|
|
||||||
import org.java_websocket.WebSocket;
|
import org.java_websocket.WebSocket;
|
||||||
|
|
||||||
public class TikTokWebSocketPingingTask {
|
public class WebSocketHeartbeatTask
|
||||||
|
{
|
||||||
private Thread thread;
|
private Thread thread;
|
||||||
private boolean isRunning = false;
|
private boolean isRunning = false;
|
||||||
private final int MAX_TIMEOUT = 250;
|
private final int MAX_TIMEOUT = 250;
|
||||||
private final int SLEEP_TIME = 500;
|
private final int SLEEP_TIME = 500;
|
||||||
|
private final byte[] heartbeatBytes = {58, 2, 104, 98}; // Byte Array of "3A026862" which is TikTok's custom heartbeat value
|
||||||
|
|
||||||
public void run(WebSocket webSocket, long pingTaskTime) {
|
public void run(WebSocket webSocket, long pingTaskTime) {
|
||||||
stop();
|
stop();
|
||||||
thread = new Thread(() -> pingTask(webSocket, pingTaskTime), "pinging-task");
|
thread = new Thread(() -> heartbeatTask(webSocket, pingTaskTime), "heartbeat-task");
|
||||||
isRunning = true;
|
isRunning = true;
|
||||||
thread.start();
|
thread.start();
|
||||||
}
|
}
|
||||||
@@ -44,11 +45,11 @@ public class TikTokWebSocketPingingTask {
|
|||||||
isRunning = false;
|
isRunning = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void pingTask(WebSocket webSocket, long pingTaskTime) {
|
private void heartbeatTask(WebSocket webSocket, long pingTaskTime) {
|
||||||
while (isRunning) {
|
while (isRunning) {
|
||||||
try {
|
try {
|
||||||
if (webSocket.isOpen()) {
|
if (webSocket.isOpen()) {
|
||||||
webSocket.sendPing();
|
webSocket.send(heartbeatBytes);
|
||||||
Thread.sleep(pingTaskTime + (int) (Math.random() * MAX_TIMEOUT));
|
Thread.sleep(pingTaskTime + (int) (Math.random() * MAX_TIMEOUT));
|
||||||
} else
|
} else
|
||||||
Thread.sleep(SLEEP_TIME);
|
Thread.sleep(SLEEP_TIME);
|
||||||
@@ -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.8.0-Release</version>
|
<version>1.8.7-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.5.0-Release'
|
implementation 'com.github.jwdeveloper.TikTok-Live-Java:Client:1.8.5-Release'
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -707,7 +707,7 @@ public static void main(String[] args) throws IOException {
|
|||||||
* - second must be class that extending TikTokEvent
|
* - second must be class that extending TikTokEvent
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public static class CustomListener implements TikTokEventListener {
|
public static class CustomListener {
|
||||||
|
|
||||||
@TikTokEventObserver
|
@TikTokEventObserver
|
||||||
public void onLike(LiveClient liveClient, TikTokLikeEvent event) {
|
public void onLike(LiveClient liveClient, TikTokLikeEvent event) {
|
||||||
|
|||||||
@@ -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.8.2-Release</version>
|
<version>1.8.7-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.8.2-Release</version>
|
<version>1.8.7-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.8.2-Release</version>
|
<version>1.8.7-Release</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@@ -22,9 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
package io.github.jwdeveloper.tiktok.extension.collector.api;
|
package io.github.jwdeveloper.tiktok.extension.collector.api;
|
||||||
|
|
||||||
import io.github.jwdeveloper.tiktok.listener.TikTokEventListener;
|
public interface LiveDataCollector
|
||||||
|
|
||||||
public interface LiveDataCollector extends TikTokEventListener
|
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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.8.2-Release</version>
|
<version>1.8.7-Release</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>extension-recorder</artifactId>
|
<artifactId>extension-recorder</artifactId>
|
||||||
|
|||||||
@@ -22,9 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
package io.github.jwdeveloper.tiktok.extension.recorder.api;
|
package io.github.jwdeveloper.tiktok.extension.recorder.api;
|
||||||
|
|
||||||
import io.github.jwdeveloper.tiktok.listener.TikTokEventListener;
|
public interface LiveRecorder {
|
||||||
|
|
||||||
public interface LiveRecorder extends TikTokEventListener {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -30,7 +30,7 @@ import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings;
|
|||||||
import io.github.jwdeveloper.tiktok.extension.recorder.api.LiveRecorder;
|
import io.github.jwdeveloper.tiktok.extension.recorder.api.LiveRecorder;
|
||||||
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.*;
|
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.*;
|
||||||
import io.github.jwdeveloper.tiktok.extension.recorder.impl.enums.LiveQuality;
|
import io.github.jwdeveloper.tiktok.extension.recorder.impl.enums.LiveQuality;
|
||||||
import io.github.jwdeveloper.tiktok.extension.recorder.impl.event.TikTokLiveRecorderStartedEvent;
|
import io.github.jwdeveloper.tiktok.extension.recorder.impl.event.*;
|
||||||
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
||||||
import io.github.jwdeveloper.tiktok.models.ConnectionState;
|
import io.github.jwdeveloper.tiktok.models.ConnectionState;
|
||||||
|
|
||||||
@@ -38,14 +38,17 @@ import java.io.*;
|
|||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.http.*;
|
import java.net.http.*;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
public class RecorderListener implements LiveRecorder {
|
public class RecorderListener implements LiveRecorder {
|
||||||
|
|
||||||
private final BiConsumer<RecorderSettings, LiveClient> consumer;
|
private final BiConsumer<RecorderSettings, LiveClient> consumer;
|
||||||
private final RecorderSettings settings;
|
private final RecorderSettings settings;
|
||||||
|
private final AtomicBoolean token = new AtomicBoolean();
|
||||||
private DownloadData downloadData;
|
private DownloadData downloadData;
|
||||||
private Thread liveDownloadThread;
|
private CompletableFuture<Void> future;
|
||||||
|
|
||||||
public RecorderListener(BiConsumer<RecorderSettings, LiveClient> consumer) {
|
public RecorderListener(BiConsumer<RecorderSettings, LiveClient> consumer) {
|
||||||
this.consumer = consumer;
|
this.consumer = consumer;
|
||||||
@@ -74,7 +77,12 @@ public class RecorderListener implements LiveRecorder {
|
|||||||
if (isConnected() || downloadData.getDownloadLiveUrl().isEmpty())
|
if (isConnected() || downloadData.getDownloadLiveUrl().isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
liveDownloadThread = new Thread(() -> {
|
var recordingStartedEvent = new TikTokLiveRecorderStartedEvent(downloadData, settings);
|
||||||
|
liveClient.publishEvent(recordingStartedEvent);
|
||||||
|
if (recordingStartedEvent.isCanceled())
|
||||||
|
liveClient.getLogger().info("Recording cancelled");
|
||||||
|
else
|
||||||
|
future = CompletableFuture.runAsync(() -> {
|
||||||
try {
|
try {
|
||||||
liveClient.getLogger().info("Recording started "+liveClient.getRoomInfo().getHostName());
|
liveClient.getLogger().info("Recording started "+liveClient.getRoomInfo().getHostName());
|
||||||
|
|
||||||
@@ -94,40 +102,34 @@ public class RecorderListener implements LiveRecorder {
|
|||||||
) {
|
) {
|
||||||
byte[] dataBuffer = new byte[1024];
|
byte[] dataBuffer = new byte[1024];
|
||||||
int bytesRead;
|
int bytesRead;
|
||||||
while ((!settings.isStopOnDisconnect() || liveClient.getRoomInfo().getConnectionState() == ConnectionState.CONNECTED) && (bytesRead = in.read(dataBuffer)) != -1) {
|
while (!token.get() && (!settings.isStopOnDisconnect() || liveClient.getRoomInfo().getConnectionState() == ConnectionState.CONNECTED) && (bytesRead = in.read(dataBuffer)) != -1) {
|
||||||
fos.write(dataBuffer, 0, bytesRead);
|
fos.write(dataBuffer, 0, bytesRead);
|
||||||
fos.flush();
|
fos.flush();
|
||||||
}
|
}
|
||||||
} catch (IOException ignored) {
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
liveClient.getLogger().severe("Stopped recording " + liveClient.getRoomInfo().getHostName());
|
liveClient.getLogger().info("Stopped recording " + liveClient.getRoomInfo().getHostName());
|
||||||
|
liveClient.publishEvent(new TikTokLiveRecorderEndedEvent(settings));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var recordingStartedEvent = new TikTokLiveRecorderStartedEvent(downloadData);
|
|
||||||
liveClient.publishEvent(recordingStartedEvent);
|
|
||||||
if (recordingStartedEvent.isCanceled())
|
|
||||||
liveClient.getLogger().info("Recording cancelled");
|
|
||||||
else
|
|
||||||
liveDownloadThread.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@TikTokEventObserver
|
@TikTokEventObserver
|
||||||
private void onDisconnected(LiveClient liveClient, TikTokDisconnectedEvent event) {
|
private void onDisconnected(LiveClient liveClient, TikTokDisconnectedEvent event) {
|
||||||
if (isConnected() && settings.isStopOnDisconnect())
|
if (isConnected() && settings.isStopOnDisconnect())
|
||||||
liveDownloadThread.interrupt();
|
token.set(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@TikTokEventObserver
|
@TikTokEventObserver
|
||||||
private void onLiveEnded(LiveClient liveClient, TikTokLiveEndedEvent event) {
|
private void onLiveEnded(LiveClient liveClient, TikTokLiveEndedEvent event) {
|
||||||
if (isConnected())
|
if (isConnected())
|
||||||
liveDownloadThread.interrupt();
|
token.set(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private DownloadData mapToDownloadData(String json) {
|
private DownloadData mapToDownloadData(String json) {
|
||||||
try {
|
try {
|
||||||
var parsedJson = JsonParser.parseString(json);
|
var parsedJson = JsonParser.parseString(json);
|
||||||
@@ -164,6 +166,6 @@ public class RecorderListener implements LiveRecorder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isConnected() {
|
private boolean isConnected() {
|
||||||
return liveDownloadThread != null && liveDownloadThread.isAlive();
|
return future != null && !future.isDone();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
* a copy of this software and associated documentation files (the
|
||||||
|
* "Software"), to deal in the Software without restriction, including
|
||||||
|
* without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
* permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
* the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be
|
||||||
|
* included in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package io.github.jwdeveloper.tiktok.extension.recorder.impl.event;
|
||||||
|
|
||||||
|
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||||
|
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.*;
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class TikTokLiveRecorderEndedEvent extends TikTokEvent {
|
||||||
|
|
||||||
|
private final RecorderSettings settings;
|
||||||
|
|
||||||
|
public TikTokLiveRecorderEndedEvent(RecorderSettings settings) {
|
||||||
|
this.settings = settings;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,21 +23,21 @@
|
|||||||
package io.github.jwdeveloper.tiktok.extension.recorder.impl.event;
|
package io.github.jwdeveloper.tiktok.extension.recorder.impl.event;
|
||||||
|
|
||||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||||
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.DownloadData;
|
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.*;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Getter
|
@Getter
|
||||||
public class TikTokLiveRecorderStartedEvent extends TikTokEvent {
|
public class TikTokLiveRecorderStartedEvent extends TikTokEvent {
|
||||||
DownloadData downloadData;
|
private final DownloadData downloadData;
|
||||||
|
private final RecorderSettings settings;
|
||||||
|
|
||||||
@Setter
|
@Setter boolean canceled;
|
||||||
boolean canceled;
|
|
||||||
|
|
||||||
public TikTokLiveRecorderStartedEvent(DownloadData downloadData) {
|
public TikTokLiveRecorderStartedEvent(DownloadData downloadData, RecorderSettings settings) {
|
||||||
this.downloadData = downloadData;
|
this.downloadData = downloadData;
|
||||||
|
this.settings = settings;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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.8.2-Release</version>
|
<version>1.8.7-Release</version>
|
||||||
<modules>
|
<modules>
|
||||||
<module>API</module>
|
<module>API</module>
|
||||||
<module>Client</module>
|
<module>Client</module>
|
||||||
|
|||||||
@@ -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.8.2-Release</version>
|
<version>1.8.7-Release</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user