From 14bf9c8c9bafd668c2dddb73b25784e393f20abd Mon Sep 17 00:00:00 2001 From: kohlerpop1 Date: Fri, 22 Nov 2024 22:31:23 -0500 Subject: [PATCH 1/3] Removal of blank spaces and unused imports/dependencies Optimize picture download memory usage and add option to convert to unsigned url Convert response data classes to final and add toString with `@Data` Made sending requests dynamic by allowing passing of BodyHandler to class --- .../TikTokWebsocketUnhandledMessageEvent.java | 4 +--- .../jwdeveloper/tiktok/data/models/Picture.java | 10 ++++++++-- .../jwdeveloper/tiktok/data/requests/GiftsData.java | 6 +++--- .../tiktok/data/requests/LiveConnectionData.java | 13 ++++++------- .../jwdeveloper/tiktok/data/requests/LiveData.java | 8 ++------ .../tiktok/data/requests/LiveUserData.java | 2 +- .../tiktok/live/LiveMessagesHandler.java | 11 +---------- .../jwdeveloper/tiktok/TikTokLiveHttpClient.java | 2 +- .../github/jwdeveloper/tiktok/http/HttpClient.java | 12 ++++++++---- .../jwdeveloper/tiktok/http/HttpProxyClient.java | 4 ++-- 10 files changed, 33 insertions(+), 39 deletions(-) diff --git a/API/src/main/java/io/github/jwdeveloper/tiktok/data/events/websocket/TikTokWebsocketUnhandledMessageEvent.java b/API/src/main/java/io/github/jwdeveloper/tiktok/data/events/websocket/TikTokWebsocketUnhandledMessageEvent.java index 83b6beb..6ba278c 100644 --- a/API/src/main/java/io/github/jwdeveloper/tiktok/data/events/websocket/TikTokWebsocketUnhandledMessageEvent.java +++ b/API/src/main/java/io/github/jwdeveloper/tiktok/data/events/websocket/TikTokWebsocketUnhandledMessageEvent.java @@ -28,8 +28,6 @@ import io.github.jwdeveloper.tiktok.data.events.common.TikTokUnhandledEvent; import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse; import lombok.Getter; - - @Getter @EventMeta(eventType = EventType.Debug) public class TikTokWebsocketUnhandledMessageEvent extends TikTokUnhandledEvent @@ -42,4 +40,4 @@ public class TikTokWebsocketUnhandledMessageEvent extends TikTokUnhandledEvent p16-va.tiktokcdn.com || p16-sign.tiktokcdn.com -> p16.tiktokcdn.com + return new Picture(link.replace("-sign-", "-").replace("-sign.", ".")); + } + @Override public String toString() { return "Picture{link='" + link + "', image=" + image + "}"; diff --git a/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/GiftsData.java b/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/GiftsData.java index e1742e2..35f367d 100644 --- a/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/GiftsData.java +++ b/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/GiftsData.java @@ -29,11 +29,11 @@ import java.util.List; public class GiftsData { - @Getter + @Data @AllArgsConstructor public static final class Response { - private String json; - private List gifts; + private final String json; + private final List gifts; } } \ No newline at end of file diff --git a/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveConnectionData.java b/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveConnectionData.java index 152fb0d..8d5a1ba 100644 --- a/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveConnectionData.java +++ b/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveConnectionData.java @@ -28,20 +28,19 @@ import lombok.Data; import lombok.Getter; import java.net.URI; -import java.time.Duration; public class LiveConnectionData { @Getter @AllArgsConstructor public static class Request { - private String roomId; + private final String roomId; } - @Getter + @Data @AllArgsConstructor public static class Response { - private String websocketCookies; - private URI websocketUrl; - private WebcastResponse webcastResponse; + private final String websocketCookies; + private final URI websocketUrl; + private final WebcastResponse webcastResponse; } -} +} \ No newline at end of file diff --git a/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveData.java b/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveData.java index 174ad90..81ed155 100644 --- a/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveData.java +++ b/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveData.java @@ -31,7 +31,7 @@ public class LiveData { @Getter @AllArgsConstructor public static class Request { - private String roomId; + private final String roomId; } @Data @@ -46,11 +46,7 @@ public class LiveData { private boolean ageRestricted; private User host; private LiveType liveType; - public Response() { - - } - - + public Response() {} } public enum LiveStatus { diff --git a/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveUserData.java b/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveUserData.java index 04ea2d0..a48d596 100644 --- a/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveUserData.java +++ b/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveUserData.java @@ -38,7 +38,7 @@ public class LiveUserData { } } - @Getter + @Data @AllArgsConstructor public static class Response { private final String json; diff --git a/API/src/main/java/io/github/jwdeveloper/tiktok/live/LiveMessagesHandler.java b/API/src/main/java/io/github/jwdeveloper/tiktok/live/LiveMessagesHandler.java index 76e05ab..4053ee8 100644 --- a/API/src/main/java/io/github/jwdeveloper/tiktok/live/LiveMessagesHandler.java +++ b/API/src/main/java/io/github/jwdeveloper/tiktok/live/LiveMessagesHandler.java @@ -22,19 +22,10 @@ */ package io.github.jwdeveloper.tiktok.live; -import io.github.jwdeveloper.tiktok.data.dto.MessageMetaData; -import io.github.jwdeveloper.tiktok.data.events.TikTokErrorEvent; -import io.github.jwdeveloper.tiktok.data.events.websocket.TikTokWebsocketMessageEvent; -import io.github.jwdeveloper.tiktok.data.events.websocket.TikTokWebsocketResponseEvent; -import io.github.jwdeveloper.tiktok.data.events.websocket.TikTokWebsocketUnhandledMessageEvent; -import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveMessageException; import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse; -import io.github.jwdeveloper.tiktok.utils.Stopwatch; - -import java.time.Duration; public interface LiveMessagesHandler { void handle(LiveClient client, WebcastResponse webcastResponse); void handleSingleMessage(LiveClient client, WebcastResponse.Message message); -} +} \ No newline at end of file diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveHttpClient.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveHttpClient.java index 039c6e7..908a29e 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveHttpClient.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveHttpClient.java @@ -225,7 +225,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient if (clientSettings.getApiKey() != null) builder.withParam("apiKey", clientSettings.getApiKey()); - var result = builder.build().toResponse(); + var result = builder.build().toHttpResponse(HttpResponse.BodyHandlers.ofByteArray()); if (result.isFailure()) throw new TikTokSignServerException("Unable to get websocket connection credentials - "+result); diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpClient.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpClient.java index 6a42b83..b5c9fd8 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpClient.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpClient.java @@ -42,11 +42,11 @@ public class HttpClient { protected final String url; private final Pattern pattern = Pattern.compile("charset=(.*?)(?=&|$)"); - public ActionResult> toResponse() { + public ActionResult> toHttpResponse(HttpResponse.BodyHandler handler) { var client = prepareClient(); var request = prepareGetRequest(); try { - var response = client.send(request, HttpResponse.BodyHandlers.ofByteArray()); + var response = client.send(request, handler); var result = ActionResult.of(response); return switch (response.statusCode()) { case 420 -> result.message("HttpResponse Code:", response.statusCode(), "| IP Cloudflare Blocked.").failure(); @@ -68,8 +68,12 @@ public class HttpClient { } } + public ActionResult toResponse(HttpResponse.BodyHandler handler) { + return toHttpResponse(handler).map(HttpResponse::body); + } + public ActionResult toJsonResponse() { - return toResponse().map(content -> new String(content.body(), charsetFrom(content.headers()))); + return toResponse(HttpResponse.BodyHandlers.ofString()); } private Charset charsetFrom(HttpHeaders headers) { @@ -87,7 +91,7 @@ public class HttpClient { } public ActionResult toBinaryResponse() { - return toResponse().map(HttpResponse::body); + return toResponse(HttpResponse.BodyHandlers.ofByteArray()); } public URI toUri() { diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpProxyClient.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpProxyClient.java index 1de9169..31331ad 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpProxyClient.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpProxyClient.java @@ -77,7 +77,7 @@ public class HttpProxyClient extends HttpClient { throw new TikTokProxyRequestException(e); } catch (IOException e) { if (e.getMessage().contains("503") && proxySettings.isFallback()) // Indicates proxy protocol is not supported - return super.toResponse(); + return super.toHttpResponse(HttpResponse.BodyHandlers.ofByteArray()); throw new TikTokProxyRequestException(e); } catch (Exception e) { throw new TikTokLiveRequestException(e); @@ -122,7 +122,7 @@ public class HttpProxyClient extends HttpClient { return ActionResult.success(response); } catch (IOException e) { if (e.getMessage().contains("503") && proxySettings.isFallback()) // Indicates proxy protocol is not supported - return super.toResponse(); + return super.toHttpResponse(HttpResponse.BodyHandlers.ofByteArray()); if (proxySettings.isAutoDiscard()) proxySettings.remove(); throw new TikTokProxyRequestException(e); From 84b0a3aaf4cf3f13ef11cb7b9f999f1d6fe7121b Mon Sep 17 00:00:00 2001 From: kohlerpop1 Date: Sun, 24 Nov 2024 00:18:34 -0500 Subject: [PATCH 2/3] Add equals and hashCode to picture for easier comparison. --- .../jwdeveloper/tiktok/data/models/Picture.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/API/src/main/java/io/github/jwdeveloper/tiktok/data/models/Picture.java b/API/src/main/java/io/github/jwdeveloper/tiktok/data/models/Picture.java index efe1053..2988f10 100644 --- a/API/src/main/java/io/github/jwdeveloper/tiktok/data/models/Picture.java +++ b/API/src/main/java/io/github/jwdeveloper/tiktok/data/models/Picture.java @@ -30,6 +30,7 @@ import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.net.URL; +import java.util.Objects; import java.util.concurrent.CompletableFuture; public class Picture { @@ -107,4 +108,14 @@ public class Picture { public String toString() { return "Picture{link='" + link + "', image=" + image + "}"; } + + @Override + public final boolean equals(Object o) { + return o == this || o instanceof Picture picture && picture.link != null && picture.link.equals(link); + } + + @Override + public int hashCode() { + return Objects.hashCode(link); + } } \ No newline at end of file From 437335f7840c4f7ec4ac4385a9050580d8d2c273 Mon Sep 17 00:00:00 2001 From: kohlerpop1 Date: Mon, 25 Nov 2024 20:11:23 -0500 Subject: [PATCH 3/3] Should be _session_id not just session_id. Maybe this will fix stream disconnections! --- .../tiktok/extension/recorder/impl/data/DownloadData.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension-recorder/src/main/java/io/github/jwdeveloper/tiktok/extension/recorder/impl/data/DownloadData.java b/extension-recorder/src/main/java/io/github/jwdeveloper/tiktok/extension/recorder/impl/data/DownloadData.java index 49ef130..992564f 100644 --- a/extension-recorder/src/main/java/io/github/jwdeveloper/tiktok/extension/recorder/impl/data/DownloadData.java +++ b/extension-recorder/src/main/java/io/github/jwdeveloper/tiktok/extension/recorder/impl/data/DownloadData.java @@ -34,6 +34,6 @@ public class DownloadData { private String sessionId; public String getFullUrl() { - return downloadLiveUrl + (downloadLiveUrl.contains("?") ? "&" : "?") + "_webnoredir=1&session_id=" + sessionId; + return downloadLiveUrl + (downloadLiveUrl.contains("?") ? "&" : "?") + "_webnoredir=1&_session_id=" + sessionId; } } \ No newline at end of file