mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 16:59:39 -05:00
Compare commits
24 Commits
1.8.4-Rele
...
1.8.8-Rele
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a8ac0f6fff | ||
|
|
732eda3d22 | ||
|
|
358954984b | ||
|
|
acb13ea58a | ||
|
|
e8129862d1 | ||
|
|
88e36e7e90 | ||
|
|
eef9d43d01 | ||
|
|
75f3896a86 | ||
|
|
e679ff23b7 | ||
|
|
e6211e69c5 | ||
|
|
1b3ee24aea | ||
|
|
c9309ee2cf | ||
|
|
a28df44cee | ||
|
|
ee866762a3 | ||
|
|
3cb7c83ef4 | ||
|
|
bf755bcb89 | ||
|
|
20bb05e527 | ||
|
|
d5a3213ad8 | ||
|
|
7e496373ef | ||
|
|
1f3c1c7d41 | ||
|
|
c160259863 | ||
|
|
fb458e7e7d | ||
|
|
5f5ada312a | ||
|
|
cb20c3dd3a |
2
.github/workflows/maven-publish.yml
vendored
2
.github/workflows/maven-publish.yml
vendored
@@ -53,7 +53,7 @@ jobs:
|
||||
restore-keys: ${{runner.os}}-m2
|
||||
|
||||
- name: 7 Create Artifacts
|
||||
uses: actions/upload-artifact@v2
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: ${{ steps.version.outputs.version_tag }}
|
||||
path: staging
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>TikTokLiveJava</artifactId>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<version>1.8.3-Release</version>
|
||||
<version>1.8.7-Release</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>API</artifactId>
|
||||
|
||||
@@ -82,9 +82,9 @@ public class LiveClientSettings {
|
||||
|
||||
/**
|
||||
* 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 */
|
||||
private boolean throwOnAgeRestriction;
|
||||
|
||||
@@ -38,9 +38,9 @@ import io.github.jwdeveloper.tiktok.live.LiveClient;
|
||||
* <p>- 2 parameters of (LiveClient, Class extending TikTokEvent)
|
||||
* <pre>
|
||||
* {@code
|
||||
* public static class CustomListener implements TikTokEventListener
|
||||
* public static class CustomListener
|
||||
* {
|
||||
* @TikTokEventObserver
|
||||
* @TikTokEventObserver
|
||||
* public void onError(LiveClient liveClient, TikTokErrorEvent event)
|
||||
* {
|
||||
* System.out.println(event.getException().getMessage());
|
||||
@@ -67,9 +67,7 @@ import io.github.jwdeveloper.tiktok.live.LiveClient;
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
//TODO I think this interface can be removed, since we are using,
|
||||
//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)")
|
||||
@Deprecated(forRemoval = true, since = "1.8.1 (This interface is not longer needed, please remove it from your class) | Removing in 1.9.0")
|
||||
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.live.LiveClient;
|
||||
|
||||
public abstract class TikTokEventListenerBase implements TikTokEventListener
|
||||
public abstract class TikTokEventListenerBase
|
||||
{
|
||||
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.listener.ListenersManager;
|
||||
import io.github.jwdeveloper.tiktok.listener.TikTokEventListener;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
@@ -81,9 +80,7 @@ public interface LiveClient {
|
||||
LiveRoomInfo getRoomInfo();
|
||||
|
||||
/**
|
||||
* Manage TikTokEventListener
|
||||
*
|
||||
* @see TikTokEventListener
|
||||
* Manage TikTokEvent Listeners
|
||||
*/
|
||||
ListenersManager getListenersManager();
|
||||
|
||||
@@ -91,4 +88,4 @@ public interface LiveClient {
|
||||
* Logger
|
||||
*/
|
||||
Logger getLogger();
|
||||
}
|
||||
}
|
||||
@@ -57,8 +57,7 @@ public interface LiveClientBuilder extends EventsBuilder<LiveClientBuilder> {
|
||||
LiveClientBuilder configure(Consumer<LiveClientSettings> onConfigure);
|
||||
|
||||
/**
|
||||
* Adding events listener class, its fancy way to register events without using lamda method
|
||||
* but actual method in class that implements TikTokEventListener
|
||||
* Adds events listener class, its fancy way to register events without using lamda method
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@@ -92,4 +91,4 @@ public interface LiveClientBuilder extends EventsBuilder<LiveClientBuilder> {
|
||||
* @return LiveClient object and connects to TikTok live asynchronously
|
||||
*/
|
||||
CompletableFuture<LiveClient> buildAndConnectAsync();
|
||||
}
|
||||
}
|
||||
@@ -155,8 +155,9 @@ enum MemberMessageAction {
|
||||
enum ControlAction {
|
||||
ControlActionUNKNOWN = 0;
|
||||
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_SUSPENDED = 4; // Stream Ended by TikTok
|
||||
}
|
||||
|
||||
enum LinkLayerMessageType
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>TikTokLiveJava</artifactId>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<version>1.8.3-Release</version>
|
||||
<version>1.8.7-Release</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -138,7 +138,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
|
||||
|
||||
//networking
|
||||
dependance.registerSingleton(HttpClientFactory.class);
|
||||
dependance.registerSingleton(TikTokWebSocketPingingTask.class);
|
||||
dependance.registerSingleton(WebSocketHeartbeatTask.class);
|
||||
if (clientSettings.isOffline()) {
|
||||
dependance.registerSingleton(LiveSocketClient.class, TikTokWebSocketOfflineClient.class);
|
||||
dependance.registerSingleton(LiveHttpClient.class, TikTokLiveHttpOfflineClient.class);
|
||||
|
||||
@@ -107,6 +107,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
|
||||
return getGiftsData();
|
||||
}
|
||||
|
||||
@Deprecated(since = "1.8.6", forRemoval = true)
|
||||
public GiftsData.Response getGiftsData() {
|
||||
var result = httpFactory.client(TIKTOK_GIFTS_URL)
|
||||
.build()
|
||||
|
||||
@@ -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.requests.GiftsData;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GiftsDataMapper {
|
||||
@@ -56,9 +57,13 @@ public class GiftsDataMapper {
|
||||
var parsedJson = JsonParser.parseString(json);
|
||||
var jsonObject = parsedJson.getAsJsonObject();
|
||||
if (jsonObject.get("data") instanceof JsonObject data && data.get("gifts") instanceof JsonArray giftArray) {
|
||||
var gifts = giftArray.asList().parallelStream()
|
||||
.map(this::mapSingleRoomGift)
|
||||
.toList();
|
||||
var gifts = new ArrayList<Gift>();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ public class LiveDataMapper {
|
||||
var link = pictureElement.getAsJsonArray("url_list").get(1).getAsString();
|
||||
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);
|
||||
return user;
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ public class TikTokCommonEventHandler
|
||||
var message = WebcastControlMessage.parseFrom(msg);
|
||||
return switch (message.getAction()) {
|
||||
case STREAM_PAUSED -> new TikTokLivePausedEvent();
|
||||
case STREAM_ENDED -> new TikTokLiveEndedEvent();
|
||||
case STREAM_ENDED, STREAM_SUSPENDED -> new TikTokLiveEndedEvent();
|
||||
case STREAM_UNPAUSED -> new TikTokLiveUnpausedEvent();
|
||||
default -> new TikTokUnhandledControlEvent(message);
|
||||
};
|
||||
@@ -85,4 +85,4 @@ public class TikTokCommonEventHandler
|
||||
return List.of(new TikTokChestEvent(chest, msg));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -58,6 +58,7 @@ public class TikTokRoomInfoEventHandler {
|
||||
@SneakyThrows
|
||||
public TikTokEvent handleUserRanking(byte[] msg) {
|
||||
var message = WebcastRoomUserSeqMessage.parseFrom(msg);
|
||||
var currentViewers = (int) message.getTotal();
|
||||
var totalUsers = message.getTotalUser();
|
||||
var userRanking = message.getRanksListList().stream().map(RankingUser::new)
|
||||
.sorted((ru1, ru2) -> Integer.compare(ru2.getScore(), ru1.getScore()))
|
||||
@@ -65,6 +66,7 @@ public class TikTokRoomInfoEventHandler {
|
||||
|
||||
return handleRoomInfo(tikTokRoomInfo ->
|
||||
{
|
||||
tikTokRoomInfo.setViewersCount(currentViewers);
|
||||
tikTokRoomInfo.setTotalViewersCount(totalUsers);
|
||||
tikTokRoomInfo.updateRanking(userRanking);
|
||||
});
|
||||
@@ -112,4 +114,4 @@ public class TikTokRoomInfoEventHandler {
|
||||
});
|
||||
return MappingResult.of(message, List.of(event, roomInfoEvent));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,7 +40,7 @@ public class TikTokWebSocketClient implements LiveSocketClient {
|
||||
private final LiveClientSettings clientSettings;
|
||||
private final LiveMessagesHandler messageHandler;
|
||||
private final LiveEventsHandler tikTokEventHandler;
|
||||
private final TikTokWebSocketPingingTask pingingTask;
|
||||
private final WebSocketHeartbeatTask heartbeatTask;
|
||||
private WebSocketClient webSocketClient;
|
||||
private boolean isConnected;
|
||||
|
||||
@@ -48,12 +48,12 @@ public class TikTokWebSocketClient implements LiveSocketClient {
|
||||
LiveClientSettings clientSettings,
|
||||
LiveMessagesHandler messageHandler,
|
||||
LiveEventsHandler tikTokEventHandler,
|
||||
TikTokWebSocketPingingTask pingingTask)
|
||||
WebSocketHeartbeatTask heartbeatTask)
|
||||
{
|
||||
this.clientSettings = clientSettings;
|
||||
this.messageHandler = messageHandler;
|
||||
this.tikTokEventHandler = tikTokEventHandler;
|
||||
this.pingingTask = pingingTask;
|
||||
this.heartbeatTask = heartbeatTask;
|
||||
isConnected = false;
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ public class TikTokWebSocketClient implements LiveSocketClient {
|
||||
private void connectDefault() {
|
||||
try {
|
||||
webSocketClient.connect();
|
||||
pingingTask.run(webSocketClient, clientSettings.getPingInterval());
|
||||
heartbeatTask.run(webSocketClient, clientSettings.getPingInterval());
|
||||
isConnected = true;
|
||||
} catch (Exception e) {
|
||||
isConnected = false;
|
||||
@@ -120,7 +120,7 @@ public class TikTokWebSocketClient implements LiveSocketClient {
|
||||
proxySettings.remove();
|
||||
continue;
|
||||
}
|
||||
pingingTask.run(webSocketClient, clientSettings.getPingInterval());
|
||||
heartbeatTask.run(webSocketClient, clientSettings.getPingInterval());
|
||||
isConnected = true;
|
||||
break;
|
||||
}
|
||||
@@ -141,7 +141,7 @@ public class TikTokWebSocketClient implements LiveSocketClient {
|
||||
public void stop() {
|
||||
if (isConnected && webSocketClient != null && webSocketClient.isOpen()) {
|
||||
webSocketClient.closeConnection(0, "");
|
||||
pingingTask.stop();
|
||||
heartbeatTask.stop();
|
||||
}
|
||||
webSocketClient = null;
|
||||
isConnected = false;
|
||||
|
||||
@@ -22,18 +22,19 @@
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok.websocket;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.live.LiveEventsHandler;
|
||||
import org.java_websocket.WebSocket;
|
||||
|
||||
public class TikTokWebSocketPingingTask {
|
||||
public class WebSocketHeartbeatTask
|
||||
{
|
||||
private Thread thread;
|
||||
private boolean isRunning = false;
|
||||
private final int MAX_TIMEOUT = 250;
|
||||
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) {
|
||||
stop();
|
||||
thread = new Thread(() -> pingTask(webSocket, pingTaskTime), "pinging-task");
|
||||
thread = new Thread(() -> heartbeatTask(webSocket, pingTaskTime), "heartbeat-task");
|
||||
isRunning = true;
|
||||
thread.start();
|
||||
}
|
||||
@@ -44,11 +45,11 @@ public class TikTokWebSocketPingingTask {
|
||||
isRunning = false;
|
||||
}
|
||||
|
||||
private void pingTask(WebSocket webSocket, long pingTaskTime) {
|
||||
private void heartbeatTask(WebSocket webSocket, long pingTaskTime) {
|
||||
while (isRunning) {
|
||||
try {
|
||||
if (webSocket.isOpen()) {
|
||||
webSocket.sendPing();
|
||||
webSocket.send(heartbeatBytes);
|
||||
Thread.sleep(pingTaskTime + (int) (Math.random() * MAX_TIMEOUT));
|
||||
} else
|
||||
Thread.sleep(SLEEP_TIME);
|
||||
@@ -70,7 +70,7 @@ Maven
|
||||
<dependency>
|
||||
<groupId>com.github.jwdeveloper.TikTok-Live-Java</groupId>
|
||||
<artifactId>Client</artifactId>
|
||||
<version>1.8.0-Release</version>
|
||||
<version>1.8.7-Release</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
@@ -87,7 +87,7 @@ dependencyResolutionManagement {
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
public static class CustomListener implements TikTokEventListener {
|
||||
public static class CustomListener {
|
||||
|
||||
@TikTokEventObserver
|
||||
public void onLike(LiveClient liveClient, TikTokLikeEvent event) {
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
<parent>
|
||||
<artifactId>TikTokLiveJava</artifactId>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<version>1.8.3-Release</version>
|
||||
<version>1.8.7-Release</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<artifactId>TikTokLiveJava</artifactId>
|
||||
<version>1.8.3-Release</version>
|
||||
<version>1.8.7-Release</version>
|
||||
</parent>
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
<dependency>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<artifactId>API</artifactId>
|
||||
<version>1.8.3-Release</version>
|
||||
<version>1.8.7-Release</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -22,9 +22,7 @@
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok.extension.collector.api;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.listener.TikTokEventListener;
|
||||
|
||||
public interface LiveDataCollector extends TikTokEventListener
|
||||
public interface LiveDataCollector
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>TikTokLiveJava</artifactId>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<version>1.8.3-Release</version>
|
||||
<version>1.8.7-Release</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>extension-recorder</artifactId>
|
||||
|
||||
@@ -22,9 +22,7 @@
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok.extension.recorder.api;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.listener.TikTokEventListener;
|
||||
|
||||
public interface LiveRecorder extends TikTokEventListener {
|
||||
public interface LiveRecorder {
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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.impl.data.*;
|
||||
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.models.ConnectionState;
|
||||
|
||||
@@ -38,14 +38,17 @@ import java.io.*;
|
||||
import java.net.URI;
|
||||
import java.net.http.*;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public class RecorderListener implements LiveRecorder {
|
||||
|
||||
private final BiConsumer<RecorderSettings, LiveClient> consumer;
|
||||
private final RecorderSettings settings;
|
||||
private final AtomicBoolean token = new AtomicBoolean();
|
||||
private DownloadData downloadData;
|
||||
private Thread liveDownloadThread;
|
||||
private CompletableFuture<Void> future;
|
||||
|
||||
public RecorderListener(BiConsumer<RecorderSettings, LiveClient> consumer) {
|
||||
this.consumer = consumer;
|
||||
@@ -74,60 +77,59 @@ public class RecorderListener implements LiveRecorder {
|
||||
if (isConnected() || downloadData.getDownloadLiveUrl().isEmpty())
|
||||
return;
|
||||
|
||||
liveDownloadThread = new Thread(() -> {
|
||||
try {
|
||||
liveClient.getLogger().info("Recording started "+liveClient.getRoomInfo().getHostName());
|
||||
|
||||
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(URI.create(downloadData.getFullUrl())).GET();
|
||||
for (var entry : LiveClientSettings.DefaultRequestHeaders().entrySet())
|
||||
requestBuilder.header(entry.getKey(), entry.getValue());
|
||||
HttpResponse<InputStream> serverResponse = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL)
|
||||
.connectTimeout(Duration.ofSeconds(10)).build().send(requestBuilder.build(), HttpResponse.BodyHandlers.ofInputStream());
|
||||
|
||||
var file = settings.getOutputFile();
|
||||
file.getParentFile().mkdirs();
|
||||
file.createNewFile();
|
||||
|
||||
try (
|
||||
var in = serverResponse.body();
|
||||
var fos = new FileOutputStream(file, true)
|
||||
) {
|
||||
byte[] dataBuffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((!settings.isStopOnDisconnect() || liveClient.getRoomInfo().getConnectionState() == ConnectionState.CONNECTED) && (bytesRead = in.read(dataBuffer)) != -1) {
|
||||
fos.write(dataBuffer, 0, bytesRead);
|
||||
fos.flush();
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
} finally {
|
||||
liveClient.getLogger().severe("Stopped recording " + liveClient.getRoomInfo().getHostName());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
var recordingStartedEvent = new TikTokLiveRecorderStartedEvent(downloadData);
|
||||
var recordingStartedEvent = new TikTokLiveRecorderStartedEvent(downloadData, settings);
|
||||
liveClient.publishEvent(recordingStartedEvent);
|
||||
if (recordingStartedEvent.isCanceled())
|
||||
liveClient.getLogger().info("Recording cancelled");
|
||||
else
|
||||
liveDownloadThread.start();
|
||||
future = CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
liveClient.getLogger().info("Recording started "+liveClient.getRoomInfo().getHostName());
|
||||
|
||||
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(URI.create(downloadData.getFullUrl())).GET();
|
||||
for (var entry : LiveClientSettings.DefaultRequestHeaders().entrySet())
|
||||
requestBuilder.header(entry.getKey(), entry.getValue());
|
||||
HttpResponse<InputStream> serverResponse = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL)
|
||||
.connectTimeout(Duration.ofSeconds(10)).build().send(requestBuilder.build(), HttpResponse.BodyHandlers.ofInputStream());
|
||||
|
||||
var file = settings.getOutputFile();
|
||||
file.getParentFile().mkdirs();
|
||||
file.createNewFile();
|
||||
|
||||
try (
|
||||
var in = serverResponse.body();
|
||||
var fos = new FileOutputStream(file, true)
|
||||
) {
|
||||
byte[] dataBuffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while (!token.get() && (!settings.isStopOnDisconnect() || liveClient.getRoomInfo().getConnectionState() == ConnectionState.CONNECTED) && (bytesRead = in.read(dataBuffer)) != -1) {
|
||||
fos.write(dataBuffer, 0, bytesRead);
|
||||
fos.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
liveClient.getLogger().info("Stopped recording " + liveClient.getRoomInfo().getHostName());
|
||||
liveClient.publishEvent(new TikTokLiveRecorderEndedEvent(settings));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@TikTokEventObserver
|
||||
private void onDisconnected(LiveClient liveClient, TikTokDisconnectedEvent event) {
|
||||
if (isConnected() && settings.isStopOnDisconnect())
|
||||
liveDownloadThread.interrupt();
|
||||
token.set(true);
|
||||
}
|
||||
|
||||
@TikTokEventObserver
|
||||
private void onLiveEnded(LiveClient liveClient, TikTokLiveEndedEvent event) {
|
||||
if (isConnected())
|
||||
liveDownloadThread.interrupt();
|
||||
token.set(true);
|
||||
}
|
||||
|
||||
|
||||
private DownloadData mapToDownloadData(String json) {
|
||||
try {
|
||||
var parsedJson = JsonParser.parseString(json);
|
||||
@@ -164,6 +166,6 @@ public class RecorderListener implements LiveRecorder {
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public class TikTokLiveRecorderStartedEvent extends TikTokEvent {
|
||||
DownloadData downloadData;
|
||||
private final DownloadData downloadData;
|
||||
private final RecorderSettings settings;
|
||||
|
||||
@Setter
|
||||
boolean canceled;
|
||||
@Setter boolean canceled;
|
||||
|
||||
public TikTokLiveRecorderStartedEvent(DownloadData downloadData) {
|
||||
public TikTokLiveRecorderStartedEvent(DownloadData downloadData, RecorderSettings settings) {
|
||||
this.downloadData = downloadData;
|
||||
this.settings = settings;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
pom.xml
2
pom.xml
@@ -7,7 +7,7 @@
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<artifactId>TikTokLiveJava</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>1.8.3-Release</version>
|
||||
<version>1.8.7-Release</version>
|
||||
<modules>
|
||||
<module>API</module>
|
||||
<module>Client</module>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>TikTokLiveJava</artifactId>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<version>1.8.3-Release</version>
|
||||
<version>1.8.7-Release</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user