mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-28 09:19:40 -05:00
Breaking changes:
'Gift': changed from class to enum, so now you can handle
incoming gifts in switch
`Events`
- new:
onGiftComboFinished
- Removed:
onGiftBrodcast
- Rename:
onGiftMessage -> onGift
onRoomPinMessage -> onRoomPin
onRoomMessage -> onRoom
onLinkMessage -> onLink
onBarrageMessage -> onBarrage
onPollMessage -> onPoll
onShopMessage -> onShop
onDetectMessage -> onDetect
`GiftManager`
added:
registerGift
findById
findByName
getGifts
removed:
getActiveGifts
This commit is contained in:
@@ -22,122 +22,26 @@
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok.tools.collector;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.TikTokLive;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveMessageException;
|
||||
import io.github.jwdeveloper.tiktok.tools.collector.db.TikTokDatabase;
|
||||
import io.github.jwdeveloper.tiktok.tools.collector.tables.ExceptionInfoModel;
|
||||
import io.github.jwdeveloper.tiktok.tools.collector.tables.TikTokErrorModel;
|
||||
import io.github.jwdeveloper.tiktok.tools.collector.tables.TikTokMessageModel;
|
||||
import io.github.jwdeveloper.tiktok.tools.collector.tables.TikTokResponseModel;
|
||||
import io.github.jwdeveloper.tiktok.tools.collector.client.TikTokMessageCollectorClient;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
public class RunCollector {
|
||||
|
||||
//https://protobuf-decoder.netlify.app/
|
||||
//https://streamdps.com/tiktok-widgets/gifts/
|
||||
|
||||
|
||||
public static List<String> ignoredEvents;
|
||||
|
||||
public static List<Class<?>> filter;
|
||||
//WebcastLinkMicBattleItemCard does streamer win battle?
|
||||
|
||||
public static void main(String[] args) throws SQLException {
|
||||
ignoredEvents = new ArrayList<>();
|
||||
//ignoredEvents = List.of("TikTokJoinEvent","TikTokLikeEvent");
|
||||
|
||||
filter = new ArrayList<>();
|
||||
// filter.add(TikTokUnhandledSocialEvent.class);
|
||||
// filter.add(TikTokFollowEvent.class);
|
||||
// filter.add(TikTokLikeEvent.class);
|
||||
// filter.add(TikTokShareEvent.class);
|
||||
// filter.add(TikTokJoinEvent.class);
|
||||
|
||||
var db = new TikTokDatabase("test");
|
||||
db.init();
|
||||
|
||||
var users = new ArrayList<String>();
|
||||
// users.add("mia_tattoo");
|
||||
// users.add("mr_wavecheck");
|
||||
users.add("bangbetmenygy");
|
||||
// users.add("szwagierkaqueen");
|
||||
for (var user : users) {
|
||||
try {
|
||||
runTikTokLiveInstance(user, db);
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void runTikTokLiveInstance(String tiktokUser, TikTokDatabase tikTokDatabase) {
|
||||
|
||||
TikTokLive.newClient(tiktokUser)
|
||||
.onConnected((liveClient, event) ->
|
||||
{
|
||||
System.out.println("CONNECTED TO " + liveClient.getRoomInfo().getHostName());
|
||||
})
|
||||
.onWebsocketResponse((liveClient, event) ->
|
||||
{
|
||||
var response = Base64.getEncoder().encodeToString(event.getResponse().toByteArray());
|
||||
|
||||
var responseModel = new TikTokResponseModel();
|
||||
responseModel.setResponse(response);
|
||||
responseModel.setHostName(liveClient.getRoomInfo().getHostName());
|
||||
|
||||
tikTokDatabase.insertResponse(responseModel);
|
||||
System.out.println("Included response");
|
||||
})
|
||||
.onWebsocketMessage((liveClient, event) ->
|
||||
{
|
||||
var eventName = event.getEvent().getClass().getSimpleName();
|
||||
|
||||
if (filter.size() != 0 && !filter.contains(event.getEvent().getClass())) {
|
||||
return;
|
||||
}
|
||||
|
||||
var messageBinary = Base64.getEncoder().encodeToString(event.getMessage().toByteArray());
|
||||
var model = new TikTokMessageModel();
|
||||
model.setType("messsage");
|
||||
model.setHostName(tiktokUser);
|
||||
model.setEventName(eventName);
|
||||
model.setMessage(messageBinary);
|
||||
|
||||
// tikTokDatabase.insertMessage(model);
|
||||
System.out.println("EVENT: [" + tiktokUser + "] " + eventName);
|
||||
})
|
||||
.onError((liveClient, event) ->
|
||||
{
|
||||
event.getException().printStackTrace();
|
||||
var exception = event.getException();
|
||||
var exceptionContent = ExceptionInfoModel.getStackTraceAsString(exception);
|
||||
var errorModel = new TikTokErrorModel();
|
||||
if (exception instanceof TikTokLiveMessageException ex) {
|
||||
errorModel.setHostName(tiktokUser);
|
||||
errorModel.setErrorName(ex.messageMethod());
|
||||
errorModel.setErrorType("error-message");
|
||||
errorModel.setExceptionContent(exceptionContent);
|
||||
errorModel.setMessage(ex.messageToBase64());
|
||||
errorModel.setResponse(ex.webcastResponseToBase64());
|
||||
} else {
|
||||
errorModel.setHostName(tiktokUser);
|
||||
errorModel.setErrorName(exception.getClass().getSimpleName());
|
||||
errorModel.setErrorType("error-system");
|
||||
errorModel.setExceptionContent(exceptionContent);
|
||||
errorModel.setMessage("");
|
||||
errorModel.setResponse("");
|
||||
}
|
||||
|
||||
|
||||
tikTokDatabase.insertError(errorModel);
|
||||
System.out.println("ERROR: " + errorModel.getErrorName());
|
||||
exception.printStackTrace();
|
||||
|
||||
})
|
||||
.buildAndRunAsync();
|
||||
TikTokMessageCollectorClient.create("messageCollector")
|
||||
.addUser("bangbetmenygy")
|
||||
.addUser("mr_cios")
|
||||
.addUser("sleepstreamxxx")
|
||||
.addUser("psychotropnazywo")
|
||||
.addUser("accordionistka")
|
||||
.buildAndRun();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* 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.tools.collector.client;
|
||||
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse;
|
||||
import io.github.jwdeveloper.tiktok.utils.FilesUtility;
|
||||
import io.github.jwdeveloper.tiktok.utils.JsonUtil;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class MessageCollector {
|
||||
|
||||
@Getter
|
||||
Map<String, Queue<MessageData>> messages;
|
||||
String outputName;
|
||||
|
||||
int limit = 20;
|
||||
public MessageCollector(String outputName) {
|
||||
this.messages = new TreeMap<>();
|
||||
this.outputName = outputName;
|
||||
load();
|
||||
}
|
||||
|
||||
public void addMessage(Logger logger, String host, WebcastResponse.Message message) {
|
||||
var name = message.getMethod();
|
||||
var payload = message.getPayload().toByteArray();
|
||||
var base64 = Base64.getEncoder().encodeToString(payload);
|
||||
|
||||
if (!messages.containsKey(name)) {
|
||||
logger.info("New Message found! " + name);
|
||||
messages.put(name, new LinkedList<>());
|
||||
}
|
||||
|
||||
var queue = messages.get(name);
|
||||
if (queue.size() > limit) {
|
||||
queue.poll();
|
||||
}
|
||||
|
||||
queue.add(new MessageData(base64, host, LocalDateTime.now().toString()));
|
||||
save();
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
return JsonUtil.toJson(messages);
|
||||
}
|
||||
|
||||
public void load() {
|
||||
var file = new File(path());
|
||||
Type type = new TypeToken<Map<String, Queue<MessageData>>>() {}.getType();
|
||||
|
||||
if (file.exists()) {
|
||||
var content = FilesUtility.loadFileContent(path());
|
||||
var gson = new GsonBuilder().create();
|
||||
messages = gson.fromJson(content,type);
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
|
||||
FilesUtility.saveFile(path(), toJson());
|
||||
}
|
||||
|
||||
public String path() {
|
||||
return Paths.get("C:\\Users\\ja\\IdeaProjects\\TikTokLiveJava\\Tools-EventsCollector\\src\\main\\resources", outputName + ".json").toString();
|
||||
}
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public class MessageData {
|
||||
String eventData;
|
||||
String uniqueId;
|
||||
String ts;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* 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.tools.collector.client;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.TikTokLive;
|
||||
import io.github.jwdeveloper.tiktok.TikTokLiveClientBuilder;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveMessageException;
|
||||
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
||||
import io.github.jwdeveloper.tiktok.tools.collector.db.TikTokDatabase;
|
||||
import io.github.jwdeveloper.tiktok.tools.collector.tables.ExceptionInfoModel;
|
||||
import io.github.jwdeveloper.tiktok.tools.collector.tables.TikTokErrorModel;
|
||||
import io.github.jwdeveloper.tiktok.tools.collector.tables.TikTokMessageModel;
|
||||
import io.github.jwdeveloper.tiktok.tools.collector.tables.TikTokResponseModel;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class TikTokClientFactory {
|
||||
private final MessageCollector messageCollector;
|
||||
private final TikTokDatabase tikTokDatabase;
|
||||
|
||||
public TikTokClientFactory(MessageCollector messageCollector, TikTokDatabase tikTokDatabase) {
|
||||
this.messageCollector = messageCollector;
|
||||
this.tikTokDatabase = tikTokDatabase;
|
||||
}
|
||||
|
||||
public CompletableFuture<LiveClient> runClientAsync(String tiktokUser, Consumer<TikTokLiveClientBuilder> onBuilder) {
|
||||
var builder = TikTokLive.newClient(tiktokUser);
|
||||
onBuilder.accept(builder);
|
||||
return builder.onConnected((liveClient, event) ->
|
||||
{
|
||||
liveClient.getLogger().info("CONNECTED TO " + liveClient.getRoomInfo().getHostName());
|
||||
})
|
||||
.onWebsocketResponse((liveClient, event) ->
|
||||
{
|
||||
var response = Base64.getEncoder().encodeToString(event.getResponse().toByteArray());
|
||||
|
||||
var responseModel = new TikTokResponseModel();
|
||||
responseModel.setResponse(response);
|
||||
responseModel.setHostName(liveClient.getRoomInfo().getHostName());
|
||||
tikTokDatabase.insertResponse(responseModel);
|
||||
liveClient.getLogger().info("Response");
|
||||
for (var message : event.getResponse().getMessagesList()) {
|
||||
messageCollector.addMessage(liveClient.getLogger(), liveClient.getRoomInfo().getHostName(), message);
|
||||
}
|
||||
})
|
||||
.onWebsocketMessage((liveClient, event) ->
|
||||
{
|
||||
var eventName = event.getEvent().getClass().getSimpleName();
|
||||
|
||||
/*
|
||||
if (filter.size() != 0 && !filter.contains(event.getEvent().getClass())) {
|
||||
return;
|
||||
}*/
|
||||
|
||||
var messageBinary = Base64.getEncoder().encodeToString(event.getMessage().toByteArray());
|
||||
var model = new TikTokMessageModel();
|
||||
model.setType("messsage");
|
||||
model.setHostName(tiktokUser);
|
||||
model.setEventName(eventName);
|
||||
model.setMessage(messageBinary);
|
||||
|
||||
// tikTokDatabase.insertMessage(model);
|
||||
liveClient.getLogger().info("EVENT: [" + tiktokUser + "] " + eventName);
|
||||
})
|
||||
.onError((liveClient, event) ->
|
||||
{
|
||||
event.getException().printStackTrace();
|
||||
var exception = event.getException();
|
||||
var exceptionContent = ExceptionInfoModel.getStackTraceAsString(exception);
|
||||
var errorModel = new TikTokErrorModel();
|
||||
if (exception instanceof TikTokLiveMessageException ex) {
|
||||
errorModel.setHostName(tiktokUser);
|
||||
errorModel.setErrorName(ex.messageMethod());
|
||||
errorModel.setErrorType("error-message");
|
||||
errorModel.setExceptionContent(exceptionContent);
|
||||
errorModel.setMessage(ex.messageToBase64());
|
||||
errorModel.setResponse(ex.webcastResponseToBase64());
|
||||
} else {
|
||||
errorModel.setHostName(tiktokUser);
|
||||
errorModel.setErrorName(exception.getClass().getSimpleName());
|
||||
errorModel.setErrorType("error-system");
|
||||
errorModel.setExceptionContent(exceptionContent);
|
||||
errorModel.setMessage("");
|
||||
errorModel.setResponse("");
|
||||
}
|
||||
|
||||
|
||||
tikTokDatabase.insertError(errorModel);
|
||||
liveClient.getLogger().info("ERROR: " + errorModel.getErrorName());
|
||||
exception.printStackTrace();
|
||||
|
||||
})
|
||||
.buildAndConnectAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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.tools.collector.client;
|
||||
|
||||
public class TikTokMessageCollectorClient
|
||||
{
|
||||
public static TikTokMessagessCollectorBuilder create(String outputName)
|
||||
{
|
||||
return new TikTokMessagessCollectorBuilder(outputName);
|
||||
}
|
||||
|
||||
public static TikTokMessagessCollectorBuilder create(MessageCollector messageCollector, String outputName)
|
||||
{
|
||||
return new TikTokMessagessCollectorBuilder(outputName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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.tools.collector.client;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.TikTokLiveClientBuilder;
|
||||
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
||||
import io.github.jwdeveloper.tiktok.tools.collector.db.TikTokDatabase;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class TikTokMessagessCollectorBuilder {
|
||||
List<String> users;
|
||||
String outputFileName;
|
||||
List<Class<?>> filters;
|
||||
Consumer<TikTokLiveClientBuilder> onBuilder;
|
||||
List<LiveClient> tiktokclients;
|
||||
|
||||
MessageCollector messageCollector;
|
||||
|
||||
public TikTokMessagessCollectorBuilder(String outputName) {
|
||||
users = new ArrayList<>();
|
||||
outputFileName = outputName;
|
||||
filters = new ArrayList<>();
|
||||
onBuilder = (e) -> {
|
||||
};
|
||||
tiktokclients = new ArrayList<>();
|
||||
messageCollector = new MessageCollector(outputName);
|
||||
}
|
||||
|
||||
public TikTokMessagessCollectorBuilder(MessageCollector messageCollector, String outputFileName) {
|
||||
this(outputFileName);
|
||||
this.messageCollector = messageCollector;
|
||||
}
|
||||
|
||||
public TikTokMessagessCollectorBuilder setOutputName(String name) {
|
||||
outputFileName = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public TikTokMessagessCollectorBuilder addOnBuilder(Consumer<TikTokLiveClientBuilder> consumer) {
|
||||
onBuilder = consumer;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokMessagessCollectorBuilder addUser(String user) {
|
||||
users.add(user);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokMessagessCollectorBuilder addEventFilter(Class<?> event) {
|
||||
filters.add(event);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MessageCollector buildAndRun() throws SQLException {
|
||||
var db = new TikTokDatabase(outputFileName);
|
||||
db.init();
|
||||
var factory = new TikTokClientFactory(messageCollector, db);
|
||||
for (var user : users) {
|
||||
var client = factory.runClientAsync(user, onBuilder);
|
||||
client.thenAccept(liveClient ->
|
||||
{
|
||||
tiktokclients.add(liveClient);
|
||||
});
|
||||
}
|
||||
return messageCollector;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
for (var client : tiktokclients) {
|
||||
client.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse;
|
||||
import io.github.jwdeveloper.tiktok.mockClient.TikTokClientMock;
|
||||
import io.github.jwdeveloper.tiktok.tools.collector.db.TikTokDatabase;
|
||||
import io.github.jwdeveloper.tiktok.tools.collector.tables.TikTokResponseModel;
|
||||
import io.github.jwdeveloper.tiktok.tools.util.MessageUtil;
|
||||
import io.github.jwdeveloper.tiktok.utils.ConsoleColors;
|
||||
import io.github.jwdeveloper.tiktok.utils.JsonUtil;
|
||||
|
||||
@@ -49,54 +50,28 @@ public class RunDbTester {
|
||||
.addResponses(responses)
|
||||
.onWebsocketUnhandledMessage((liveClient, event) ->
|
||||
{
|
||||
liveClient.getLogger().info("Unhandled Message! " + event.getData().getMethod());
|
||||
var sb = new StringBuilder();
|
||||
sb.append("Unhandled Message! " );
|
||||
sb.append(event.getData().getMethod());
|
||||
sb.append(MessageUtil.getContent(event.getData()));
|
||||
|
||||
liveClient.getLogger().info(sb.toString());
|
||||
})
|
||||
.onWebsocketMessage((liveClient, event) ->
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.append(event.getEvent().getClass().getSimpleName());
|
||||
sb.append(event.getEvent().toJson());
|
||||
// sb.append(ConsoleColors.YELLOW + messageContent(event.getMessage()));
|
||||
|
||||
liveClient.getLogger().info(sb.toString());
|
||||
liveClient.getLogger().fine(sb.toString());
|
||||
})
|
||||
.build();
|
||||
|
||||
updateLogger(client.getLogger());
|
||||
|
||||
client.connect();
|
||||
}
|
||||
|
||||
protected static String messageContent(WebcastResponse.Message message) {
|
||||
try {
|
||||
|
||||
var methodName = message.getMethod();
|
||||
if (!methodName.contains("Webcast")) {
|
||||
methodName = "Webcast" + methodName;
|
||||
}
|
||||
var inputClazz = Class.forName("io.github.jwdeveloper.tiktok.messages.webcast." + methodName);
|
||||
var parseMethod = inputClazz.getDeclaredMethod("parseFrom", ByteString.class);
|
||||
var deserialized = parseMethod.invoke(null, message.getPayload());
|
||||
return JsonUtil.messageToJson(deserialized);
|
||||
} catch (Exception ex) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static void updateLogger(Logger logger) {
|
||||
for (var handler : logger.getHandlers()) {
|
||||
logger.removeHandler(handler);
|
||||
}
|
||||
|
||||
var handler = new ConsoleHandler();
|
||||
handler.setFormatter(new Formatter() {
|
||||
@Override
|
||||
public String format(LogRecord record) {
|
||||
return ConsoleColors.WHITE_BRIGHT + record.getLevel() + ": " + ConsoleColors.GREEN + record.getMessage() + "\n" + ConsoleColors.RESET;
|
||||
}
|
||||
});
|
||||
logger.setUseParentHandlers(false);
|
||||
logger.addHandler(handler);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -23,32 +23,37 @@
|
||||
package io.github.jwdeveloper.tiktok.tools.tester;
|
||||
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastLinkMicFanTicketMethod;
|
||||
import io.github.jwdeveloper.tiktok.mockClient.TikTokClientMock;
|
||||
import io.github.jwdeveloper.tiktok.mockClient.mocks.LiveClientMock;
|
||||
import io.github.jwdeveloper.tiktok.tools.util.MessageUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
public class RunLogTester {
|
||||
public class RunJsonTester {
|
||||
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
|
||||
var input ="CjwKHVdlYmNhc3RMaW5rTWljRmFuVGlja2V0TWV0aG9kEIWWp7ig2O6OZRiGlviyrNjpjmUgza2G4a8xMAESZwoNCIWIouKYhLqBYhCbARCbASpTaHR0cHM6Ly9wMTYtd2ViY2FzdC50aWt0b2tjZG4uY29tL2ltZy93ZWJjYXN0LXNnL3Rpa3Rva19saW5rbWljX2NvaW5AM3gucG5nfjB4MC5wbmc=";
|
||||
var bytes = Base64.getDecoder().decode(input);
|
||||
var a= WebcastLinkMicFanTicketMethod.parseFrom(bytes);
|
||||
|
||||
|
||||
|
||||
var messages = getMessages();
|
||||
var client = TikTokClientMock.create().build();
|
||||
var client =(LiveClientMock) TikTokClientMock.create()
|
||||
.onWebsocketUnhandledMessage((liveClient, event) ->
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.append("Unhandled Message! " );
|
||||
sb.append(event.getData().getMethod());
|
||||
sb.append("\n");
|
||||
sb.append(MessageUtil.getContent(event.getData()));
|
||||
|
||||
|
||||
liveClient.getLogger().info(sb.toString());
|
||||
})
|
||||
.onError((liveClient, event) ->
|
||||
{
|
||||
event.getException().printStackTrace();
|
||||
})
|
||||
.build();
|
||||
for(var msg : messages.entrySet())
|
||||
{
|
||||
for(var content : msg.getValue())
|
||||
@@ -72,7 +77,6 @@ public class RunLogTester {
|
||||
var messages = jsonObject.get(key).getAsJsonArray();
|
||||
for (var msg : messages) {
|
||||
var data = msg.getAsJsonObject().get("eventData").getAsString();
|
||||
// System.out.println("KEY: " + key + " DATA: " + data);
|
||||
res.computeIfAbsent(key, s -> new ArrayList<>()).add(data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.tools.util;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse;
|
||||
import io.github.jwdeveloper.tiktok.utils.ConsoleColors;
|
||||
import io.github.jwdeveloper.tiktok.utils.JsonUtil;
|
||||
|
||||
public class MessageUtil
|
||||
{
|
||||
public static String getContent(WebcastResponse.Message message) {
|
||||
try {
|
||||
var methodName = message.getMethod();
|
||||
var inputClazz = Class.forName("io.github.jwdeveloper.tiktok.messages.webcast." + methodName);
|
||||
var parseMethod = inputClazz.getDeclaredMethod("parseFrom", ByteString.class);
|
||||
var deserialized = parseMethod.invoke(null, message.getPayload());
|
||||
return JsonUtil.messageToJson(deserialized);
|
||||
} catch (Exception ex) {
|
||||
return ConsoleColors.RED+ "Can not find mapper for "+message.getMethod();
|
||||
}
|
||||
}
|
||||
|
||||
public static String getContent(String methodName, byte[] bytes) {
|
||||
try {
|
||||
|
||||
var inputClazz = Class.forName("io.github.jwdeveloper.tiktok.messages.webcast." + methodName);
|
||||
var parseMethod = inputClazz.getDeclaredMethod("parseFrom", byte[].class);
|
||||
var deserialized = parseMethod.invoke(null, bytes);
|
||||
return JsonUtil.messageToJson(deserialized);
|
||||
} catch (Exception ex) {
|
||||
return ConsoleColors.RED+ "Can not find mapper for "+methodName;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user