Skip to content

Commit c0f51c2

Browse files
authored
load arl from track user data if present (#253)
1 parent 549ddd8 commit c0f51c2

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

main/src/main/java/com/github/topi314/lavasrc/deezer/DeezerAudioTrack.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
1616
import com.sedmelluq.discord.lavaplayer.track.InternalAudioTrack;
1717
import com.sedmelluq.discord.lavaplayer.track.playback.LocalAudioTrackExecutor;
18+
import kotlinx.serialization.json.JsonObject;
19+
import kotlinx.serialization.json.JsonPrimitive;
1820
import org.apache.commons.codec.binary.Hex;
1921
import org.apache.http.client.CookieStore;
2022
import org.apache.http.client.config.RequestConfig;
@@ -57,13 +59,13 @@ private static String formatFormats(TrackFormat[] formats) {
5759
return String.join(",", strFormats);
5860
}
5961

60-
private JsonBrowser getJsonResponse(HttpUriRequest request, boolean useArl) throws IOException {
62+
private JsonBrowser getJsonResponse(HttpUriRequest request, String arl) throws IOException {
6163
try (HttpInterface httpInterface = this.sourceManager.getHttpInterface()) {
6264
httpInterface.getContext().setRequestConfig(RequestConfig.custom().setCookieSpec("standard").build());
6365
httpInterface.getContext().setCookieStore(cookieStore);
6466

65-
if (useArl && this.sourceManager.getArl() != null) {
66-
request.setHeader("Cookie", "arl=" + this.sourceManager.getArl());
67+
if (arl != null) {
68+
request.setHeader("Cookie", "arl=" + arl);
6769
}
6870

6971
return LavaSrcTools.fetchResponseAsJson(httpInterface, request);
@@ -72,21 +74,21 @@ private JsonBrowser getJsonResponse(HttpUriRequest request, boolean useArl) thro
7274

7375
private String getSessionId() throws IOException {
7476
var getSessionID = new HttpPost(DeezerAudioSourceManager.PRIVATE_API_BASE + "?method=deezer.ping&input=3&api_version=1.0&api_token=");
75-
var sessionIdJson = this.getJsonResponse(getSessionID, false);
77+
var sessionIdJson = this.getJsonResponse(getSessionID, null);
7678

7779
DeezerAudioSourceManager.checkResponse(sessionIdJson, "Failed to get session ID: ");
7880
return sessionIdJson.get("results").get("SESSION").text();
7981
}
8082

81-
private LicenseToken generateLicenceToken(boolean useArl) throws IOException {
83+
private LicenseToken generateLicenceToken(String arl) throws IOException {
8284
var request = new HttpGet(DeezerAudioSourceManager.PRIVATE_API_BASE + "?method=deezer.getUserData&input=3&api_version=1.0&api_token=");
8385

8486
// session ID is not needed with ARL and vice-versa.
85-
if (!useArl || this.sourceManager.getArl() == null) {
87+
if (arl == null) {
8688
request.setHeader("Cookie", "sid=" + this.getSessionId());
8789
}
8890

89-
var json = this.getJsonResponse(request, useArl);
91+
var json = this.getJsonResponse(request, arl);
9092
DeezerAudioSourceManager.checkResponse(json, "Failed to get user token: ");
9193

9294
return new LicenseToken(
@@ -95,30 +97,30 @@ private LicenseToken generateLicenceToken(boolean useArl) throws IOException {
9597
);
9698
}
9799

98-
public SourceWithFormat getSource(boolean useArl, boolean isRetry) throws IOException, URISyntaxException {
99-
var licenseToken = this.generateLicenceToken(useArl);
100+
public SourceWithFormat getSource(String arl, boolean isRetry) throws IOException, URISyntaxException {
101+
var licenseToken = this.generateLicenceToken(arl);
100102

101103
var getTrackToken = new HttpPost(DeezerAudioSourceManager.PRIVATE_API_BASE + "?method=song.getData&input=3&api_version=1.0&api_token=" + licenseToken.apiToken);
102104
getTrackToken.setEntity(new StringEntity("{\"sng_id\":\"" + this.trackInfo.identifier + "\"}", ContentType.APPLICATION_JSON));
103-
var trackTokenJson = this.getJsonResponse(getTrackToken, useArl);
105+
var trackTokenJson = this.getJsonResponse(getTrackToken, arl);
104106
DeezerAudioSourceManager.checkResponse(trackTokenJson, "Failed to get track token: ");
105107

106108
if (trackTokenJson.get("error").get("VALID_TOKEN_REQUIRED").text() != null && !isRetry) {
107109
// "error":{"VALID_TOKEN_REQUIRED":"Invalid CSRF token"}
108110
// seems to indicate an invalid API token?
109-
return this.getSource(useArl, true);
111+
return this.getSource(arl, true);
110112
}
111113

112114
var trackToken = trackTokenJson.get("results").get("TRACK_TOKEN").text();
113115

114116
var getMediaURL = new HttpPost(DeezerAudioSourceManager.MEDIA_BASE + "/get_url");
115117
getMediaURL.setEntity(new StringEntity("{\"license_token\":\"" + licenseToken.userLicenseToken + "\",\"media\":[{\"type\":\"FULL\",\"formats\":[" + formatFormats(this.sourceManager.getFormats()) + "]}],\"track_tokens\": [\"" + trackToken + "\"]}", ContentType.APPLICATION_JSON));
116118

117-
var json = this.getJsonResponse(getMediaURL, useArl);
119+
var json = this.getJsonResponse(getMediaURL, arl);
118120
for (var error : json.get("data").get("errors").values()) {
119121
if (error.get("code").asLong(0) == 2000) {
120122
// error code 2000 = failed to decode track token
121-
return this.getSource(useArl, true);
123+
return this.getSource(arl, true);
122124
}
123125
}
124126
DeezerAudioSourceManager.checkResponse(json, "Failed to get media URL: ");
@@ -151,7 +153,19 @@ public void process(LocalAudioTrackExecutor executor) throws Exception {
151153
return;
152154
}
153155

154-
var source = this.getSource(this.sourceManager.getArl() != null, false);
156+
String arl = null;
157+
var userData = this.getUserData(JsonObject.class);
158+
if (userData != null && userData.containsKey("arl")) {
159+
var userDataArl = userData.get("arl");
160+
if (userDataArl instanceof JsonPrimitive && ((JsonPrimitive) userDataArl).isString()) {
161+
arl = ((JsonPrimitive) userDataArl).getContent();
162+
}
163+
}
164+
if (arl == null) {
165+
arl = this.sourceManager.getArl();
166+
}
167+
168+
var source = this.getSource(arl, false);
155169
try (var stream = new DeezerPersistentHttpStream(httpInterface, source.url, source.contentLength, this.getTrackDecryptionKey())) {
156170
processDelegate(source.format.trackFactory.apply(this.trackInfo, stream), executor);
157171
}

main/src/main/java/com/github/topi314/lavasrc/mirror/MirroringAudioTrack.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public void process(LocalAudioTrackExecutor executor) throws Exception {
5050
track = tracks.get(0);
5151
}
5252
if (track instanceof InternalAudioTrack) {
53+
((InternalAudioTrack) track).setUserData(this.getUserData());
5354
var internalTrack = (InternalAudioTrack) track;
5455
log.debug("Loaded track mirror from {} {}({}) ", internalTrack.getSourceManager().getSourceName(), internalTrack.getInfo().title, internalTrack.getInfo().uri);
5556
processDelegate(internalTrack, executor);

0 commit comments

Comments
 (0)