mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
@@ -38,9 +38,9 @@ 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)
|
||||||
* {
|
* {
|
||||||
* System.out.println(event.getException().getMessage());
|
* System.out.println(event.getException().getMessage());
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
@@ -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);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
@@ -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
|
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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,60 +77,59 @@ 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);
|
||||||
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);
|
|
||||||
liveClient.publishEvent(recordingStartedEvent);
|
liveClient.publishEvent(recordingStartedEvent);
|
||||||
if (recordingStartedEvent.isCanceled())
|
if (recordingStartedEvent.isCanceled())
|
||||||
liveClient.getLogger().info("Recording cancelled");
|
liveClient.getLogger().info("Recording cancelled");
|
||||||
else
|
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
|
@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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user