Update gifts manager

This commit is contained in:
JW
2024-02-25 21:29:21 +01:00
parent 63dd8c20ac
commit 0fcac60cbe
12 changed files with 30 additions and 29 deletions

View File

@@ -33,6 +33,13 @@ import java.util.logging.Level;
@Data @Data
public class LiveClientSettings { public class LiveClientSettings {
/**
* Determines if gifts data is downloaded before TikTokLive starts,
* when `false` then client.giftManager() does not contain initial gifts
*/
private boolean fetchGifts;
/** /**
* ISO-Language for Client * ISO-Language for Client
*/ */

View File

@@ -93,6 +93,7 @@ public class TikTokLive {
} }
//I don't like it, but it is reasonable for now
private static GiftsManager giftsManager; private static GiftsManager giftsManager;
/** /**
@@ -100,21 +101,18 @@ public class TikTokLive {
* *
* @return GiftsManager * @return GiftsManager
*/ */
public static GiftsManager gifts() public static GiftsManager gifts() {
{ if (giftsManager != null) {
if(giftsManager != null)
{
return giftsManager; return giftsManager;
} }
synchronized (GiftsManager.class)
try
{ {
giftsManager = new TikTokGiftsManager(requests().fetchGiftsData().getGifts()); if (giftsManager == null)
return giftsManager; {
} catch (Exception ex) return new TikTokGiftsManager(requests().fetchGiftsData().getGifts());
{ }
throw ex;
} }
return giftsManager;
} }

View File

@@ -58,6 +58,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
protected final List<TikTokEventListener> listeners; protected final List<TikTokEventListener> listeners;
protected Consumer<TikTokMapper> onCustomMappings; protected Consumer<TikTokMapper> onCustomMappings;
protected Logger logger; protected Logger logger;
protected GiftsManager giftsManager;
public TikTokLiveClientBuilder(String userName) { public TikTokLiveClientBuilder(String userName) {
this.clientSettings = LiveClientSettings.createDefault(); this.clientSettings = LiveClientSettings.createDefault();
@@ -99,6 +100,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
httpSettings.getParams().put("webcast_language", clientSettings.getClientLanguage()); httpSettings.getParams().put("webcast_language", clientSettings.getClientLanguage());
this.logger = LoggerFactory.create(clientSettings.getHostName(), clientSettings); this.logger = LoggerFactory.create(clientSettings.getHostName(), clientSettings);
this.giftsManager = clientSettings.isFetchGifts() ? TikTokLive.gifts() : new TikTokGiftsManager(List.of());
} }
public LiveClient build() { public LiveClient build() {
@@ -112,8 +114,6 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
var httpClientFactory = new HttpClientFactory(clientSettings); var httpClientFactory = new HttpClientFactory(clientSettings);
var tikTokLiveHttpClient = new TikTokLiveHttpClient(httpClientFactory, clientSettings); var tikTokLiveHttpClient = new TikTokLiveHttpClient(httpClientFactory, clientSettings);
var gifts = tikTokLiveHttpClient.getGiftsData().getGifts();
var giftsManager = new TikTokGiftsManager(gifts);
var eventsMapper = createMapper(giftsManager, tiktokRoomInfo); var eventsMapper = createMapper(giftsManager, tiktokRoomInfo);
var messageHandler = new TikTokLiveMessageHandler(tikTokEventHandler, eventsMapper); var messageHandler = new TikTokLiveMessageHandler(tikTokEventHandler, eventsMapper);

View File

@@ -14,7 +14,8 @@ import java.util.stream.Collectors;
public class TikTokGiftsManager implements GiftsManager { public class TikTokGiftsManager implements GiftsManager {
private final Map<Integer, Gift> giftsByIdIndex; private final Map<Integer, Gift> giftsByIdIndex;
public TikTokGiftsManager(List<Gift> giftList) { public TikTokGiftsManager(List<Gift> giftList)
{
giftsByIdIndex = giftList.stream().collect(Collectors.toConcurrentMap(Gift::getId, e -> e)); giftsByIdIndex = giftList.stream().collect(Collectors.toConcurrentMap(Gift::getId, e -> e));
} }

View File

@@ -23,7 +23,6 @@
package io.github.jwdeveloper.tiktok; package io.github.jwdeveloper.tiktok;
import io.github.jwdeveloper.tiktok.extension.collector.TikTokLiveCollector; import io.github.jwdeveloper.tiktok.extension.collector.TikTokLiveCollector;
import java.io.IOException; import java.io.IOException;
@@ -49,7 +48,7 @@ public class CollectorExample {
collector.connectDatabase(); collector.connectDatabase();
var users = List.of("tehila_723", "dino123597", "domaxyzx", "dash4214", "obserwacje_live"); var users = List.of("tehila_723", "dino123597", "domaxyzx", "dash4214", "obserwacje_live");
var sessionTag = "Tag1"; Map<String, Object> additionalDataFields = Map.of("sessionTag", "ExampleTag");
for (var user : users) { for (var user : users) {
TikTokLive.newClient(user) TikTokLive.newClient(user)
.configure(liveClientSettings -> .configure(liveClientSettings ->
@@ -60,8 +59,9 @@ public class CollectorExample {
{ {
event.getException().printStackTrace(); event.getException().printStackTrace();
}) })
.addListener(collector.newListener(Map.of("sessionTag", sessionTag), document -> .addListener(collector.newListener(additionalDataFields, document ->
{ {
//filtering document data before it is inserted to database
if (document.get("dataType") == "message") { if (document.get("dataType") == "message") {
return false; return false;
} }

View File

@@ -44,7 +44,6 @@ public class CustomEventExample {
{ {
clientSettings.setPrintToConsole(true); clientSettings.setPrintToConsole(true);
}) })
.onGift((liveClient, event) -> .onGift((liveClient, event) ->
{ {
if (event.getGift().getDiamondCost() > 100) if (event.getGift().getDiamondCost() > 100)

View File

@@ -29,6 +29,8 @@ public class GiftsExample {
public static void main(String[] args) { public static void main(String[] args) {
var giftsManager = TikTokLive.gifts(); var giftsManager = TikTokLive.gifts();
var giftsList = giftsManager.toList(); var giftsList = giftsManager.toList();
for (var gift : giftsList) { for (var gift : giftsList) {
System.out.println("Gift: " + gift); System.out.println("Gift: " + gift);

View File

@@ -32,12 +32,13 @@ import java.time.Duration;
import java.util.logging.Level; import java.util.logging.Level;
public class SimpleExample { public class SimpleExample {
public static String TIKTOK_HOSTNAME = "dash4114"; public static String TIKTOK_HOSTNAME = "kvadromama_marina1";
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
showLogo(); showLogo();
var gifts = TikTokLive.gifts();
TikTokLive.newClient(SimpleExample.TIKTOK_HOSTNAME) TikTokLive.newClient(SimpleExample.TIKTOK_HOSTNAME)
.configure(clientSettings -> .configure(clientSettings ->

View File

@@ -52,7 +52,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.github.jwdeveloper.tiktok</groupId> <groupId>io.github.jwdeveloper.tiktok</groupId>
<artifactId>Tools</artifactId> <artifactId>Tools-ReadmeGenerator</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>

View File

@@ -24,8 +24,8 @@ package io.github.jwdeveloper.tiktok.tools.collector.client;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import io.github.jwdeveloper.tiktok.FilesUtility;
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse; import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse;
import io.github.jwdeveloper.tiktok.utils.FilesUtility;
import io.github.jwdeveloper.tiktok.utils.JsonUtil; import io.github.jwdeveloper.tiktok.utils.JsonUtil;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;

View File

@@ -23,12 +23,6 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>io.github.jwdeveloper.tiktok</groupId>
<artifactId>Tools</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>com.github.jwdeveloper.Descrabble</groupId> <groupId>com.github.jwdeveloper.Descrabble</groupId>
<artifactId>Descrabble-Full</artifactId> <artifactId>Descrabble-Full</artifactId>
@@ -44,7 +38,7 @@
<dependency> <dependency>
<groupId>io.github.jwdeveloper.tiktok</groupId> <groupId>io.github.jwdeveloper.tiktok</groupId>
<artifactId>Client</artifactId> <artifactId>Client</artifactId>
<version>1.3.0-Release</version> <version>${project.version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@@ -22,7 +22,6 @@
*/ */
package io.github.jwdeveloper.tiktok; package io.github.jwdeveloper.tiktok;
import io.github.jwdeveloper.tiktok.utils.FilesUtility;
import java.util.regex.Pattern; import java.util.regex.Pattern;