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:
JW
2023-10-11 01:20:07 +02:00
parent 043dfe95d8
commit de27e71e93
64 changed files with 548 additions and 630 deletions

View File

@@ -0,0 +1,178 @@
/*
* 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;
import io.github.jwdeveloper.tiktok.annotations.EventMeta;
import io.github.jwdeveloper.tiktok.annotations.EventType;
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftEvent;
import io.github.jwdeveloper.tiktok.live.builder.EventsBuilder;
import io.github.jwdeveloper.tiktok.utils.FilesUtility;
import io.github.jwdeveloper.tiktok.utils.TemplateUtility;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.reflections.Reflections;
import java.util.*;
import java.util.regex.Pattern;
public class EventsInfoGenerator {
public static void main(String[] args) throws ClassNotFoundException {
var res = new EventsInfoGenerator().run();
System.out.println(res);
}
public String run() {
var events = getEventsDtos();
var builder = new StringBuilder();
for (var entry : events.entrySet()) {
builder.append(System.lineSeparator());
builder.append(System.lineSeparator());
builder.append(" **" + entry.getKey().name() + "**:").append(System.lineSeparator());
for (var dto : entry.getValue()) {
var link = getLink(dto);
builder.append(System.lineSeparator());
builder.append(link);
}
}
builder.append(System.lineSeparator());
builder.append("# Examples");
builder.append(System.lineSeparator());
for (var entry : events.entrySet()) {
for (var dto : entry.getValue()) {
builder.append("<br>");
builder.append(getMethodContent(dto));
}
}
return builder.toString();
}
public StringBuilder getLink(EventDto dto) {
var sb = new StringBuilder();
var name = dto.getMethodName().toLowerCase()+"-"+dto.getEventClazz().getSimpleName().toLowerCase();
sb.append("- [").append(dto.getMethodName()).append("](#").append(name).append(")");
return sb;
}
public String getMethodContent(EventDto dto) {
var variables = new HashMap<String, Object>();
var doc = getClazzDocumentation(dto.getEventClazz());
variables.put("method-name", dto.getMethodName());
variables.put("content", doc);
variables.put("event-name", dto.getEventClazz().getSimpleName());
var temp = """
#### {{method-name}} [{{event-name}}](https://github.com/jwdeveloper/TikTok-Live-Java/blob/master/API/src/main/java/io/github/jwdeveloper/tiktok/events/messages.java)
{{content}}
```java
TikTokLive.newClient("host-name")
.{{method-name}}((liveClient, event) ->
{
})
.buildAndConnect();
```
""";
return TemplateUtility.generateTemplate(temp, variables);
}
public String getClazzDocumentation(Class<?> clazz) {
var path = clazz.getName();
path = path.replace(".", "\\");
var fullPath = "C:\\Users\\ja\\IdeaProjects\\TikTokLiveJava\\API\\src\\main\\java\\" + path + ".java";
var content = FilesUtility.loadFileContent(fullPath);
var index = content.indexOf(" */");
content = content.substring(index+4);
String pattern = "(?s)\\/\\\\*(.*?)\\*\\/";
var r = Pattern.compile(pattern);
var m = r.matcher(content);
if (!m.find()) {
return "";
}
var group = m.group(1);
var reuslt = group.replace("*","").replaceAll("\\*", "");
return reuslt;
}
public Map<EventType, List<EventDto>> getEventsDtos(){
var result = new TreeMap<EventType, List<EventDto>>();
var baseClazz = EventsBuilder.class;
var reflections = new Reflections("io.github.jwdeveloper.tiktok.data.events");
var classes = reflections.getSubTypesOf(TikTokEvent.class);
var methods = baseClazz.getDeclaredMethods();
for (var method : methods) {
if (method.getName().equals("onEvent")) {
var dto = new EventDto(EventType.Message, "onEvent", TikTokEvent.class);
result.computeIfAbsent(EventType.Message, eventType -> new ArrayList<>()).add(dto);
continue;
}
var parsedName = method.getName().replaceFirst("on", "");
var name = "TikTok" + parsedName + "Event";
var optional = classes.stream().filter(e -> e.getSimpleName().equals(name)).findFirst();
if (optional.isEmpty()) {
System.out.println("Not found!: " + name);
continue;
}
var clazz = optional.get();
var annotation = clazz.getAnnotation(EventMeta.class);
var dto = new EventDto(annotation.eventType(), method.getName(), clazz);
result.computeIfAbsent(dto.eventType, eventType -> new ArrayList<>()).add(dto);
}
return result;
}
@AllArgsConstructor
@Getter
public static final class EventDto {
private EventType eventType;
private String methodName;
private Class eventClazz;
@Override
public String toString() {
return "EventDto{" +
"eventType=" + eventType +
", methodName='" + methodName + '\'' +
", eventClazz=" + eventClazz.getSimpleName() +
'}';
}
}
}

