Skip to content

Commit b6247fe

Browse files
kohlerpop1jwdeveloper
authored andcommitted
Final Proxy Commit!
1 parent 0dd952a commit b6247fe

File tree

13 files changed

+174
-106
lines changed

13 files changed

+174
-106
lines changed

API/src/main/java/io/github/jwdeveloper/tiktok/data/events/TikTokDisconnectedEvent.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,20 @@
2424
import io.github.jwdeveloper.tiktok.annotations.EventMeta;
2525
import io.github.jwdeveloper.tiktok.annotations.EventType;
2626
import io.github.jwdeveloper.tiktok.data.events.common.TikTokLiveClientEvent;
27-
27+
import lombok.Getter;
2828

2929
/**
3030
* Triggered when the connection gets disconnected. In that case you can call connect() again to have a reconnect logic.
3131
* Note that you should wait a little bit before attempting a reconnect to to avoid being rate-limited.
3232
*/
3333
@EventMeta(eventType = EventType.Control)
3434
public class TikTokDisconnectedEvent extends TikTokLiveClientEvent {
35-
}
35+
@Getter private final String reason;
36+
public TikTokDisconnectedEvent(String reason) {
37+
this.reason = reason.isBlank() ? "None" : reason;
38+
}
39+
40+
public TikTokDisconnectedEvent() {
41+
this("None");
42+
}
43+
}

API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveUserData.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
*/
2323
package io.github.jwdeveloper.tiktok.data.requests;
2424

25-
import lombok.AllArgsConstructor;
26-
import lombok.Data;
27-
import lombok.Getter;
28-
import lombok.Setter;
25+
import lombok.*;
2926

