Skip to content

Commit bbfa7b4

Browse files
authored
Merge pull request #55 from jwdeveloper/develop-1.2.0
Develop 1.2.0
2 parents 6da4092 + 4d97fd9 commit bbfa7b4

File tree

11 files changed

+141
-46
lines changed

11 files changed

+141
-46
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.data.events.control;
24+
25+
import io.github.jwdeveloper.tiktok.annotations.*;
26+
import io.github.jwdeveloper.tiktok.data.events.common.TikTokLiveClientEvent;
27+
import io.github.jwdeveloper.tiktok.data.requests.*;
28+
import lombok.*;
29+
30+
/**
31+
* Triggered before the connection is established.
32+
*/
33+
@EventMeta(eventType = EventType.Control)
34+
public class TikTokPreConnectionEvent extends TikTokLiveClientEvent
35+
{
36+
@Getter private final LiveUserData.Response userData;
37+
@Getter private final LiveData.Response roomData;
38+
@Getter @Setter boolean cancelConnection = false;
39+
40+
public TikTokPreConnectionEvent(LiveUserData.Response userData, LiveData.Response liveData) {
41+
this.userData = userData;
42+
this.roomData = liveData;
43+
}
44+
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,19 @@ public static class Response {
4444
private int totalViewers;
4545
private boolean ageRestricted;
4646
private User host;
47+
private LiveType liveType;
4748
}
4849

4950
public enum LiveStatus {
5051
HostNotFound,
5152
HostOnline,
5253
HostOffline,
5354
}
54-
}
55+
56+
public enum LiveType {
57+
SOLO,
58+
BOX,
59+
BATTLE,
60+
CO_HOST
61+
}
62+
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@
3333
@Setter
3434
public class ProxyClientSettings implements Iterator<ProxyData>
3535
{
36-
private boolean enabled, lastSuccess;
36+
private boolean enabled, lastSuccess, autoDiscard = true, fallback = true;
3737
private Rotation rotation = Rotation.CONSECUTIVE;
3838
private final List<ProxyData> proxyList = new ArrayList<>();
39-
private int index = 0;
40-
private boolean autoDiscard = true;
39+
private int index = -1;
4140
private Proxy.Type type = Proxy.Type.DIRECT;
4241
private Consumer<ProxyData> onProxyUpdated = x -> {};
4342

@@ -78,7 +77,10 @@ public ProxyData rotate() {
7877
index = new Random().nextInt(proxyList.size());
7978
yield proxyList.get(index).clone();
8079
}
81-
case NONE -> proxyList.get(index).clone();
80+
case NONE -> {
81+
index = Math.max(index, 0);
82+
yield proxyList.get(index).clone();
83+
}
8284
};
8385
onProxyUpdated.accept(nextProxy);
8486
return nextProxy;
@@ -99,6 +101,7 @@ public void setIndex(int index) {
99101
this.index = index;
100102
}
101103
}
104+
102105
public ProxyClientSettings clone()
103106
{
104107
ProxyClientSettings settings = new ProxyClientSettings();

API/src/main/java/io/github/jwdeveloper/tiktok/exceptions/TikTokLiveRequestException.java

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

2525

26-
/*
26+
/**
2727
* Happens while bad response from Http request to TikTok
2828
*/
2929
public class TikTokLiveRequestException extends TikTokLiveException
@@ -46,4 +46,4 @@ public TikTokLiveRequestException(Throwable cause) {
4646
public TikTokLiveRequestException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
4747
super(message, cause, enableSuppression, writableStackTrace);
4848
}
49-
}
49+
}

