Removal of TikTokRoomDataResponseEvent.java as its included in TikTokPreConnectionEvent.java

Added ListUser#toString
Changed TikTokLiveHttpClient.getStartingPayload and TikTokLiveHttpClient.getByteResponse to protected
Altered DownloadData to be dynamic to declare request values or append them instead!
This commit is contained in:
kohlerpop1
2024-07-18 17:41:27 -04:00
parent ee0b559758
commit 3d4a517adb
6 changed files with 56 additions and 79 deletions

View File

@@ -1,38 +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.data.events.http;
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.requests.LiveData;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
@EventMeta(eventType = EventType.Debug)
public class TikTokRoomDataResponseEvent extends TikTokEvent
{
private final LiveData.Response liveData;
}

View File

@@ -53,4 +53,19 @@ public class ListUser
AUDIO,
VIDEO
}
@Override
public String toString() {
return "ListUser{" +
"user=" + user +
", linkType=" + linkType +
", linkMicId=" + linkMicId +
", linkStatus=" + linkStatus +
", modifyTime=" + modifyTime +
", linkerId=" + linkerId +
", userPosition=" + userPosition +
", silenceStatus=" + silenceStatus +
", roleType=" + roleType +
"}";
}
}

View File

@@ -26,7 +26,6 @@ import com.google.protobuf.ByteString;
import io.github.jwdeveloper.tiktok.data.events.*;
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
import io.github.jwdeveloper.tiktok.data.events.control.*;
import io.github.jwdeveloper.tiktok.data.events.http.TikTokRoomDataResponseEvent;
import io.github.jwdeveloper.tiktok.data.events.room.TikTokRoomInfoEvent;
import io.github.jwdeveloper.tiktok.data.requests.*;
import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings;
@@ -89,8 +88,7 @@ public class TikTokLiveClient implements LiveClient
if (e instanceof TikTokLiveOfflineHostException && clientSettings.isRetryOnConnectionFailure()) {
try {
Thread.sleep(clientSettings.getRetryConnectionTimeout().toMillis());
} catch (Exception ignored) {
}
} catch (Exception ignored) {}
logger.info("Reconnecting");
tikTokEventHandler.publish(this, new TikTokReconnectingEvent());
this.connect();
@@ -133,8 +131,6 @@ public class TikTokLiveClient implements LiveClient
if (liveData.getLiveStatus() == LiveData.LiveStatus.HostOffline)
throw new TikTokLiveOfflineHostException("LiveStream for " + roomInfo.getHostName() + " not found, is the Host offline?");
tikTokEventHandler.publish(this, new TikTokRoomDataResponseEvent(liveData));
roomInfo.setTitle(liveData.getTitle());
roomInfo.setViewersCount(liveData.getViewers());
roomInfo.setTotalViewersCount(liveData.getTotalViewers());

View File

