Merge pull request #16 from jwdeveloper/develop-1-0-4

Changes:
   Generated new Gifts Json
   
   TikTokLive.isLiveOnline() check if live if online
   TikTokLive.isLiveOnlineAsync()
   
   TikTokLive.isHostNameValid() check if hostName is correct
   TikTokLive.isHostNameValidAsync()
This commit is contained in:
Jacek W
2023-11-10 22:28:01 +01:00
committed by GitHub
8 changed files with 88 additions and 28 deletions

View File

@@ -73,7 +73,7 @@ public interface EventsBuilder<T> {
T onShare(EventConsumer<TikTokShareEvent> event); T onShare(EventConsumer<TikTokShareEvent> event);
T onUnhandledSocial(EventConsumer<TikTokUnhandledSocialEvent> event); T onUnhandledSocial(EventConsumer<TikTokUnhandledSocialEvent> event);
T onChestOpen(EventConsumer<TikTokChestEvent> event); // T onChest(EventConsumer<TikTokChestEvent> event);
T onLivePaused(EventConsumer<TikTokLivePausedEvent> event); T onLivePaused(EventConsumer<TikTokLivePausedEvent> event);

View File

@@ -23,25 +23,68 @@
package io.github.jwdeveloper.tiktok; package io.github.jwdeveloper.tiktok;
import io.github.jwdeveloper.tiktok.http.TikTokLiveOnlineChecker; import io.github.jwdeveloper.tiktok.http.TikTokDataChecker;
import io.github.jwdeveloper.tiktok.live.builder.LiveClientBuilder; import io.github.jwdeveloper.tiktok.live.builder.LiveClientBuilder;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
public class TikTokLive public class TikTokLive
{ {
/**
*
* @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) public static LiveClientBuilder newClient(String hostName)
{ {
return new TikTokLiveClientBuilder(hostName); return new TikTokLiveClientBuilder(hostName);
} }
/**
*
* @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) public static boolean isLiveOnline(String hostName)
{ {
return new TikTokLiveOnlineChecker().isOnline(hostName); return new TikTokDataChecker().isOnline(hostName);
} }
/**
*
* @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<Boolean> isLiveOnlineAsync(String hostName) public static CompletableFuture<Boolean> isLiveOnlineAsync(String hostName)
{ {
return new TikTokLiveOnlineChecker().isOnlineAsync(hostName); return new TikTokDataChecker().isOnlineAsync(hostName);
}
/**
*
* @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)
{
return new TikTokDataChecker().isHostNameValid(hostName);
}
/**
*
* @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<Boolean> isHostNameValidAsync(String hostName)
{
return new TikTokDataChecker().isHostNameValidAsync(hostName);
} }
} }

View File

@@ -192,8 +192,8 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
return this; return this;
} }
@Override // @Override
public LiveClientBuilder onChestOpen(EventConsumer<TikTokChestEvent> event) { public LiveClientBuilder onChest(EventConsumer<TikTokChestEvent> event) {
tikTokEventHandler.subscribe(TikTokChestEvent.class, event); tikTokEventHandler.subscribe(TikTokChestEvent.class, event);
return this; return this;
} }

View File

@@ -70,16 +70,6 @@ public class TikTokGiftManager implements GiftManager {
field.set(enumInstance, name); field.set(enumInstance, name);
Arrays.stream(Gift.class.getSuperclass().getDeclaredFields()).toList().forEach(field1 ->
{
System.out.println(field1.getName()+" ");
});
field = Gift.class.getSuperclass().getDeclaredField("name");
field.setAccessible(true);
field.set(enumInstance,"dupa");
// EnumSet // EnumSet
field = Gift.class.getDeclaredField("diamondCost"); field = Gift.class.getDeclaredField("diamondCost");
field.setAccessible(true); field.setAccessible(true);

View File

@@ -1,18 +1,21 @@
package io.github.jwdeveloper.tiktok.http; package io.github.jwdeveloper.tiktok.http;
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException; import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException;
import io.github.jwdeveloper.tiktok.live.LiveClient;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class TikTokLiveOnlineChecker public class TikTokDataChecker
{ {
public CompletableFuture<Boolean> isOnlineAsync(String hostName) { public CompletableFuture<Boolean> isOnlineAsync(String hostName) {
return CompletableFuture.supplyAsync(() -> isOnline(hostName)); return CompletableFuture.supplyAsync(() -> isOnline(hostName));
} }
public CompletableFuture<Boolean> isHostNameValidAsync(String hostName) {
return CompletableFuture.supplyAsync(() -> isOnline(hostName));
}
public boolean isOnline(String hostName) { public boolean isOnline(String hostName) {
var factory = new TikTokHttpRequestFactory(new TikTokCookieJar()); var factory = new TikTokHttpRequestFactory(new TikTokCookieJar());
var url = getLiveUrl(hostName); var url = getLiveUrl(hostName);
@@ -26,10 +29,28 @@ public class TikTokLiveOnlineChecker
} }
} }
public boolean isHostNameValid(String hostName) {
var factory = new TikTokHttpRequestFactory(new TikTokCookieJar());
var url = getProfileUrl(hostName);
try {
var response = factory.get(url);
var titleContent = extractTitleContent(response);
return isTitleHostNameValid(titleContent, hostName);
} catch (Exception e)
{
throw new TikTokLiveRequestException("Unable to make check host name valid request",e);
}
}
private boolean isTitleLiveOnline(String title) { private boolean isTitleLiveOnline(String title) {
return title.contains("is LIVE"); return title.contains("is LIVE");
} }
private boolean isTitleHostNameValid(String title, String hostName)
{
return title.contains(hostName);
}
private String extractTitleContent(String html) { private String extractTitleContent(String html) {
var regex = "<title\\b[^>]*>(.*?)<\\/title>"; var regex = "<title\\b[^>]*>(.*?)<\\/title>";
var pattern = Pattern.compile(regex); var pattern = Pattern.compile(regex);
@@ -48,4 +69,11 @@ public class TikTokLiveOnlineChecker
sb.append("/live"); sb.append("/live");
return sb.toString(); return sb.toString();
} }
private String getProfileUrl(String hostName) {
var sb = new StringBuilder();
sb.append("https://www.tiktok.com/@");
sb.append(hostName);
return sb.toString();
}
} }

View File

@@ -46,7 +46,7 @@ public class TikTokGiftManagerTest {
var gifts = giftManager.getGifts(); var gifts = giftManager.getGifts();
var optional = gifts.stream().filter(r -> r == fakeGift).findFirst(); var optional = gifts.stream().filter(r -> r == fakeGift).findFirst();
Assertions.assertTrue(optional.isPresent()); Assertions.assertTrue(optional.isPresent());
Assertions.assertNotNull(optional.get().name()); // Assertions.assertNotNull(optional.get().name());
} }
@Test @Test

View File

@@ -3,15 +3,13 @@ package io.github.jwdeveloper.tiktok.http;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class TikTokLiveOnlineCheckerTest { public class TikTokLiveOnlineCheckerTest {
private final String TARGET_USER = "bangbetmenygy"; private final String TARGET_USER = "bangbetmenygy";
@Test @Test
public void shouldTestOnline() { public void shouldTestOnline() {
var sut = new TikTokLiveOnlineChecker(); var sut = new TikTokDataChecker();
var result = sut.isOnline(TARGET_USER); var result = sut.isOnline(TARGET_USER);
Assertions.assertTrue(result); Assertions.assertTrue(result);

View File

@@ -22,8 +22,7 @@
*/ */
package io.github.jwdeveloper.tiktok; package io.github.jwdeveloper.tiktok;
import io.github.jwdeveloper.tiktok.data.models.Picture; import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveOfflineHostException;
import io.github.jwdeveloper.tiktok.data.models.gifts.Gift;
import io.github.jwdeveloper.tiktok.utils.ConsoleColors; import io.github.jwdeveloper.tiktok.utils.ConsoleColors;
import java.io.IOException; import java.io.IOException;
@@ -39,6 +38,12 @@ public class SimpleExample
// set tiktok username // set tiktok username
/* /*
Optional checking if host name is correct
if(TikTokLive.isHostNameValid(TIKTOK_HOSTNAME))
{
System.out.println("Live is online!");
}
Optional checking if live is online Optional checking if live is online
if(TikTokLive.isLiveOnline(TIKTOK_HOSTNAME)) if(TikTokLive.isLiveOnline(TIKTOK_HOSTNAME))
{ {
@@ -90,10 +95,6 @@ public class SimpleExample
{ {
print(ConsoleColors.RED,"[Disconnected]"); print(ConsoleColors.RED,"[Disconnected]");
}) })
.onChestOpen((liveClient, event) ->
{
print(ConsoleColors.GREEN,"Chest has been open by ",event.getUser().getName());
})
.onRoom((liveClient, event) -> .onRoom((liveClient, event) ->
{ {