`TikTokEventListener` new method of listening events
   see it at TestApplication/ListenerExample.java

Bugs:
 - Fixed bug: Websocket was sending ping after it was closed
This commit is contained in:
JW
2023-09-07 03:19:25 +02:00
parent 911e2b12a5
commit 4a157143ec
99 changed files with 2558 additions and 762 deletions

View File

@@ -0,0 +1,75 @@
package io.github.jwdeveloper.tiktok;
import io.github.jwdeveloper.tiktok.annotations.EventMeta;
import io.github.jwdeveloper.tiktok.annotations.EventType;
import io.github.jwdeveloper.tiktok.events.TikTokEvent;
import org.reflections.Reflections;
import java.util.*;
public class EventsListGenerator
{
//[a](https://github.com/jwdeveloper/TikTok-Live-Java/blob/master/API/src/main/java/io/github/jwdeveloper/tiktok/events/messages/TikTokBarrageMessageEvent.java)
//Message Events:
//- [member](#member)
public static String GetEventsList()
{
var classes = getClasses();
var builder = new StringBuilder();
for(var entry : classes.entrySet())
{
builder.append(System.lineSeparator());
builder.append(" **"+entry.getKey().name()+"**:").append(System.lineSeparator());
for(var clazz : entry.getValue())
{
var name = clazz.getSimpleName();
var baseUrl ="https://github.com/jwdeveloper/TikTok-Live-Java/blob/master/API/src/main/java/io/github/jwdeveloper/tiktok/events/messages/"+name+".java";
builder.append("- [").append(name).append("](").append(baseUrl).append(")").append(System.lineSeparator());
}
}
return builder.toString();
}
private static Map<EventType, List<Class<?>>> getClasses()
{
Reflections reflections = new Reflections("io.github.jwdeveloper.tiktok.events.messages");
var classes = reflections.getSubTypesOf(TikTokEvent.class).stream().toList();
Map<EventType, List<Class<?>>> classMap = new HashMap<>();
// Group classes by EventType
for (Class<?> clazz : classes) {
EventType eventType = getEventType(clazz);
classMap.computeIfAbsent(eventType, k -> new ArrayList<>()).add(clazz);
}
return classMap;
}
private static EventType getEventType(Class<?> clazz) {
EventMeta annotation = clazz.getAnnotation(EventMeta.class);
if (annotation != null) {
return annotation.eventType();
}
return EventType.Custom; // Default value if annotation not present
}
private class EventTypeComparator implements Comparator<Class<?>> {
@Override
public int compare(Class<?> class1, Class<?> class2) {
EventType eventType1 = getEventType(class1);
EventType eventType2 = getEventType(class2);
return eventType1.compareTo(eventType2);
}
private EventType getEventType(Class<?> clazz) {
EventMeta annotation = clazz.getAnnotation(EventMeta.class);
if (annotation != null) {
return annotation.eventType();
}
return EventType.Custom;
}
}
}

View File

@@ -0,0 +1,45 @@
package io.github.jwdeveloper.tiktok;
import io.github.jwdeveloper.tiktok.utils.FilesUtility;
import java.util.regex.Pattern;
public class LiveClientMethodsGenerator
{
//| Method Name | Description |
//| ----------- | ----------- |
public static String generate(String path)
{
var sb = new StringBuilder();
sb.append("| Method Name | Description |");
sb.append(System.lineSeparator());
sb.append("| ----------- | ----------- |");
var content = FilesUtility.loadFileContent(path);
var pattern = "// \\s*(.*?)\\n\\s*(\\w+)\\s*\\(.*?\\)";
// Create a Pattern object
var regex = Pattern.compile(pattern);
// Create a Matcher object
var matcher = regex.matcher(content);
// Find and print method names and comments
while (matcher.find()) {
String comment = matcher.group(1);
String methodName = matcher.group(2);
sb.append(System.lineSeparator());
sb.append("| ").append(methodName).append(" | ").append(comment).append(" |");
}
sb.append(System.lineSeparator());
return sb.toString();
}
}

View File

@@ -0,0 +1,10 @@
package io.github.jwdeveloper.tiktok;
public class Main {
public static void main(String[] args)
{
var generator = new ReadmeGenerator();
generator.generate();
}
}

View File

@@ -0,0 +1,66 @@
package io.github.jwdeveloper.tiktok;
import io.github.jwdeveloper.tiktok.utils.FilesUtility;
import io.github.jwdeveloper.tiktok.utils.TemplateUtility;
import java.util.HashMap;
import java.util.regex.Pattern;
public class ReadmeGenerator
{
public void generate()
{
var template = FilesUtility.getFileFromResource(Main.class,"template.md");
var variables = new HashMap<String,Object>();
var pomPath = "C:\\Users\\ja\\IdeaProjects\\TikTokLiveJava\\Tools-ReadmeGenerator\\pom.xml";
variables.put("version", getCurrentVersion(pomPath));
var exampleCodePath = "C:\\Users\\ja\\IdeaProjects\\TikTokLiveJava\\TestApplication\\src\\main\\java\\io\\github\\jwdeveloper\\tiktok\\SimpleExample.java";
variables.put("Code-Example", getCodeExample(exampleCodePath));
var exampleConfigurationPath = "C:\\Users\\ja\\IdeaProjects\\TikTokLiveJava\\TestApplication\\src\\main\\java\\io\\github\\jwdeveloper\\tiktok\\ConfigurationExample.java";
variables.put("Configuration-Example", getCodeExample(exampleConfigurationPath));
variables.put("Events", EventsListGenerator.GetEventsList());
var listenerExamplePath = "C:\\Users\\ja\\IdeaProjects\\TikTokLiveJava\\TestApplication\\src\\main\\java\\io\\github\\jwdeveloper\\tiktok\\ListenerExample.java";
variables.put("Listener-Example", getCodeExample(listenerExamplePath));
// var liveClientPath = "C:\\Users\\ja\\IdeaProjects\\TikTokLiveJava\\API\\src\\main\\java\\io\\github\\jwdeveloper\\tiktok\\live\\LiveClient.java";
// variables.put("methods", LiveClientMethodsGenerator.generate(liveClientPath));
template = TemplateUtility.generateTemplate(template, variables);
var outputPath = "C:\\Users\\ja\\IdeaProjects\\TikTokLiveJava\\Tools-ReadmeGenerator\\src\\main\\resources\\output.md";
FilesUtility.saveFile(outputPath, template);
}
public String getCurrentVersion(String path)
{
var content = FilesUtility.loadFileContent(path);
var pattern = "<version>(.*?)<\\/version>";
// Create a Pattern object
var regex = Pattern.compile(pattern);
// Create a Matcher object
var matcher = regex.matcher(content);
// Find the first match
if (matcher.find()) {
// Extract and print the version
return matcher.group(1);
}
return "VERSION NOT FOUND";
}
public String getCodeExample(String path)
{
return FilesUtility.loadFileContent(path);
}
}