Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ public class TikTokUserInfo

String roomId;

long startTime;

public enum UserStatus
{
NotFound,
Offline,
LivePaused,
Live
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ public interface LiveRoomInfo
*/
int getTotalViewersCount();
int getLikesCount();
long getStartTime();
boolean isAgeRestricted();
String getRoomId();
String getHostName();
String getTitle();
User getHostUser();
List<RankingUser> getUsersRanking();
ConnectionState getConnectionState();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package io.github.jwdeveloper.tiktok;

import io.github.jwdeveloper.tiktok.data.dto.TikTokUserInfo;
import io.github.jwdeveloper.tiktok.data.events.TikTokDisconnectedEvent;
import io.github.jwdeveloper.tiktok.data.events.TikTokErrorEvent;
import io.github.jwdeveloper.tiktok.data.events.TikTokReconnectingEvent;
Expand Down Expand Up @@ -135,13 +136,15 @@ public void tryConnect() {

apiService.updateSessionId();

TikTokUserInfo info = apiService.fetchUserInfoFromTikTokApi(liveRoomInfo.getHostName());
liveRoomInfo.setStartTime(info.getStartTime());
if (clientSettings.getRoomId() != null) {
liveRoomInfo.setRoomId(clientSettings.getRoomId());
logger.info("Using roomID from settings: " + clientSettings.getRoomId());
} else {
var roomId = apiService.fetchRoomId(liveRoomInfo.getHostName());
liveRoomInfo.setRoomId(roomId);
liveRoomInfo.setRoomId(info.getRoomId());
}
apiService.updateRoomId(liveRoomInfo.getRoomId());


var liveRoomMeta = apiService.fetchRoomInfo();
Expand Down Expand Up @@ -194,4 +197,4 @@ public void publishEvent(TikTokEvent event)
{
tikTokEventHandler.publish(this, event);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ protected void validate() {
clientSettings.setTimeout(Duration.ofSeconds(Constants.DEFAULT_TIMEOUT));
}

if (clientSettings.getClientLanguage() == null || clientSettings.getClientLanguage().equals("")) {
if (clientSettings.getClientLanguage() == null || clientSettings.getClientLanguage().isEmpty()) {
clientSettings.setClientLanguage(Constants.DefaultClientSettings().getClientLanguage());
}


if (clientSettings.getHostName() == null || clientSettings.getHostName().equals("")) {
if (clientSettings.getHostName() == null || clientSettings.getHostName().isEmpty()) {
throw new TikTokLiveException("HostName can not be null");
}

Expand Down Expand Up @@ -552,11 +552,4 @@ public TikTokLiveClientBuilder onReconnecting(EventConsumer<TikTokReconnectingEv
tikTokEventHandler.subscribe(TikTokReconnectingEvent.class, event);
return this;
}
}







}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
*/
package io.github.jwdeveloper.tiktok;

import io.github.jwdeveloper.tiktok.data.models.Picture;
import io.github.jwdeveloper.tiktok.data.models.RankingUser;
import io.github.jwdeveloper.tiktok.data.models.users.User;
import io.github.jwdeveloper.tiktok.models.ConnectionState;
import io.github.jwdeveloper.tiktok.live.LiveRoomInfo;
import io.github.jwdeveloper.tiktok.models.ConnectionState;
import lombok.Data;

import java.util.LinkedList;
Expand All @@ -42,6 +41,8 @@ public class TikTokRoomInfo implements LiveRoomInfo {

private int totalViewersCount;

private long startTime;

private boolean ageRestricted;

private User host;
Expand Down Expand Up @@ -69,5 +70,4 @@ public void updateRanking(List<RankingUser> rankingUsers) {
usersRanking.clear();
usersRanking.addAll(rankingUsers);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,16 @@
*/
package io.github.jwdeveloper.tiktok.http;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.*;
import io.github.jwdeveloper.tiktok.ClientSettings;
import io.github.jwdeveloper.tiktok.data.dto.TikTokUserInfo;
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveOfflineHostException;
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException;
import io.github.jwdeveloper.tiktok.live.LiveRoomMeta;
import io.github.jwdeveloper.tiktok.mappers.LiveRoomMetaMapper;
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse;

import java.util.HashMap;
import java.util.logging.Logger;
import java.util.regex.Pattern;

public class TikTokApiService {
private final TikTokHttpClient tiktokHttpClient;
Expand All @@ -48,7 +44,6 @@ public TikTokApiService(TikTokHttpClient apiClient, Logger logger, ClientSetting
this.clientSettings = clientSettings;
}


public void updateSessionId() {
if (clientSettings.getSessionId() == null) {
return;
Expand All @@ -59,24 +54,19 @@ public void updateSessionId() {
tiktokHttpClient.setSessionId(clientSettings.getSessionId());
}

public String fetchRoomId(String userName) {
var userInfo = fetchUserInfoFromTikTokApi(userName);
clientSettings.getClientParameters().put("room_id", userInfo.getRoomId());
logger.info("RoomID -> " + userInfo.getRoomId());
return userInfo.getRoomId();
public void updateRoomId(String roomId)
{
clientSettings.getClientParameters().put("room_id", roomId);
}

public TikTokUserInfo fetchUserInfoFromTikTokApi(String userName) {

var params = new HashMap<>(clientSettings.getClientParameters());
params.put("uniqueId", userName);
params.put("sourceType", 54);
JsonObject roomData = null;
JsonObject roomData;
try {
roomData = tiktokHttpClient.getJsonFromTikTokApi("api-live/user/room/", params);
} catch (Exception e) {


throw new TikTokLiveRequestException("Failed to fetch pre connection room information, it happens when TikTok temporary blocks you. Try to connect again in few minutes");
}

Expand All @@ -86,7 +76,7 @@ public TikTokUserInfo fetchUserInfoFromTikTokApi(String userName) {
throw new TikTokLiveRequestException("fetchRoomIdFromTiktokApi -> Unable to fetch roomID, contact with developer");
}
if (message.equals("user_not_found")) {
return new TikTokUserInfo(TikTokUserInfo.UserStatus.NotFound, "");
return new TikTokUserInfo(TikTokUserInfo.UserStatus.NotFound, "", -1);
}
//live -> status 2
//live paused -> 3
Expand All @@ -96,14 +86,17 @@ public TikTokUserInfo fetchUserInfoFromTikTokApi(String userName) {
var roomId = user.get("roomId").getAsString();
var status = user.get("status").getAsInt();

var liveRoom = data.getAsJsonObject("liveRoom");
long startTime = liveRoom.get("startTime").getAsLong();

var statusEnum = switch (status) {
case 2 -> TikTokUserInfo.UserStatus.Live;
case 3 -> TikTokUserInfo.UserStatus.LivePaused;
case 4 -> TikTokUserInfo.UserStatus.Offline;
default -> TikTokUserInfo.UserStatus.NotFound;
};

return new TikTokUserInfo(statusEnum, roomId);
return new TikTokUserInfo(statusEnum, roomId, startTime);
}


Expand Down Expand Up @@ -138,4 +131,4 @@ public WebcastResponse fetchClientData() {
throw new TikTokLiveRequestException("Failed to fetch live websocket connection data", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*/
package io.github.jwdeveloper.tiktok.mappers;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.github.jwdeveloper.tiktok.data.models.Picture;
import io.github.jwdeveloper.tiktok.data.models.users.User;
Expand Down Expand Up @@ -123,4 +122,4 @@ public User getUser(JsonObject jsonElement)
user.addAttribute(UserAttribute.LiveHost);
return user;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import com.google.gson.JsonObject;
import io.github.jwdeveloper.tiktok.ClientSettings;
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException;
import io.github.jwdeveloper.tiktok.live.LiveRoomMeta;
import io.github.jwdeveloper.tiktok.mappers.LiveRoomMetaMapper;
Expand Down Expand Up @@ -84,28 +83,6 @@ void updateSessionId_ValidSessionId_SetsSessionId() {
verify(tiktokHttpClient, times(1)).setSessionId("validSessionId");
}


// @Test
void fetchRoomId_ValidResponse_ReturnsRoomId() throws Exception {
String expectedRoomId = "123456";
String htmlResponse = "room_id=" + expectedRoomId ;
when(tiktokHttpClient.getLivestreamPage(anyString())).thenReturn(htmlResponse);

String roomId = tikTokApiService.fetchRoomId("username");

assertEquals(expectedRoomId, roomId);
verify(clientSettings.getClientParameters()).put("room_id", expectedRoomId);
}

// @Test
void fetchRoomId_ExceptionThrown_ThrowsTikTokLiveRequestException() throws Exception {
when(tiktokHttpClient.getLivestreamPage(anyString())).thenThrow(new Exception("some exception"));

assertThrows(TikTokLiveRequestException.class, () -> {
tikTokApiService.fetchRoomId("username");
});
}

//@Test
void fetchRoomInfo_ValidResponse_ReturnsLiveRoomMeta() throws Exception {
HashMap<String, Object> clientParameters = new HashMap<>();
Expand Down