Skip to content

Commit ffabf09

Browse files
authored
Merge pull request #53 from jwdeveloper/develop-1.1.1
Develop 1.1.1
2 parents 2385d1e + 7468fc2 commit ffabf09

File tree

7 files changed

+62
-131
lines changed

7 files changed

+62
-131
lines changed

API/src/main/java/io/github/jwdeveloper/tiktok/data/settings/LiveClientSettings.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@ public class LiveClientSettings {
3636
/**
3737
* ISO-Language for Client
3838
*/
39-
4039
private String clientLanguage;
4140

4241
/**
4342
* Whether to Retry if Connection Fails
4443
*/
4544
private boolean retryOnConnectionFailure;
4645

47-
4846
/**
4947
* Before retrying connect, wait for select amount of time
5048
*/
@@ -53,42 +51,35 @@ public class LiveClientSettings {
5351
/**
5452
* Whether to print Logs to Console
5553
*/
56-
5754
private boolean printToConsole = true;
55+
5856
/**
5957
* LoggingLevel for Logs
6058
*/
6159
private Level logLevel;
6260

63-
6461
/**
6562
* Optional: Use it if you need to change TikTok live hostname in builder
6663
*/
6764
private String hostName;
6865

69-
7066
/**
7167
* Parameters used in requests to TikTok api
7268
*/
7369
private HttpClientSettings httpSettings;
7470

75-
76-
/*
71+
/**
7772
* Optional: Sometimes not every messages from chat are send to TikTokLiveJava to fix this issue you can set sessionId
7873
* documentation how to obtain sessionId https://github.com/isaackogan/TikTok-Live-Connector#send-chat-messages
7974
*/
8075
private String sessionId;
8176

82-
/*
77+
/**
8378
* Optional: By default roomID is fetched before connect to live, but you can set it manually
8479
*
8580
*/
8681
private String roomId;
8782

88-
89-
90-
91-
9283
public static LiveClientSettings createDefault()
9384
{
9485
var httpSettings = new HttpClientSettings();
@@ -103,12 +94,10 @@ public static LiveClientSettings createDefault()
10394
clientSettings.setPrintToConsole(false);
10495
clientSettings.setLogLevel(Level.ALL);
10596

106-
10797
clientSettings.setHttpSettings(httpSettings);
10898
return clientSettings;
10999
}
110100

111-
112101
/**
113102
* Default Parameters for HTTP-Request
114103
*/
@@ -147,11 +136,9 @@ public static Map<String, Object> DefaultClientParams() {
147136
clientParams.put("webcast_sdk_version", "1.3.0");
148137
clientParams.put("update_version_code", "1.3.0");
149138

150-
151139
return clientParams;
152140
}
153141

154-
155142
/**
156143
* Default Headers for HTTP-Request
157144
*/
@@ -167,6 +154,4 @@ public static Map<String, String> DefaultRequestHeaders() {
167154
headers.put("Accept-Language", "en-US,en; q=0.9");
168155
return headers;
169156
}
170-
171-
}
172-
157+
}

