Skip to content

Commit 4aa02b3

Browse files
committed
Add visitorData option to config
1 parent d10c18f commit 4aa02b3

File tree

6 files changed

+75
-23
lines changed

6 files changed

+75
-23
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,10 @@ Specifying the token is as simple as doing:
203203
```yaml
204204
plugins:
205205
youtube:
206-
poToken: "paste your token here"
206+
pot:
207+
token: "paste your po_token here"
208+
visitorData: "paste your visitor_data here"
207209
```
208-
You do **not** need the `visitor_data` that is also produced by the script.
209210

210211
> [!NOTE]
211212
> A `poToken` is not a silver bullet, and currently it only applies to requests made via the `WEB` client.

common/src/main/java/dev/lavalink/youtube/clients/ClientConfig.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,31 @@ public ClientConfig withUserAgent(@NotNull String userAgent) {
7373
return this;
7474
}
7575

76-
public ClientConfig withVisitorData(@NotNull String visitorData) {
76+
public ClientConfig withVisitorData(@Nullable String visitorData) {
7777
this.visitorData = visitorData;
78-
withClientField("visitorData", visitorData);
78+
79+
if (visitorData != null) {
80+
withClientField("visitorData", visitorData);
81+
} else {
82+
Map<String, Object> context = (Map<String, Object>) root.get("context");
83+
84+
if (context != null) {
85+
Map<String, Object> client = (Map<String, Object>) context.get("client");
86+
87+
if (client != null) {
88+
client.remove("visitorData");
89+
90+
if (client.isEmpty()) {
91+
context.remove("client");
92+
}
93+
}
94+
95+
if (context.isEmpty()) {
96+
root.remove("context");
97+
}
98+
}
99+
}
100+
79101
return this;
80102
}
81103

common/src/main/java/dev/lavalink/youtube/clients/Web.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,18 @@ public Web(@NotNull ClientOptions options) {
5151
this.options = options;
5252
}
5353

54-
public static void setPoToken(String poToken) {
54+
public static void setPoTokenAndVisitorData(String poToken, String visitorData) {
5555
Web.poToken = poToken;
5656

57-
if (poToken == null) {
57+
if (poToken == null || visitorData == null) {
5858
BASE_CONFIG.getRoot().remove("serviceIntegrityDimensions");
59+
BASE_CONFIG.withVisitorData(null);
5960
return;
6061
}
6162

6263
Map<String, Object> sid = BASE_CONFIG.putOnceAndJoin(BASE_CONFIG.getRoot(), "serviceIntegrityDimensions");
6364
sid.put("poToken", poToken);
65+
BASE_CONFIG.withVisitorData(visitorData);
6466
}
6567

6668
protected void fetchClientConfig(@NotNull HttpInterface httpInterface) {
@@ -109,12 +111,11 @@ protected void fetchClientConfig(@NotNull HttpInterface httpInterface) {
109111
BASE_CONFIG.withClientField("clientVersion", clientVersion);
110112
}
111113

112-
String visitorData = client.get("visitorData").text();
113-
114-
if (visitorData != null && !visitorData.isEmpty() && BASE_CONFIG.getVisitorData() == null) {
115-
// don't overwrite if visitorData was already set.
116-
BASE_CONFIG.withVisitorData(visitorData);
117-
}
114+
// String visitorData = client.get("visitorData").text();
115+
//
116+
// if (visitorData != null && !visitorData.isEmpty()) {
117+
// BASE_CONFIG.withVisitorData(visitorData);
118+
// }
118119
}
119120
} catch (IOException e) {
120121
throw ExceptionTools.toRuntimeException(e);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package dev.lavalink.youtube.plugin;
2+
3+
public class Pot {
4+
private String token;
5+
private String visitorData;
6+
7+
public String getToken() {
8+
return token != null && !token.isEmpty() ? token : null;
9+
}
10+
11+
public String getVisitorData() {
12+
return visitorData != null && !visitorData.isEmpty() ? visitorData : null;
13+
}
14+
15+
public void setPoToken(String token) {
16+
this.token = token;
17+
}
18+
19+
public void setVisitorData(String visitorData) {
20+
this.visitorData = visitorData;
21+
}
22+
}

plugin/src/main/java/dev/lavalink/youtube/plugin/YoutubeConfig.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class YoutubeConfig {
1414
private boolean allowSearch = true;
1515
private boolean allowDirectVideoIds = true;
1616
private boolean allowDirectPlaylistIds = true;
17-
private String poToken;
17+
private Pot pot = null;
1818
private String[] clients;
1919
private Map<String, ClientOptions> clientOptions = new HashMap<>();
2020

@@ -34,8 +34,8 @@ public boolean getAllowDirectPlaylistIds() {
3434
return allowDirectPlaylistIds;
3535
}
3636

37-
public String getPoToken() {
38-
return poToken;
37+
public Pot getPot() {
38+
return pot;
3939
}
4040

4141
public String[] getClients() {
@@ -62,8 +62,8 @@ public void setAllowDirectPlaylistIds(boolean allowDirectPlaylistIds) {
6262
this.allowDirectPlaylistIds = allowDirectPlaylistIds;
6363
}
6464

65-
public void setPoToken(String poToken) {
66-
this.poToken = poToken;
65+
public void setPot(Pot pot) {
66+
this.pot = pot;
6767
}
6868

6969
public void setClients(String[] clients) {

plugin/src/main/java/dev/lavalink/youtube/plugin/YoutubePluginLoader.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.sedmelluq.lava.extensions.youtuberotator.tools.ip.Ipv6Block;
99
import dev.arbjerg.lavalink.api.AudioPlayerManagerConfiguration;
1010
import dev.lavalink.youtube.YoutubeAudioSourceManager;
11-
import dev.lavalink.youtube.clients.ClientConfig;
1211
import dev.lavalink.youtube.clients.ClientOptions;
1312
import dev.lavalink.youtube.clients.Web;
1413
import dev.lavalink.youtube.clients.skeleton.Client;
@@ -163,11 +162,18 @@ public AudioPlayerManager configure(AudioPlayerManager audioPlayerManager) {
163162
clients = clientProvider.getDefaultClients();
164163
} else {
165164
clients = youtubeConfig.getClients();
166-
String poToken = youtubeConfig.getPoToken();
167-
168-
if (poToken != null && !poToken.isEmpty()) {
169-
log.debug("Setting poToken for WEB client to {}", poToken);
170-
Web.setPoToken(poToken);
165+
Pot pot = youtubeConfig.getPot();
166+
167+
if (pot != null) {
168+
String token = pot.getToken();
169+
String visitorData = pot.getVisitorData();
170+
171+
if (token != null && visitorData != null) {
172+
log.debug("Applying poToken and visitorData to WEB client (token: {}, vd: {})", token, visitorData);
173+
Web.setPoTokenAndVisitorData(token, visitorData);
174+
} else if (token != null || visitorData != null) {
175+
log.warn("Both pot.token and pot.visitorData must be specified and valid for pot to apply.");
176+
}
171177
}
172178
}
173179

0 commit comments

Comments
 (0)