API/src/main/java/io/github/jwdeveloper/tiktok/live/builder/EventsBuilder.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
2626
import io.github.jwdeveloper.tiktok.data.events.*;
27+
import io.github.jwdeveloper.tiktok.data.events.control.TikTokPreConnectionEvent;
2728
import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftComboEvent;
2829
import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftEvent;
2930
import io.github.jwdeveloper.tiktok.data.events.http.TikTokHttpResponseEvent;
@@ -149,6 +150,13 @@ public interface EventsBuilder<T> {
149150
*/
150151
T onConnected(EventConsumer<TikTokConnectedEvent> action);
151152

153+
/**
154+
* Invoked before client has been successfully connected to live
155+
* @param action
156+
* @return
157+
*/
158+
T onPreConnection(EventConsumer<TikTokPreConnectionEvent> action);
159+
152160
/**
153161
* Invoked when client tries to reconnect
154162
* @param action
@@ -215,6 +223,4 @@ public interface EventsBuilder<T> {
215223
//T onLinkMicBattle(TikTokEventConsumer<TikTokLinkMicBattleEvent> event);
216224

217225
//T onUnhandledControl(TikTokEventConsumer<TikTokUnhandledControlEvent> event);
218-
}
219-
220-
226+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import io.github.jwdeveloper.tiktok.data.events.TikTokErrorEvent;
2727
import io.github.jwdeveloper.tiktok.data.events.TikTokReconnectingEvent;
2828
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
29-
import io.github.jwdeveloper.tiktok.data.events.control.TikTokConnectingEvent;
29+
import io.github.jwdeveloper.tiktok.data.events.control.*;
3030
import io.github.jwdeveloper.tiktok.data.events.http.TikTokRoomDataResponseEvent;
3131
import io.github.jwdeveloper.tiktok.data.events.room.TikTokRoomInfoEvent;
3232
import io.github.jwdeveloper.tiktok.data.requests.LiveConnectionData;
@@ -150,6 +150,11 @@ public void tryConnect() {
150150
liveRoomInfo.setAgeRestricted(liveData.isAgeRestricted());
151151
liveRoomInfo.setHost(liveData.getHost());
152152

153+
var preconnectEvent = new TikTokPreConnectionEvent(userData, liveData);
154+
tikTokEventHandler.publish(this, preconnectEvent);
155+
if (preconnectEvent.isCancelConnection())
156+
throw new TikTokLiveException("TikTokPreConnectionEvent cancelled connection!");
157+
153158
var liveConnectionRequest =new LiveConnectionData.Request(userData.getRoomId());
154159
var liveConnectionData = httpClient.fetchLiveConnectionData(liveConnectionRequest);
155160
webSocketClient.start(liveConnectionData, this);

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

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import io.github.jwdeveloper.tiktok.data.events.*;
2626
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
27+
import io.github.jwdeveloper.tiktok.data.events.control.TikTokPreConnectionEvent;
2728
import io.github.jwdeveloper.tiktok.data.events.envelop.TikTokChestEvent;
2829
import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftComboEvent;
2930
import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftEvent;
@@ -275,9 +276,7 @@ public CompletableFuture<LiveClient> buildAndConnectAsync() {
275276
return build().connectAsync();
276277
}
277278

278-
279-
public TikTokLiveClientBuilder onUnhandledSocial(
280-
EventConsumer<TikTokUnhandledSocialEvent> event) {
279+
public TikTokLiveClientBuilder onUnhandledSocial(EventConsumer<TikTokUnhandledSocialEvent> event) {
281280
tikTokEventHandler.subscribe(TikTokUnhandledSocialEvent.class, event);
282281
return this;
283282
}
@@ -289,8 +288,7 @@ public LiveClientBuilder onChest(EventConsumer<TikTokChestEvent> event) {
289288
}
290289

291290

292-
public TikTokLiveClientBuilder onLinkMicFanTicket(
293-
EventConsumer<TikTokLinkMicFanTicketEvent> event) {
291+
public TikTokLiveClientBuilder onLinkMicFanTicket(EventConsumer<TikTokLinkMicFanTicketEvent> event) {
294292
tikTokEventHandler.subscribe(TikTokLinkMicFanTicketEvent.class, event);
295293
return this;
296294
}
@@ -305,14 +303,12 @@ public TikTokLiveClientBuilder onShop(EventConsumer<TikTokShopEvent> event) {
305303
return this;
306304
}
307305

308-
public TikTokLiveClientBuilder onDetect(
309-
EventConsumer<TikTokDetectEvent> event) {
306+
public TikTokLiveClientBuilder onDetect(EventConsumer<TikTokDetectEvent> event) {
310307
tikTokEventHandler.subscribe(TikTokDetectEvent.class, event);
311308
return this;
312309
}
313310

314-
public TikTokLiveClientBuilder onLinkLayer(
315-
EventConsumer<TikTokLinkLayerEvent> event) {
311+
public TikTokLiveClientBuilder onLinkLayer(EventConsumer<TikTokLinkLayerEvent> event) {
316312
tikTokEventHandler.subscribe(TikTokLinkLayerEvent.class, event);
317313
return this;
318314
}
@@ -322,6 +318,11 @@ public TikTokLiveClientBuilder onConnected(EventConsumer<TikTokConnectedEvent> e
322318
return this;
323319
}
324320

321+
public TikTokLiveClientBuilder onPreConnection(EventConsumer<TikTokPreConnectionEvent> event) {
322+
tikTokEventHandler.subscribe(TikTokPreConnectionEvent.class, event);
323+
return this;
324+
}
325+
325326
public TikTokLiveClientBuilder onCaption(EventConsumer<TikTokCaptionEvent> event) {
326327
tikTokEventHandler.subscribe(TikTokCaptionEvent.class, event);
327328
return this;
@@ -332,8 +333,7 @@ public TikTokLiveClientBuilder onQuestion(EventConsumer<TikTokQuestionEvent> eve
332333
return this;
333334
}
334335

335-
public TikTokLiveClientBuilder onRoomPin(
336-
EventConsumer<TikTokRoomPinEvent> event) {
336+
public TikTokLiveClientBuilder onRoomPin(EventConsumer<TikTokRoomPinEvent> event) {
337337
tikTokEventHandler.subscribe(TikTokRoomPinEvent.class, event);
338338
return this;
339339
}
@@ -373,8 +373,7 @@ public TikTokLiveClientBuilder onLink(EventConsumer<TikTokLinkEvent> event) {
373373
return this;
374374
}
375375

376-
public TikTokLiveClientBuilder onBarrage(
377-
EventConsumer<TikTokBarrageEvent> event) {
376+
public TikTokLiveClientBuilder onBarrage(EventConsumer<TikTokBarrageEvent> event) {
378377
tikTokEventHandler.subscribe(TikTokBarrageEvent.class, event);
379378
return this;
380379
}
@@ -391,8 +390,7 @@ public TikTokLiveClientBuilder onGiftCombo(EventConsumer<TikTokGiftComboEvent> e
391390
}
392391

393392

394-
public TikTokLiveClientBuilder onLinkMicArmies(
395-
EventConsumer<TikTokLinkMicArmiesEvent> event) {
393+
public TikTokLiveClientBuilder onLinkMicArmies(EventConsumer<TikTokLinkMicArmiesEvent> event) {
396394
tikTokEventHandler.subscribe(TikTokLinkMicArmiesEvent.class, event);
397395
return this;
398396
}
@@ -402,20 +400,17 @@ public TikTokLiveClientBuilder onEmote(EventConsumer<TikTokEmoteEvent> event) {
402400
return this;
403401
}
404402

405-
public TikTokLiveClientBuilder onUnauthorizedMember(
406-
EventConsumer<TikTokUnauthorizedMemberEvent> event) {
403+
public TikTokLiveClientBuilder onUnauthorizedMember(EventConsumer<TikTokUnauthorizedMemberEvent> event) {
407404
tikTokEventHandler.subscribe(TikTokUnauthorizedMemberEvent.class, event);
408405
return this;
409406
}
410407

411-
public TikTokLiveClientBuilder onInRoomBanner(
412-
EventConsumer<TikTokInRoomBannerEvent> event) {
408+
public TikTokLiveClientBuilder onInRoomBanner(EventConsumer<TikTokInRoomBannerEvent> event) {
413409
tikTokEventHandler.subscribe(TikTokInRoomBannerEvent.class, event);
414410
return this;
415411
}
416412

417-
public TikTokLiveClientBuilder onLinkMicMethod(
418-
EventConsumer<TikTokLinkMicMethodEvent> event) {
413+
public TikTokLiveClientBuilder onLinkMicMethod(EventConsumer<TikTokLinkMicMethodEvent> event) {
419414
tikTokEventHandler.subscribe(TikTokLinkMicMethodEvent.class, event);
420415
return this;
421416
}
@@ -487,8 +482,7 @@ public TikTokLiveClientBuilder onShare(EventConsumer<TikTokShareEvent> event) {
487482
return this;
488483
}
489484

490-
public TikTokLiveClientBuilder onUnhandledMember(
491-
EventConsumer<TikTokUnhandledMemberEvent> event) {
485+
public TikTokLiveClientBuilder onUnhandledMember(EventConsumer<TikTokUnhandledMemberEvent> event) {
492486
tikTokEventHandler.subscribe(TikTokUnhandledMemberEvent.class, event);
493487
return this;
494488
}
@@ -498,20 +492,17 @@ public TikTokLiveClientBuilder onSubNotify(EventConsumer<TikTokSubNotifyEvent> e
498492
return this;
499493
}
500494

501-
public TikTokLiveClientBuilder onLinkMicBattle(
502-
EventConsumer<TikTokLinkMicBattleEvent> event) {
495+
public TikTokLiveClientBuilder onLinkMicBattle(EventConsumer<TikTokLinkMicBattleEvent> event) {
503496
tikTokEventHandler.subscribe(TikTokLinkMicBattleEvent.class, event);
504497
return this;
505498
}
506499

507-
public TikTokLiveClientBuilder onDisconnected(
508-
EventConsumer<TikTokDisconnectedEvent> event) {
500+
public TikTokLiveClientBuilder onDisconnected(EventConsumer<TikTokDisconnectedEvent> event) {
509501
tikTokEventHandler.subscribe(TikTokDisconnectedEvent.class, event);
510502
return this;
511503
}
512504

513-
public TikTokLiveClientBuilder onUnhandledControl(
514-
EventConsumer<TikTokUnhandledControlEvent> event) {
505+
public TikTokLiveClientBuilder onUnhandledControl(EventConsumer<TikTokUnhandledControlEvent> event) {
515506
tikTokEventHandler.subscribe(TikTokUnhandledControlEvent.class, event);
516507
return this;
517508
}

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,30 @@ public TikTokLiveHttpClient() {
6666

6767
public GiftsData.Response fetchGiftsData() {
6868
var url = TIKTOK_URL_WEBCAST + "gift/list/";
69+
var proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
70+
if (proxyClientSettings.isEnabled()) {
71+
while (proxyClientSettings.hasNext()) {
72+
try {
73+
var optional = httpFactory.client(url)
74+
.build()
75+
.toJsonResponse();
76+
77+
if (optional.isEmpty()) {
78+
throw new TikTokLiveRequestException("Unable to fetch gifts information's");
79+
}
80+
var json = optional.get();
81+
return giftsDataMapper.map(json);
82+
} catch (TikTokProxyRequestException ignored) {}
83+
}
84+
}
6985
var optional = httpFactory.client(url)
7086
.build()
7187
.toJsonResponse();
7288

7389
if (optional.isEmpty()) {
7490
throw new TikTokLiveRequestException("Unable to fetch gifts information's");
7591
}
92+
7693
var json = optional.get();
7794
return giftsDataMapper.map(json);
7895
}
@@ -85,11 +102,11 @@ public LiveUserData.Response fetchLiveUserData(String userName) {
85102

86103
@Override
87104
public LiveUserData.Response fetchLiveUserData(LiveUserData.Request request) {
105+
var url = TIKTOK_URL_WEB + "api-live/user/room";
88106
var proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
89107
if (proxyClientSettings.isEnabled()) {
90108
while (proxyClientSettings.hasNext()) {
91109
try {
92-
var url = TIKTOK_URL_WEB + "api-live/user/room";
93110
var optional = httpFactory.client(url)
94111
.withParam("uniqueId", request.getUserName())
95112
.withParam("sourceType", "54")
@@ -105,7 +122,6 @@ public LiveUserData.Response fetchLiveUserData(LiveUserData.Request request) {
105122
} catch (TikTokProxyRequestException ignored) {}
106123
}
107124
}
108-
var url = TIKTOK_URL_WEB + "api-live/user/room";
109125
var optional = httpFactory.client(url)
110126
.withParam("uniqueId", request.getUserName())
111127
.withParam("sourceType", "54")
@@ -127,11 +143,11 @@ public LiveData.Response fetchLiveData(String roomId) {
127143

128144
@Override
129145
public LiveData.Response fetchLiveData(LiveData.Request request) {
146+
var url = TIKTOK_URL_WEBCAST + "room/info";
130147
var proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
131148
if (proxyClientSettings.isEnabled()) {
132149
while (proxyClientSettings.hasNext()) {
133150
try {
134-
var url = TIKTOK_URL_WEBCAST + "room/info";
135151
var optional = httpFactory.client(url)
136152
.withParam("room_id", request.getRoomId())
137153
.build()
@@ -146,7 +162,6 @@ public LiveData.Response fetchLiveData(LiveData.Request request) {
146162
} catch (TikTokProxyRequestException ignored) {}
147163
}
148164
}
149-
var url = TIKTOK_URL_WEBCAST + "room/info";
150165
var optional = httpFactory.client(url)
151166
.withParam("room_id", request.getRoomId())
152167
.build()

Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class HttpClient {
3939
protected final String url;
4040
private final Pattern pattern = Pattern.compile("charset=(.*?)(?=&|$)");
4141

42-
public Optional<HttpResponse<byte[]>> toResponse() {
42+
public Optional<HttpResponse<byte[]>> toResponse() {
4343
var client = prepareClient();
4444
var request = prepareGetRequest();
4545
try {

0 commit comments

Comments
 (0)