From 6b22154c82dca2a8cb7cd2c0427840ee939a4982 Mon Sep 17 00:00:00 2001 From: kohlerpop1 Date: Thu, 15 Feb 2024 16:40:39 -0500 Subject: [PATCH] Pushing broken changes for JW! --- .../github/jwdeveloper/tiktok/TikTokLive.java | 14 ++-- .../tiktok/TikTokLiveClientBuilder.java | 48 ++++------- .../tiktok/TikTokLiveHttpClient.java | 15 ++-- .../tiktok/common/ActionResult.java | 83 +++++++++++++++++++ .../tiktok/common/ActionResultBuilder.java | 27 ++++++ .../tiktok/common/LoggerFactory.java | 32 +++++++ .../jwdeveloper/tiktok/http/HttpClient.java | 27 ++---- 7 files changed, 182 insertions(+), 64 deletions(-) create mode 100644 Client/src/main/java/io/github/jwdeveloper/tiktok/common/ActionResult.java create mode 100644 Client/src/main/java/io/github/jwdeveloper/tiktok/common/ActionResultBuilder.java create mode 100644 Client/src/main/java/io/github/jwdeveloper/tiktok/common/LoggerFactory.java diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLive.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLive.java index 2d67e89..3c2f1e0 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLive.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLive.java @@ -31,8 +31,8 @@ import java.util.concurrent.CompletableFuture; public class TikTokLive { /** + * Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo' * @param hostName profile name of Tiktok user could be found in profile link - * example: https://www.tiktok.com/@dostawcavideo hostName would be dostawcavideo * @return LiveClientBuilder */ public static LiveClientBuilder newClient(String hostName) { @@ -40,9 +40,8 @@ public class TikTokLive { } /** - * + * Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo' * @param hostName profile name of Tiktok user could be found in profile link - * example: https://www.tiktok.com/@dostawcavideo hostName would be dostawcavideo * @return true if live is Online, false if is offline */ public static boolean isLiveOnline(String hostName) @@ -51,9 +50,8 @@ public class TikTokLive { } /** - * + * Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo' * @param hostName profile name of Tiktok user could be found in profile link - * example: https://www.tiktok.com/@dostawcavideo hostName would be dostawcavideo * @return true if live is Online, false if is offline */ public static CompletableFuture isLiveOnlineAsync(String hostName) @@ -62,9 +60,8 @@ public class TikTokLive { } /** - * + * Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo' * @param hostName profile name of Tiktok user could be found in profile link - * example: https://www.tiktok.com/@dostawcavideo hostName would be dostawcavideo * @return true is hostName name is valid and exists, false if not */ public static boolean isHostNameValid(String hostName) @@ -73,9 +70,8 @@ public class TikTokLive { } /** - * + * Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo' * @param hostName profile name of Tiktok user could be found in profile link - * example: https://www.tiktok.com/@dostawcavideo hostName would be dostawcavideo * @return true is hostName name is valid and exists, false if not */ public static CompletableFuture isHostNameValidAsync(String hostName) diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClientBuilder.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClientBuilder.java index 26a69e2..80c5793 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClientBuilder.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClientBuilder.java @@ -22,69 +22,52 @@ */ package io.github.jwdeveloper.tiktok; +import io.github.jwdeveloper.tiktok.common.LoggerFactory; import io.github.jwdeveloper.tiktok.data.events.*; import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent; import io.github.jwdeveloper.tiktok.data.events.control.TikTokPreConnectionEvent; import io.github.jwdeveloper.tiktok.data.events.envelop.TikTokChestEvent; -import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftComboEvent; -import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftEvent; +import io.github.jwdeveloper.tiktok.data.events.gift.*; import io.github.jwdeveloper.tiktok.data.events.http.TikTokHttpResponseEvent; import io.github.jwdeveloper.tiktok.data.events.poll.TikTokPollEvent; -import io.github.jwdeveloper.tiktok.data.events.room.TikTokRoomInfoEvent; -import io.github.jwdeveloper.tiktok.data.events.room.TikTokRoomPinEvent; -import io.github.jwdeveloper.tiktok.data.events.social.TikTokFollowEvent; -import io.github.jwdeveloper.tiktok.data.events.social.TikTokJoinEvent; -import io.github.jwdeveloper.tiktok.data.events.social.TikTokLikeEvent; -import io.github.jwdeveloper.tiktok.data.events.social.TikTokShareEvent; -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.data.events.room.*; +import io.github.jwdeveloper.tiktok.data.events.social.*; +import io.github.jwdeveloper.tiktok.data.events.websocket.*; +import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings; import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException; import io.github.jwdeveloper.tiktok.gifts.TikTokGiftManager; import io.github.jwdeveloper.tiktok.http.HttpClientFactory; -import io.github.jwdeveloper.tiktok.listener.TikTokEventListener; -import io.github.jwdeveloper.tiktok.listener.TikTokListenersManager; -import io.github.jwdeveloper.tiktok.live.GiftManager; -import io.github.jwdeveloper.tiktok.live.LiveClient; -import io.github.jwdeveloper.tiktok.live.builder.EventConsumer; -import io.github.jwdeveloper.tiktok.live.builder.LiveClientBuilder; -import io.github.jwdeveloper.tiktok.mappers.TikTokGenericEventMapper; -import io.github.jwdeveloper.tiktok.mappers.TikTokLiveMapper; -import io.github.jwdeveloper.tiktok.mappers.TikTokLiveMapperHelper; -import io.github.jwdeveloper.tiktok.mappers.TikTokMapper; +import io.github.jwdeveloper.tiktok.listener.*; +import io.github.jwdeveloper.tiktok.live.*; +import io.github.jwdeveloper.tiktok.live.builder.*; +import io.github.jwdeveloper.tiktok.mappers.*; import io.github.jwdeveloper.tiktok.mappers.data.MappingResult; -import io.github.jwdeveloper.tiktok.mappers.handlers.TikTokCommonEventHandler; -import io.github.jwdeveloper.tiktok.mappers.handlers.TikTokGiftEventHandler; -import io.github.jwdeveloper.tiktok.mappers.handlers.TikTokRoomInfoEventHandler; -import io.github.jwdeveloper.tiktok.mappers.handlers.TikTokSocialMediaEventHandler; +import io.github.jwdeveloper.tiktok.mappers.handlers.*; import io.github.jwdeveloper.tiktok.messages.webcast.*; -import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings; import io.github.jwdeveloper.tiktok.utils.ConsoleColors; import io.github.jwdeveloper.tiktok.websocket.TikTokWebSocketClient; -import java.util.ArrayList; -import java.util.List; +import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; +import java.util.logging.Formatter; import java.util.logging.*; public class TikTokLiveClientBuilder implements LiveClientBuilder { protected final LiveClientSettings clientSettings; - protected final Logger logger; protected final TikTokLiveEventHandler tikTokEventHandler; protected final List listeners; protected Consumer onCustomMappings; + protected Logger logger; public TikTokLiveClientBuilder(String userName) { this.clientSettings = LiveClientSettings.createDefault(); this.clientSettings.setHostName(userName); this.tikTokEventHandler = new TikTokLiveEventHandler(); - this.logger = Logger.getLogger(TikTokLive.class.getSimpleName() + " " + userName); this.listeners = new ArrayList<>(); - this.onCustomMappings = (e) -> { - }; + this.onCustomMappings = (e) -> {}; } public LiveClientBuilder onMapping(Consumer onCustomMappings) { @@ -124,6 +107,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder { httpSettings.getParams().put("webcast_language", clientSettings.getClientLanguage()); + this.logger = LoggerFactory.create(clientSettings.getHostName(), clientSettings); var handler = new ConsoleHandler(); handler.setFormatter(new Formatter() { @Override 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 f1b95b6..6752bb2 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveHttpClient.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveHttpClient.java @@ -23,8 +23,9 @@ package io.github.jwdeveloper.tiktok; import com.google.protobuf.InvalidProtocolBufferException; +import io.github.jwdeveloper.tiktok.common.LoggerFactory; import io.github.jwdeveloper.tiktok.data.requests.*; -import io.github.jwdeveloper.tiktok.data.settings.*; +import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings; import io.github.jwdeveloper.tiktok.exceptions.*; import io.github.jwdeveloper.tiktok.http.*; import io.github.jwdeveloper.tiktok.http.mappers.*; @@ -32,9 +33,10 @@ import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse; import java.net.http.HttpResponse; import java.util.Optional; +import java.util.logging.Logger; -public class TikTokLiveHttpClient implements LiveHttpClient { - +public class TikTokLiveHttpClient implements LiveHttpClient +{ /** * Signing API by Isaac Kogan */ @@ -48,10 +50,12 @@ public class TikTokLiveHttpClient implements LiveHttpClient { private final LiveUserDataMapper liveUserDataMapper; private final LiveDataMapper liveDataMapper; private final GiftsDataMapper giftsDataMapper; + private final Logger logger; public TikTokLiveHttpClient(HttpClientFactory factory, LiveClientSettings settings) { this.httpFactory = factory; - clientSettings = settings; + this.clientSettings = settings; + this.logger = LoggerFactory.create("HttpClient", clientSettings); liveUserDataMapper = new LiveUserDataMapper(); liveDataMapper = new LiveDataMapper(); giftsDataMapper = new GiftsDataMapper(); @@ -169,7 +173,8 @@ public class TikTokLiveHttpClient implements LiveHttpClient { try { var optionalHeader = credentialsResponse.headers().firstValue("x-set-tt-cookie"); if (optionalHeader.isEmpty()) { - throw new TikTokSignServerException("Sign server did not return the set-cookie header"); + logger.warning("SignServer Headers: "+credentialsResponse.headers().map()); + throw new TikTokSignServerException("Sign server did not return the x-set-tt-cookie header"); } var websocketCookie = optionalHeader.get(); var webcastResponse = WebcastResponse.parseFrom(credentialsResponse.body()); diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/common/ActionResult.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/common/ActionResult.java new file mode 100644 index 0000000..bf68260 --- /dev/null +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/common/ActionResult.java @@ -0,0 +1,83 @@ +package io.github.jwdeveloper.tiktok.common; + +import lombok.Data; + +import java.util.function.Function; + +@Data +public class ActionResult +{ + private boolean success = true; + private T content; + private String message; + + protected ActionResult(T object) { + this.content = object; + } + + protected ActionResult(T object, boolean success) { + this(object); + this.success = success; + } + + protected ActionResult(T object, boolean success, String message) { + this(object, success); + this.message = message; + } + + public static ActionResultBuilder of(T content) { + return new ActionResultBuilder<>(content); + } + + public boolean isFailure() { + return !isSuccess(); + } + + public boolean hasMessage() { + return message != null; + } + + public boolean hasContent() { + return content != null; + } + + public static ActionResult success() { + return new ActionResult<>(null, true); + } + + public ActionResult cast(Output output) { + return new ActionResult<>(output, this.isSuccess(), this.getMessage()); + } + + public ActionResult cast() { + return new ActionResult<>(null, this.isSuccess(), this.getMessage()); + } + + public ActionResult map(Function mapper) { + return hasContent() ? cast(mapper.apply(content)) : cast(); + } + + public static ActionResult cast(ActionResult action, Output output) { + return new ActionResult<>(output, action.isSuccess(), action.getMessage()); + } + + public static ActionResult success(T payload) { + return new ActionResult<>(payload, true); + } + + public static ActionResult success(T payload, String message) { + return new ActionResult<>(payload, true, message); + } + + public static ActionResult failure() { + return new ActionResult<>(null, false); + } + + public static ActionResult failure(String message) { + return new ActionResult<>(null, false, message); + } + + public static ActionResult failure(T target, String message) { + return new ActionResult<>(target, false, message); + } +} \ No newline at end of file diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/common/ActionResultBuilder.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/common/ActionResultBuilder.java new file mode 100644 index 0000000..369d35f --- /dev/null +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/common/ActionResultBuilder.java @@ -0,0 +1,27 @@ +package io.github.jwdeveloper.tiktok.common; + +import java.util.Arrays; +import java.util.stream.Collectors; + +public class ActionResultBuilder +{ + private final T content; + private String message; + + public ActionResultBuilder(T content) { + this.content = content; + } + + public ActionResultBuilder message(Object... messages) { + this.message = Arrays.stream(messages).map(Object::toString).collect(Collectors.joining(" ")); + return this; + } + + public ActionResult success() { + return ActionResult.success(content, message); + } + + public ActionResult failure() { + return ActionResult.success(content, message); + } +} \ No newline at end of file diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/common/LoggerFactory.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/common/LoggerFactory.java new file mode 100644 index 0000000..2e3f61d --- /dev/null +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/common/LoggerFactory.java @@ -0,0 +1,32 @@ +package io.github.jwdeveloper.tiktok.common; + +import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings; +import io.github.jwdeveloper.tiktok.utils.ConsoleColors; + +import java.util.logging.*; + +public class LoggerFactory +{ + public static Logger create(String name, LiveClientSettings settings) { + Logger logger = Logger.getLogger(name); + var handler = new ConsoleHandler(); + handler.setFormatter(new Formatter() { + @Override + public String format(LogRecord record) { + var sb = new StringBuilder(); + sb.append(ConsoleColors.GREEN).append("[").append(record.getLoggerName()).append("] "); + sb.append(ConsoleColors.GREEN).append("[").append(record.getLevel()).append("]: "); + sb.append(ConsoleColors.WHITE_BRIGHT).append(record.getMessage()); + sb.append(ConsoleColors.RESET).append("\n"); + return sb.toString(); + } + }); + logger.setUseParentHandlers(false); + logger.addHandler(handler); + logger.setLevel(settings.getLogLevel()); + if (!settings.isPrintToConsole()) { + logger.setLevel(Level.OFF); + } + return logger; + } +} \ No newline at end of file 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 f7a8d35..2716bdb 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 @@ -22,6 +22,7 @@ */ package io.github.jwdeveloper.tiktok.http; +import io.github.jwdeveloper.tiktok.common.ActionResult; import io.github.jwdeveloper.tiktok.data.settings.HttpClientSettings; import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException; import lombok.AllArgsConstructor; @@ -34,36 +35,26 @@ import java.util.regex.*; import java.util.stream.Collectors; @AllArgsConstructor -public class HttpClient { +public class HttpClient +{ protected final HttpClientSettings httpClientSettings; protected final String url; private final Pattern pattern = Pattern.compile("charset=(.*?)(?=&|$)"); - public Optional> toResponse() { + public ActionResult> toResponse() { var client = prepareClient(); var request = prepareGetRequest(); try { var response = client.send(request, HttpResponse.BodyHandlers.ofByteArray()); - if (response.statusCode() != 200) { - return Optional.empty(); - } - - return Optional.of(response); - } catch (Exception e) { + var result = ActionResult.of(response); + return response.statusCode() != 200 ? result.message("HttpResponse Code: ", response.statusCode()).failure() : result.success(); + } catch (Exception e) { throw new TikTokLiveRequestException(e); } } - public Optional toJsonResponse() { - var optional = toResponse(); - if (optional.isEmpty()) { - return Optional.empty(); - } - - var response = optional.get(); - var body = response.body(); - var charset = charsetFrom(response.headers()); - return Optional.of(new String(body,charset)); + public ActionResult toJsonResponse() { + return toResponse().map(content -> new String(content.body(), charsetFrom(content.headers()))); } private Charset charsetFrom(HttpHeaders headers) {