View File

@@ -1,82 +0,0 @@
/*
* 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;
import io.github.jwdeveloper.tiktok.annotations.EventMeta;
import io.github.jwdeveloper.tiktok.annotations.EventType;
import io.github.jwdeveloper.tiktok.data.events.common.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
}
}

View File

@@ -1,87 +0,0 @@
/*
* 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;
public class FullEventsExampleClass
{
public void run()
{
TikTokLive.newClient("username")
.onConnected((liveClient, event) ->
{
})
.onDisconnected((liveClient, event) ->
{
})
.onError((liveClient, event) ->
{
})
.onGift((liveClient, event) ->
{
})
.onLike((liveClient, event) ->
{
})
.onJoin((liveClient, event) ->
{
})
.onFollow((liveClient, event) ->
{
})
.onShare((liveClient, event) ->
{
})
.onRoomUserInfo((liveClient, event) ->
{
})
.onComment((liveClient, event) ->
{
})
.onLiveEnded((liveClient, event) ->
{
})
.onLivePaused((liveClient, event) ->
{
}).onEmote((liveClient, event) ->
{
});
}
}

View File

@@ -1,67 +0,0 @@
/*
* 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;
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

@@ -43,20 +43,14 @@ public class ReadmeGenerator
variables.put("version", getCurrentVersion());
var exampleCodePath = "C:\\Users\\ja\\IdeaProjects\\TikTokLiveJava\\TestApplication\\src\\main\\java\\io\\github\\jwdeveloper\\tiktok\\SimpleExample.java";
variables.put("Code-Example", getCodeExample(exampleCodePath));
var exampleCodePath = "C:\\Users\\ja\\IdeaProjects\\TikTokLiveJava\\Examples\\src\\main\\java\\io\\github\\jwdeveloper\\tiktok\\SimpleExample.java";
variables.put("code-content", getCodeExample(exampleCodePath));
variables.put("events-content", new EventsInfoGenerator().run());
var exampleConfigurationPath = "C:\\Users\\ja\\IdeaProjects\\TikTokLiveJava\\TestApplication\\src\\main\\java\\io\\github\\jwdeveloper\\tiktok\\ConfigurationExample.java";
variables.put("Configuration-Example", getCodeExample(exampleConfigurationPath));
var listenerExamplePath = "C:\\Users\\ja\\IdeaProjects\\TikTokLiveJava\\Examples\\src\\main\\java\\io\\github\\jwdeveloper\\tiktok\\ListenerExample.java";
// variables.put("listener-content", getCodeExample(listenerExamplePath));
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";
@@ -72,7 +66,9 @@ public class ReadmeGenerator
public String getCodeExample(String path)
{
return FilesUtility.loadFileContent(path);
var content = FilesUtility.loadFileContent(path);
content = content.substring(content.indexOf("*/")+2);
return content;
}
}

View File

@@ -51,38 +51,20 @@ Do you prefer other programming languages?
2. Create your first chat connection
```java
{{Code-Example}}
{{code-content}}
```
## Configuration
## Events
{{events-content}}
<br>
<br>
```java
{{Configuration-Example}}
```
## Listener Example
```java
{{Listener-Example}}
{{listener-content}}
```
## Methods
A `client (LiveClient)` object contains the following methods.
| Method Name | Description |
|---------------------| ----------- |
| connect | Connects to the live stream. |
| disconnect | Disconnects the connection. |
| getGiftManager | Gets the meta informations about all gifts. |
| getRoomInfo | Gets the current room info from TikTok API including streamer info, room status and statistics. |
| getListenersManager | Gets and manage TikTokEventListeners |
## Events
A `TikTokLive` object has the following events
{{Events}}
<br><br>
## Contributing
Your improvements are welcome! Feel free to open an <a href="https://github.com/jwdeveloper/TikTok-Live-Java/issues">issue</a> or <a href="https://github.com/jwdeveloper/TikTok-Live-Java/pulls">pull request</a>.