API/src/main/java/io/github/jwdeveloper/tiktok/data/settings/ProxyClientSettings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public ProxyData rotate() {
8787
@Override
8888
public void remove() {
8989
proxyList.remove(index);
90+
lastSuccess = false; // index is no longer valid and lastSuccess needs falsified
9091
}
9192

9293
public void setIndex(int index) {

Client/src/main/java/io/github/jwdeveloper/tiktok/websocket/TikTokWebSocketClient.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import io.github.jwdeveloper.tiktok.data.dto.ProxyData;
2727
import io.github.jwdeveloper.tiktok.data.requests.LiveConnectionData;
2828
import io.github.jwdeveloper.tiktok.data.settings.*;
29-
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
29+
import io.github.jwdeveloper.tiktok.exceptions.*;
3030
import io.github.jwdeveloper.tiktok.live.LiveClient;
3131
import org.java_websocket.client.WebSocketClient;
3232

@@ -41,7 +41,7 @@ public class TikTokWebSocketClient implements SocketClient {
4141
private final TikTokLiveEventHandler tikTokEventHandler;
4242
private WebSocketClient webSocketClient;
4343

44-
private TikTokWebSocketPingingTask pingingTask;
44+
private final TikTokWebSocketPingingTask pingingTask;
4545
private boolean isConnected;
4646

4747
public TikTokWebSocketClient(
@@ -91,13 +91,28 @@ private void connectDefault() {
9191
}
9292

9393
public void connectProxy(ProxyClientSettings proxySettings) {
94+
try {
95+
if (proxySettings.getType() == Proxy.Type.SOCKS) {
96+
SSLContext sc = SSLContext.getInstance("SSL");
97+
sc.init(null, new TrustManager[]{new X509TrustManager() {
98+
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {}
99+
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {}
100+
public X509Certificate[] getAcceptedIssuers() { return null; }
101+
}}, null);
102+
webSocketClient.setSocketFactory(sc.getSocketFactory());
103+
}
104+
} catch (Exception e) {
105+
// This will never be thrown.
106+
throw new TikTokProxyRequestException("Unable to set Socks proxy SSL instance");
107+
}
94108
while (proxySettings.hasNext()) {
95109
ProxyData proxyData = proxySettings.next();
96110
if (!tryProxyConnection(proxySettings, proxyData)) {
97111
if (proxySettings.isAutoDiscard())
98112
proxySettings.remove();
99113
continue;
100114
}
115+
pingingTask.run(webSocketClient);
101116
isConnected = true;
102117
break;
103118
}
@@ -107,21 +122,6 @@ public void connectProxy(ProxyClientSettings proxySettings) {
107122

108123
public boolean tryProxyConnection(ProxyClientSettings proxySettings, ProxyData proxyData) {
109124
try {
110-
if (proxySettings.getType() == Proxy.Type.SOCKS) {
111-
SSLContext sc = SSLContext.getInstance("SSL");
112-
sc.init(null, new TrustManager[]{new X509TrustManager() {
113-
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
114-
}
115-
116-
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
117-
}
118-
119-
public X509Certificate[] getAcceptedIssuers() {
120-
return null;
121-
}
122-
}}, null);
123-
webSocketClient.setSocketFactory(sc.getSocketFactory());
124-
}
125125
webSocketClient.setProxy(new Proxy(proxySettings.getType(), proxyData.toSocketAddress()));
126126
webSocketClient.connect();
127127
return true;

Client/src/main/java/io/github/jwdeveloper/tiktok/websocket/TikTokWebSocketPingingTask.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,28 @@ public class TikTokWebSocketPingingTask
1111
private final int MIN_TIMEOUT = 250;
1212
private final int MAX_TIMEOUT = 500;
1313

14-
1514
public void run(WebSocket webSocket)
1615
{
1716
stop();
18-
thread = new Thread(() ->
19-
{
20-
pingTask(webSocket);
21-
});
22-
isRunning =true;
17+
thread = new Thread(() -> pingTask(webSocket));
18+
isRunning = true;
2319
thread.start();
2420
}
2521

2622
public void stop()
2723
{
28-
if(thread != null)
29-
{
24+
if (thread != null)
3025
thread.interrupt();
31-
}
3226
isRunning = false;
3327
}
3428

3529

3630
private void pingTask(WebSocket webSocket)
3731
{
3832
var random = new Random();
39-
while (isRunning)
40-
{
41-
try
42-
{
43-
if(!webSocket.isOpen())
44-
{
33+
while (isRunning) {
34+
try {
35+
if (!webSocket.isOpen()) {
4536
Thread.sleep(100);
4637
continue;
4738
}
@@ -50,11 +41,10 @@ private void pingTask(WebSocket webSocket)
5041
var timeout = random.nextInt(MAX_TIMEOUT)+MIN_TIMEOUT;
5142
Thread.sleep(timeout);
5243
}
53-
catch (Exception e)
54-
{
44+
catch (Exception e) {
5545
isRunning = false;
5646
}
5747
}
5848

5949
}
60-
}
50+
}

extension-recorder/src/main/java/io/github/jwdeveloper/tiktok/extension/recorder/impl/RecorderListener.java

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,18 @@
2424

2525
import com.google.gson.JsonParser;
2626
import io.github.jwdeveloper.tiktok.annotations.TikTokEventObserver;
27-
import io.github.jwdeveloper.tiktok.data.events.TikTokLiveEndedEvent;
27+
import io.github.jwdeveloper.tiktok.data.events.*;
28+
import io.github.jwdeveloper.tiktok.data.events.http.TikTokRoomDataResponseEvent;
2829
import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings;
29-
import io.github.jwdeveloper.tiktok.extension.recorder.api.LiveRecorder;
30-
import io.github.jwdeveloper.tiktok.data.events.TikTokConnectedEvent;
31-
import io.github.jwdeveloper.tiktok.data.events.TikTokDisconnectedEvent;
3230
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
33-
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.DownloadData;
34-
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.RecorderSettings;
35-
import io.github.jwdeveloper.tiktok.data.events.http.TikTokRoomDataResponseEvent;
31+
import io.github.jwdeveloper.tiktok.extension.recorder.api.LiveRecorder;
32+
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.*;
3633
import io.github.jwdeveloper.tiktok.extension.recorder.impl.enums.LiveQuality;
37-
import io.github.jwdeveloper.tiktok.extension.recorder.impl.event.TikTokLiveRecorderStartedEvent;
38-
import io.github.jwdeveloper.tiktok.http.HttpClientFactory;
3934
import io.github.jwdeveloper.tiktok.live.LiveClient;
4035

4136
import javax.net.ssl.HttpsURLConnection;
4237
import java.io.*;
4338
import java.net.URL;
44-
import java.util.List;
4539
import java.util.concurrent.TimeUnit;
4640
import java.util.function.Consumer;
4741

@@ -86,13 +80,11 @@ private void onResponse(LiveClient liveClient, TikTokRoomDataResponseEvent event
8680
throw new TikTokLiveException("Unable to find download live url!");
8781
}
8882
liveClient.getLogger().info("Live download url found!");
89-
9083
}
9184

9285
@TikTokEventObserver
9386
private void onConnected(LiveClient liveClient, TikTokConnectedEvent event) {
94-
liveDownloadThread = new Thread(() ->
95-
{
87+
liveDownloadThread = new Thread(() -> {
9688
try {
9789
var bufferSize = 1024;
9890
var url = new URL(downloadData.getFullUrl());
@@ -102,66 +94,57 @@ private void onConnected(LiveClient liveClient, TikTokConnectedEvent event) {
10294
socksConnection.setRequestProperty(entry.getKey(), entry.getValue());
10395
}
10496

105-
try (var in = new BufferedInputStream(socksConnection.getInputStream())) {
106-
var path = settings.getOutputPath() + File.separator + settings.getOutputFileName();
107-
var file = new File(path);
108-
file.getParentFile().mkdirs();
109-
var fileOutputStream = new FileOutputStream(file);
110-
byte dataBuffer[] = new byte[bufferSize];
111-
int bytesRead;
112-
while ((bytesRead = in.read(dataBuffer, 0, bufferSize)) != -1) {
113-
fileOutputStream.write(dataBuffer, 0, bytesRead);
114-
}
115-
} catch (IOException e) {
116-
throw e;
97+
var in = new BufferedInputStream(socksConnection.getInputStream());
98+
var path = settings.getOutputPath() + File.separator + settings.getOutputFileName();
99+
var file = new File(path);
100+
file.getParentFile().mkdirs();
101+
var fileOutputStream = new FileOutputStream(file);
102+
byte[] dataBuffer = new byte[bufferSize];
103+
int bytesRead;
104+
while ((bytesRead = in.read(dataBuffer, 0, bufferSize)) != -1) {
105+
fileOutputStream.write(dataBuffer, 0, bytesRead);
117106
}
118-
} catch (Exception e) {
107+
in.close();
108+
} catch (Exception e) {
119109
e.printStackTrace();
120-
;
121110
}
122-
123111
});
124112

125-
126113
liveDownloadThread.start();
127114
}
128115

129-
130116
private static void downloadUsingStream(String urlStr, String file) throws IOException {
131117
URL url = new URL(urlStr);
132118
BufferedInputStream bis = new BufferedInputStream(url.openStream());
133119
FileOutputStream fis = new FileOutputStream(file);
134120
byte[] buffer = new byte[1024];
135-
int count = 0;
136-
while ((count = bis.read(buffer, 0, 1024)) != -1) {
121+
int count;
122+
while ((count = bis.read(buffer, 0, 1024)) != -1)
137123
fis.write(buffer, 0, count);
138-
}
139124
fis.close();
140125
bis.close();
141126
}
142127

143128

144129
@TikTokEventObserver
145130
private void onDisconnected(LiveClient liveClient, TikTokDisconnectedEvent event) {
146-
liveDownloadThread.interrupt();
131+
if (isConnected())
132+
liveDownloadThread.interrupt();
147133
}
148134

149135
@TikTokEventObserver
150136
private void onDisconnected(LiveClient liveClient, TikTokLiveEndedEvent event) {
151-
liveDownloadThread.interrupt();
137+
if (isConnected())
138+
liveDownloadThread.interrupt();
152139
}
153140

154141
private int terminateFfmpeg(final Process process) {
155142
if (!process.isAlive()) {
156-
/*
157-
* ffmpeg -version, do nothing
158-
*/
143+
// ffmpeg -version, do nothing
159144
return process.exitValue();
160145
}
161146

162-
/*
163-
* ffmpeg -f x11grab
164-
*/
147+
// ffmpeg -f x11grab
165148
System.out.println("About to destroy the child process...");
166149
try (final OutputStreamWriter out = new OutputStreamWriter(process.getOutputStream(), UTF_8)) {
167150
out.write('q');
@@ -174,7 +157,7 @@ private int terminateFfmpeg(final Process process) {
174157
process.waitFor();
175158
}
176159
return process.exitValue();
177-
} catch (final InterruptedException ie) {
160+
} catch (InterruptedException ie) {
178161
System.out.println("Interrupted");
179162
ie.printStackTrace();
180163
Thread.currentThread().interrupt();
@@ -201,12 +184,10 @@ private DownloadData mapToDownloadData(String json) {
201184
.get("flv")
202185
.getAsString();
203186

204-
205187
var sessionId = streamDataJsonObject.getAsJsonObject("common")
206188
.get("session_id")
207189
.getAsString();
208190

209-
210191
//main
211192
//https://pull-f5-tt03.fcdn.eu.tiktokcdn.com/stage/stream-3284937501738533765.flv?session_id=136-20240109000954BF818F1B3A8E5E39E238&_webnoredir=1
212193
//Working
@@ -216,5 +197,7 @@ private DownloadData mapToDownloadData(String json) {
216197
return new DownloadData(urlLink, sessionId);
217198
}
218199

219-
220-
}
200+
private boolean isConnected() {
201+
return liveDownloadThread != null && liveDownloadThread.isAlive();
202+
}
203+
}

0 commit comments

Comments
 (0)