diff --git a/API/src/main/java/io/github/jwdeveloper/tiktok/live/builder/EventsBuilder.java b/API/src/main/java/io/github/jwdeveloper/tiktok/live/builder/EventsBuilder.java index b84ad3d..e672648 100644 --- a/API/src/main/java/io/github/jwdeveloper/tiktok/live/builder/EventsBuilder.java +++ b/API/src/main/java/io/github/jwdeveloper/tiktok/live/builder/EventsBuilder.java @@ -73,7 +73,7 @@ public interface EventsBuilder { T onShare(EventConsumer event); T onUnhandledSocial(EventConsumer event); - T onChestOpen(EventConsumer event); + T onChest(EventConsumer event); T onLivePaused(EventConsumer event); diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLive.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLive.java index aeadea8..42bd045 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLive.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLive.java @@ -23,25 +23,68 @@ 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 java.util.concurrent.CompletableFuture; 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) { 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) { - 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 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 isHostNameValidAsync(String hostName) + { + return new TikTokDataChecker().isHostNameValidAsync(hostName); } } diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClientBuilder.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClientBuilder.java index ba27c27..44c75c1 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClientBuilder.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClientBuilder.java @@ -193,7 +193,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder { } @Override - public LiveClientBuilder onChestOpen(EventConsumer event) { + public LiveClientBuilder onChest(EventConsumer event) { tikTokEventHandler.subscribe(TikTokChestEvent.class, event); return this; } diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/http/TikTokLiveOnlineChecker.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/http/TikTokDataChecker.java similarity index 59% rename from Client/src/main/java/io/github/jwdeveloper/tiktok/http/TikTokLiveOnlineChecker.java rename to Client/src/main/java/io/github/jwdeveloper/tiktok/http/TikTokDataChecker.java index d733f4c..9c3e74f 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/http/TikTokLiveOnlineChecker.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/http/TikTokDataChecker.java @@ -1,18 +1,21 @@ package io.github.jwdeveloper.tiktok.http; import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException; -import io.github.jwdeveloper.tiktok.live.LiveClient; import java.util.concurrent.CompletableFuture; import java.util.regex.Pattern; -public class TikTokLiveOnlineChecker +public class TikTokDataChecker { public CompletableFuture isOnlineAsync(String hostName) { return CompletableFuture.supplyAsync(() -> isOnline(hostName)); } + public CompletableFuture isHostNameValidAsync(String hostName) { + return CompletableFuture.supplyAsync(() -> isOnline(hostName)); + } + public boolean isOnline(String hostName) { var factory = new TikTokHttpRequestFactory(new TikTokCookieJar()); 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) { return title.contains("is LIVE"); } + private boolean isTitleHostNameValid(String title, String hostName) + { + return title.contains(hostName); + } + private String extractTitleContent(String html) { var regex = "]*>(.*?)<\\/title>"; var pattern = Pattern.compile(regex); @@ -48,4 +69,11 @@ public class TikTokLiveOnlineChecker sb.append("/live"); return sb.toString(); } + + private String getProfileUrl(String hostName) { + var sb = new StringBuilder(); + sb.append("https://www.tiktok.com/@"); + sb.append(hostName); + return sb.toString(); + } } diff --git a/Client/src/test/java/io/github/jwdeveloper/tiktok/http/TikTokLiveOnlineCheckerTest.java b/Client/src/test/java/io/github/jwdeveloper/tiktok/http/TikTokLiveOnlineCheckerTest.java index 52d8139..c82c969 100644 --- a/Client/src/test/java/io/github/jwdeveloper/tiktok/http/TikTokLiveOnlineCheckerTest.java +++ b/Client/src/test/java/io/github/jwdeveloper/tiktok/http/TikTokLiveOnlineCheckerTest.java @@ -3,15 +3,13 @@ package io.github.jwdeveloper.tiktok.http; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - public class TikTokLiveOnlineCheckerTest { private final String TARGET_USER = "bangbetmenygy"; @Test public void shouldTestOnline() { - var sut = new TikTokLiveOnlineChecker(); + var sut = new TikTokDataChecker(); var result = sut.isOnline(TARGET_USER); Assertions.assertTrue(result); diff --git a/Examples/src/main/java/io/github/jwdeveloper/tiktok/SimpleExample.java b/Examples/src/main/java/io/github/jwdeveloper/tiktok/SimpleExample.java index 85be3ae..78c7e70 100644 --- a/Examples/src/main/java/io/github/jwdeveloper/tiktok/SimpleExample.java +++ b/Examples/src/main/java/io/github/jwdeveloper/tiktok/SimpleExample.java @@ -22,8 +22,7 @@ */ package io.github.jwdeveloper.tiktok; -import io.github.jwdeveloper.tiktok.data.models.Picture; -import io.github.jwdeveloper.tiktok.data.models.gifts.Gift; +import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveOfflineHostException; import io.github.jwdeveloper.tiktok.utils.ConsoleColors; import java.io.IOException; @@ -39,6 +38,12 @@ public class SimpleExample // 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 if(TikTokLive.isLiveOnline(TIKTOK_HOSTNAME)) { @@ -90,7 +95,7 @@ public class SimpleExample { print(ConsoleColors.RED,"[Disconnected]"); }) - .onChestOpen((liveClient, event) -> + .onChest((liveClient, event) -> { print(ConsoleColors.GREEN,"Chest has been open by ",event.getUser().getName()); })