@@ -203,7 +203,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
}
}
private ActionResult<HttpResponse<byte[]>> getStartingPayload(LiveConnectionData.Request request) {
protected ActionResult<HttpResponse<byte[]>> getStartingPayload(LiveConnectionData.Request request) {
var proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
if (proxyClientSettings.isEnabled()) {
while (proxyClientSettings.hasNext()) {
@@ -215,7 +215,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
return getByteResponse(request.getRoomId());
}
private ActionResult<HttpResponse<byte[]>> getByteResponse(String room_id) {
protected ActionResult<HttpResponse<byte[]>> getByteResponse(String room_id) {
HttpClientBuilder builder = httpFactory.client(TIKTOK_SIGN_API)
.withParam("client", "ttlive-java")
.withParam("uuc", "1") //MAGIC NUMBER!

View File

@@ -25,7 +25,7 @@ package io.github.jwdeveloper.tiktok.extension.recorder.impl;
import com.google.gson.JsonParser;
import io.github.jwdeveloper.tiktok.annotations.TikTokEventObserver;
import io.github.jwdeveloper.tiktok.data.events.*;
import io.github.jwdeveloper.tiktok.data.events.http.TikTokRoomDataResponseEvent;
import io.github.jwdeveloper.tiktok.data.events.control.TikTokPreConnectionEvent;
import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings;
import io.github.jwdeveloper.tiktok.extension.recorder.api.LiveRecorder;
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.*;
@@ -34,28 +34,29 @@ import io.github.jwdeveloper.tiktok.extension.recorder.impl.event.TikTokLiveReco
import io.github.jwdeveloper.tiktok.live.LiveClient;
import io.github.jwdeveloper.tiktok.models.ConnectionState;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;
import java.util.function.*;
import java.net.URI;
import java.net.http.*;
import java.time.Duration;
import java.util.function.BiConsumer;
public class RecorderListener implements LiveRecorder {
private final BiConsumer<RecorderSettings, LiveClient> consumer;
private RecorderSettings settings;
private final RecorderSettings settings;
private DownloadData downloadData;
private Thread liveDownloadThread;
public RecorderListener(BiConsumer<RecorderSettings, LiveClient> consumer) {
this.consumer = consumer;
this.settings = RecorderSettings.DEFAULT();
}
@TikTokEventObserver
private void onResponse(LiveClient liveClient, TikTokRoomDataResponseEvent event) {
settings = RecorderSettings.DEFAULT();
private void onResponse(LiveClient liveClient, TikTokPreConnectionEvent event) {
consumer.accept(settings, liveClient);
var json = event.getLiveData().getJson();
var json = event.getUserData().getJson();
liveClient.getLogger().info("Searching for live download url");
downloadData = settings.getPrepareDownloadData() != null ?
@@ -70,26 +71,26 @@ public class RecorderListener implements LiveRecorder {
@TikTokEventObserver
private void onConnected(LiveClient liveClient, TikTokConnectedEvent event) {
if (isConnected())
if (isConnected() || downloadData.getDownloadLiveUrl().isEmpty())
return;
liveDownloadThread = new Thread(() -> {
try {
liveClient.getLogger().info("Recording started "+liveClient.getRoomInfo().getHostName());
var url = new URL(downloadData.getFullUrl());
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
var headers = LiveClientSettings.DefaultRequestHeaders();
for (var entry : headers.entrySet()) {
connection.setRequestProperty(entry.getKey(), entry.getValue());
}
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(URI.create(downloadData.getFullUrl())).GET();
for (var entry : LiveClientSettings.DefaultRequestHeaders().entrySet())
requestBuilder.header(entry.getKey(), entry.getValue());
HttpResponse<InputStream> serverResponse = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(10)).build().send(requestBuilder.build(), HttpResponse.BodyHandlers.ofInputStream());
var file = settings.getOutputFile();
file.getParentFile().mkdirs();
file.createNewFile();
try (
var in = connection.getInputStream();
var fos = new FileOutputStream(file)
var in = serverResponse.body();
var fos = new FileOutputStream(file, true)
) {
byte[] dataBuffer = new byte[1024];
int bytesRead;
@@ -128,35 +129,38 @@ public class RecorderListener implements LiveRecorder {
private DownloadData mapToDownloadData(String json) {
var parsedJson = JsonParser.parseString(json);
var jsonObject = parsedJson.getAsJsonObject();
var streamDataJson = jsonObject.getAsJsonObject("data")
.getAsJsonObject("stream_url")
.getAsJsonObject("live_core_sdk_data")
try {
var parsedJson = JsonParser.parseString(json);
var jsonObject = parsedJson.getAsJsonObject();
var streamDataJson = jsonObject.getAsJsonObject("data")
.getAsJsonObject("liveRoom")
.getAsJsonObject("streamData")
.getAsJsonObject("pull_data")
.get("stream_data")
.getAsString();
var streamDataJsonObject = JsonParser.parseString(streamDataJson).getAsJsonObject();
var streamDataJsonObject = JsonParser.parseString(streamDataJson).getAsJsonObject();
var urlLink = streamDataJsonObject.getAsJsonObject("data")
var urlLink = streamDataJsonObject.getAsJsonObject("data")
.getAsJsonObject(LiveQuality.origin.name())
.getAsJsonObject("main")
.get("flv")
.getAsString();
var sessionId = streamDataJsonObject.getAsJsonObject("common")
var sessionId = streamDataJsonObject.getAsJsonObject("common")
.get("session_id")
.getAsString();
//main
//https://pull-f5-tt03.fcdn.eu.tiktokcdn.com/stage/stream-3284937501738533765.flv?session_id=136-20240109000954BF818F1B3A8E5E39E238&_webnoredir=1
//Working
//https://pull-f5-tt03.fcdn.eu.tiktokcdn.com/game/stream-3284937501738533765_sd5.flv?_session_id=136-20240109001052D91FDBC00143211020C8.1704759052997&_webnoredir=1
//https://pull-f5-tt02.fcdn.eu.tiktokcdn.com/stage/stream-3861399216374940610_uhd5.flv?_session_id=136-20240109000223D0BAA1A83974490EE630.1704758544391&_webnoredir=1
//main
//https://pull-f5-tt03.fcdn.eu.tiktokcdn.com/stage/stream-3284937501738533765.flv?session_id=136-20240109000954BF818F1B3A8E5E39E238&_webnoredir=1
//Working
//https://pull-f5-tt03.fcdn.eu.tiktokcdn.com/game/stream-3284937501738533765_sd5.flv?_session_id=136-20240109001052D91FDBC00143211020C8.1704759052997&_webnoredir=1
//https://pull-f5-tt02.fcdn.eu.tiktokcdn.com/stage/stream-3861399216374940610_uhd5.flv?_session_id=136-20240109000223D0BAA1A83974490EE630.1704758544391&_webnoredir=1
return new DownloadData(urlLink, sessionId);
return new DownloadData(urlLink, sessionId);
} catch (Exception e) {
return new DownloadData("", "");
}
}
private boolean isConnected() {

View File

@@ -34,6 +34,6 @@ public class DownloadData {
private String sessionId;
public String getFullUrl() {
return downloadLiveUrl + "&_webnoredir=1&session_id=" + sessionId;
}
return downloadLiveUrl + (downloadLiveUrl.contains("?") ? "&" : "?") + "_webnoredir=1&session_id=" + sessionId;
}
}