mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
@@ -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<WebcastResponse.Message>
|
||||
|
||||
@@ -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 {
|
||||
@@ -85,8 +86,7 @@ public class Picture {
|
||||
throw new TikTokLiveException("Unable map downloaded image", e);
|
||||
}
|
||||
|
||||
var bais = new ByteArrayInputStream(baos.toByteArray());
|
||||
try {
|
||||
try (var bais = new ByteArrayInputStream(baos.toByteArray())) {
|
||||
return ImageIO.read(bais);
|
||||
} catch (IOException e) {
|
||||
throw new TikTokLiveException("Unable map downloaded image bytes to Image", e);
|
||||
@@ -97,8 +97,25 @@ public class Picture {
|
||||
return new Picture("");
|
||||
}
|
||||
|
||||
public Picture asUnsigned() {
|
||||
if (link == null || link.isEmpty())
|
||||
return this;
|
||||
// p16-sign-va.tiktokcdn.com -> 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 + "}";
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
@@ -29,11 +29,11 @@ import java.util.List;
|
||||
|
||||
public class GiftsData
|
||||
{
|
||||
@Getter
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public static final class Response
|
||||
{
|
||||
private String json;
|
||||
private List<Gift> gifts;
|
||||
private final String json;
|
||||
private final List<Gift> gifts;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -38,7 +38,7 @@ public class LiveUserData {
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public static class Response {
|
||||
private final String json;
|
||||
|
||||
@@ -22,16 +22,7 @@
|
||||
*/
|
||||
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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -42,11 +42,11 @@ public class HttpClient {
|
||||
protected final String url;
|
||||
private final Pattern pattern = Pattern.compile("charset=(.*?)(?=&|$)");
|
||||
|
||||
public ActionResult<HttpResponse<byte[]>> toResponse() {
|
||||
public <T> ActionResult<HttpResponse<T>> toHttpResponse(HttpResponse.BodyHandler<T> 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 <T> ActionResult<T> toResponse(HttpResponse.BodyHandler<T> handler) {
|
||||
return toHttpResponse(handler).map(HttpResponse::body);
|
||||
}
|
||||
|
||||
public ActionResult<String> 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<byte[]> toBinaryResponse() {
|
||||
return toResponse().map(HttpResponse::body);
|
||||
return toResponse(HttpResponse.BodyHandlers.ofByteArray());
|
||||
}
|
||||
|
||||
public URI toUri() {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user