mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
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
This commit is contained in:
@@ -28,8 +28,6 @@ import io.github.jwdeveloper.tiktok.data.events.common.TikTokUnhandledEvent;
|
|||||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse;
|
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@EventMeta(eventType = EventType.Debug)
|
@EventMeta(eventType = EventType.Debug)
|
||||||
public class TikTokWebsocketUnhandledMessageEvent extends TikTokUnhandledEvent<WebcastResponse.Message>
|
public class TikTokWebsocketUnhandledMessageEvent extends TikTokUnhandledEvent<WebcastResponse.Message>
|
||||||
|
|||||||
@@ -85,8 +85,7 @@ public class Picture {
|
|||||||
throw new TikTokLiveException("Unable map downloaded image", e);
|
throw new TikTokLiveException("Unable map downloaded image", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
var bais = new ByteArrayInputStream(baos.toByteArray());
|
try (var bais = new ByteArrayInputStream(baos.toByteArray())) {
|
||||||
try {
|
|
||||||
return ImageIO.read(bais);
|
return ImageIO.read(bais);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new TikTokLiveException("Unable map downloaded image bytes to Image", e);
|
throw new TikTokLiveException("Unable map downloaded image bytes to Image", e);
|
||||||
@@ -97,6 +96,13 @@ public class Picture {
|
|||||||
return new 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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Picture{link='" + link + "', image=" + image + "}";
|
return "Picture{link='" + link + "', image=" + image + "}";
|
||||||
|
|||||||
@@ -29,11 +29,11 @@ import java.util.List;
|
|||||||
|
|
||||||
public class GiftsData
|
public class GiftsData
|
||||||
{
|
{
|
||||||
@Getter
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public static final class Response
|
public static final class Response
|
||||||
{
|
{
|
||||||
private String json;
|
private final String json;
|
||||||
private List<Gift> gifts;
|
private final List<Gift> gifts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -28,20 +28,19 @@ import lombok.Data;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.time.Duration;
|
|
||||||
|
|
||||||
public class LiveConnectionData {
|
public class LiveConnectionData {
|
||||||
@Getter
|
@Getter
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public static class Request {
|
public static class Request {
|
||||||
private String roomId;
|
private final String roomId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public static class Response {
|
public static class Response {
|
||||||
private String websocketCookies;
|
private final String websocketCookies;
|
||||||
private URI websocketUrl;
|
private final URI websocketUrl;
|
||||||
private WebcastResponse webcastResponse;
|
private final WebcastResponse webcastResponse;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -31,7 +31,7 @@ public class LiveData {
|
|||||||
@Getter
|
@Getter
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public static class Request {
|
public static class Request {
|
||||||
private String roomId;
|
private final String roomId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@@ -46,11 +46,7 @@ public class LiveData {
|
|||||||
private boolean ageRestricted;
|
private boolean ageRestricted;
|
||||||
private User host;
|
private User host;
|
||||||
private LiveType liveType;
|
private LiveType liveType;
|
||||||
public Response() {
|
public Response() {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum LiveStatus {
|
public enum LiveStatus {
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ public class LiveUserData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public static class Response {
|
public static class Response {
|
||||||
private final String json;
|
private final String json;
|
||||||
|
|||||||
@@ -22,16 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
package io.github.jwdeveloper.tiktok.live;
|
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.messages.webcast.WebcastResponse;
|
||||||
import io.github.jwdeveloper.tiktok.utils.Stopwatch;
|
|
||||||
|
|
||||||
import java.time.Duration;
|
|
||||||
|
|
||||||
public interface LiveMessagesHandler {
|
public interface LiveMessagesHandler {
|
||||||
void handle(LiveClient client, WebcastResponse webcastResponse);
|
void handle(LiveClient client, WebcastResponse webcastResponse);
|
||||||
|
|||||||
@@ -225,7 +225,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
|
|||||||
if (clientSettings.getApiKey() != null)
|
if (clientSettings.getApiKey() != null)
|
||||||
builder.withParam("apiKey", clientSettings.getApiKey());
|
builder.withParam("apiKey", clientSettings.getApiKey());
|
||||||
|
|
||||||
var result = builder.build().toResponse();
|
var result = builder.build().toHttpResponse(HttpResponse.BodyHandlers.ofByteArray());
|
||||||
|
|
||||||
if (result.isFailure())
|
if (result.isFailure())
|
||||||
throw new TikTokSignServerException("Unable to get websocket connection credentials - "+result);
|
throw new TikTokSignServerException("Unable to get websocket connection credentials - "+result);
|
||||||
|
|||||||
@@ -42,11 +42,11 @@ public class HttpClient {
|
|||||||
protected final String url;
|
protected final String url;
|
||||||
private final Pattern pattern = Pattern.compile("charset=(.*?)(?=&|$)");
|
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 client = prepareClient();
|
||||||
var request = prepareGetRequest();
|
var request = prepareGetRequest();
|
||||||
try {
|
try {
|
||||||
var response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
|
var response = client.send(request, handler);
|
||||||
var result = ActionResult.of(response);
|
var result = ActionResult.of(response);
|
||||||
return switch (response.statusCode()) {
|
return switch (response.statusCode()) {
|
||||||
case 420 -> result.message("HttpResponse Code:", response.statusCode(), "| IP Cloudflare Blocked.").failure();
|
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() {
|
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) {
|
private Charset charsetFrom(HttpHeaders headers) {
|
||||||
@@ -87,7 +91,7 @@ public class HttpClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ActionResult<byte[]> toBinaryResponse() {
|
public ActionResult<byte[]> toBinaryResponse() {
|
||||||
return toResponse().map(HttpResponse::body);
|
return toResponse(HttpResponse.BodyHandlers.ofByteArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public URI toUri() {
|
public URI toUri() {
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ public class HttpProxyClient extends HttpClient {
|
|||||||
throw new TikTokProxyRequestException(e);
|
throw new TikTokProxyRequestException(e);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (e.getMessage().contains("503") && proxySettings.isFallback()) // Indicates proxy protocol is not supported
|
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);
|
throw new TikTokProxyRequestException(e);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new TikTokLiveRequestException(e);
|
throw new TikTokLiveRequestException(e);
|
||||||
@@ -122,7 +122,7 @@ public class HttpProxyClient extends HttpClient {
|
|||||||
return ActionResult.success(response);
|
return ActionResult.success(response);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (e.getMessage().contains("503") && proxySettings.isFallback()) // Indicates proxy protocol is not supported
|
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())
|
if (proxySettings.isAutoDiscard())
|
||||||
proxySettings.remove();
|
proxySettings.remove();
|
||||||
throw new TikTokProxyRequestException(e);
|
throw new TikTokProxyRequestException(e);
|
||||||
|
|||||||
Reference in New Issue
Block a user