3027
public class LiveUserData {
3128

@@ -38,15 +35,18 @@ public static class Request {
3835
@Getter
3936
@AllArgsConstructor
4037
public static class Response {
41-
4238
private String json;
43-
4439
private UserStatus userStatus;
45-
4640
private String roomId;
41+
private long startedAtTimeStamp;
4742

43+
public boolean isLiveOnline() {
44+
return userStatus == LiveUserData.UserStatus.Live || userStatus == LiveUserData.UserStatus.LivePaused;
45+
}
4846

49-
private long startedAtTimeStamp;
47+
public boolean isHostNameValid() {
48+
return userStatus != LiveUserData.UserStatus.NotFound;
49+
}
5050
}
5151

5252
public enum UserStatus {
@@ -55,6 +55,4 @@ public enum UserStatus {
5555
LivePaused,
5656
Live,
5757
}
58-
}
59-
60-
58+
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ public boolean hasNext() {
6363
}
6464

6565
@Override
66-
public ProxyData next()
67-
{
68-
if (lastSuccess)
69-
return proxyList.get(index);
66+
public ProxyData next() {
67+
return lastSuccess ? proxyList.get(index) : rotate();
68+
}
69+
70+
public ProxyData rotate() {
7071
var nextProxy = switch (rotation)
7172
{
7273
case CONSECUTIVE -> {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2023-2023 jwdeveloper [email protected]
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
package io.github.jwdeveloper.tiktok.exceptions;
24+
25+
26+
/*
27+
* Happens while bad response from http proxy request to TikTok
28+
*/
29+
public class TikTokProxyRequestException extends TikTokLiveException
30+
{
31+
public TikTokProxyRequestException() {
32+
}
33+
34+
public TikTokProxyRequestException(String message) {
35+
super(message);
36+
}
37+
38+
public TikTokProxyRequestException(String message, Throwable cause) {
39+
super(message, cause);
40+
}
41+
42+
public TikTokProxyRequestException(Throwable cause) {
43+
super(cause);
44+
}
45+
46+
public TikTokProxyRequestException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
47+
super(message, cause, enableSuppression, writableStackTrace);
48+
}
49+
}

Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLive.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
package io.github.jwdeveloper.tiktok;
2424

2525

26-
import io.github.jwdeveloper.tiktok.data.requests.LiveUserData;
2726
import io.github.jwdeveloper.tiktok.http.LiveHttpClient;
2827
import io.github.jwdeveloper.tiktok.live.builder.LiveClientBuilder;
2928

@@ -48,8 +47,7 @@ public static LiveClientBuilder newClient(String hostName) {
4847
*/
4948
public static boolean isLiveOnline(String hostName)
5049
{
51-
LiveUserData.UserStatus status = requests().fetchLiveUserData(hostName).getUserStatus();
52-
return status == LiveUserData.UserStatus.Live || status == LiveUserData.UserStatus.LivePaused;
50+
return requests().fetchLiveUserData(hostName).isLiveOnline();
5351
}
5452

5553

@@ -72,8 +70,7 @@ public static CompletableFuture<Boolean> isLiveOnlineAsync(String hostName)
7270
*/
7371
public static boolean isHostNameValid(String hostName)
7472
{
75-
LiveUserData.UserStatus status = requests().fetchLiveUserData(hostName).getUserStatus();
76-
return status != LiveUserData.UserStatus.NotFound;
73+
return requests().fetchLiveUserData(hostName).isHostNameValid();
7774
}
7875

7976
/**

Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClient.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,15 @@ public TikTokLiveClient(TikTokRoomInfo tikTokLiveMeta,
7676

7777

7878
public void connectAsync(Consumer<LiveClient> onConnection) {
79-
CompletableFuture.supplyAsync(() ->
80-
{
79+
CompletableFuture.runAsync(() -> {
8180
connect();
8281
onConnection.accept(this);
83-
return this;
8482
});
85-
8683
}
8784

8885

8986
public CompletableFuture<LiveClient> connectAsync() {
90-
return CompletableFuture.supplyAsync(() ->
91-
{
87+
return CompletableFuture.supplyAsync(() -> {
9288
connect();
9389
return this;
9490
});
@@ -105,8 +101,7 @@ public void connect() {
105101
if (e instanceof TikTokLiveOfflineHostException && clientSettings.isRetryOnConnectionFailure()) {
106102
try {
107103
Thread.sleep(clientSettings.getRetryConnectionTimeout().toMillis());
108-
} catch (Exception ignored) {
109-
}
104+
} catch (Exception ignored) {}
110105
logger.info("Reconnecting");
111106
tikTokEventHandler.publish(this, new TikTokReconnectingEvent());
112107
this.connect();
@@ -120,14 +115,12 @@ public void connect() {
120115
}
121116

122117
public void tryConnect() {
123-
if (!liveRoomInfo.hasConnectionState(ConnectionState.DISCONNECTED))
124-
{
118+
if (!liveRoomInfo.hasConnectionState(ConnectionState.DISCONNECTED)) {
125119
throw new TikTokLiveException("Already connected");
126120
}
127121

128122
setState(ConnectionState.CONNECTING);
129123

130-
131124
var userDataRequest = new LiveUserData.Request(liveRoomInfo.getHostName());
132125
var userData = httpClient.fetchLiveUserData(userDataRequest);
133126
liveRoomInfo.setStartTime(userData.getStartedAtTimeStamp());
@@ -139,7 +132,6 @@ public void tryConnect() {
139132
throw new TikTokLiveOfflineHostException("User not found: "+liveRoomInfo.getHostUser());
140133
}
141134

142-
143135
var liveDataRequest = new LiveData.Request(userData.getRoomId());
144136
var liveData = httpClient.fetchLiveData(liveDataRequest);
145137
if (liveData.getLiveStatus() == LiveData.LiveStatus.HostNotFound) {
@@ -155,7 +147,6 @@ public void tryConnect() {
155147
liveRoomInfo.setAgeRestricted(liveData.isAgeRestricted());
156148
liveRoomInfo.setHost(liveData.getHost());
157149

158-
159150
var liveConnectionRequest =new LiveConnectionData.Request(userData.getRoomId());
160151
var liveConnectionData = httpClient.fetchLiveConnectionData(liveConnectionRequest);
161152
webSocketClient.start(liveConnectionData, this);
@@ -181,7 +172,6 @@ public void publishEvent(TikTokEvent event) {
181172
tikTokEventHandler.publish(this, event);
182173
}
183174

184-
185175
public LiveRoomInfo getRoomInfo() {
186176
return liveRoomInfo;
187177
}
@@ -200,6 +190,4 @@ public Logger getLogger() {
200190
public GiftManager getGiftManager() {
201191
return tikTokGiftManager;
202192
}
203-
204-
205193
}

Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClientBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public LiveClient build() {
156156

157157

158158
var httpClientFactory = new HttpClientFactory(clientSettings);
159-
var tikTokLiveHttpClient = new TikTokLiveHttpClient(httpClientFactory);
159+
var tikTokLiveHttpClient = new TikTokLiveHttpClient(httpClientFactory, clientSettings);
160160

161161
var webSocketClient = new TikTokWebSocketClient(
162162
clientSettings,
@@ -346,7 +346,7 @@ public <E extends TikTokEvent> LiveClientBuilder onEvent(Class<E> eventClass, Ev
346346

347347

348348
@Override
349-
public LiveClientBuilder onRoomInfo(EventConsumer<TikTokRoomInfoEvent> event) {
349+
public TikTokLiveClientBuilder onRoomInfo(EventConsumer<TikTokRoomInfoEvent> event) {
350350
tikTokEventHandler.subscribe(TikTokRoomInfoEvent.class, event);
351351
return this;
352352
}
@@ -358,7 +358,7 @@ public TikTokLiveClientBuilder onLivePaused(EventConsumer<TikTokLivePausedEvent>
358358
}
359359

360360
@Override
361-
public LiveClientBuilder onLiveUnpaused(EventConsumer<TikTokLiveUnpausedEvent> event) {
361+
public TikTokLiveClientBuilder onLiveUnpaused(EventConsumer<TikTokLiveUnpausedEvent> event) {
362362
tikTokEventHandler.subscribe(TikTokLiveUnpausedEvent.class, event);
363363
return this;
364364
}

0 commit comments

Comments
 (0)