Skip to content

Commit

Permalink
port soundcloud short url to lavaplayer v2 (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: Freya Arbjerg <freya@arbjerg.dev>
  • Loading branch information
topi314 and freyacodes authored Aug 1, 2023
1 parent 222aa39 commit 0106503
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class SoundCloudAudioSourceManager implements AudioSourceManager, HttpCon

private static final String MOBILE_URL_REGEX = "^(?:http://|https://|)soundcloud\\.app\\.goo\\.gl/([a-zA-Z0-9-_]+)/?(?:\\?.*|)$";
private static final String TRACK_URL_REGEX = "^(?:http://|https://|)(?:www\\.|)(?:m\\.|)soundcloud\\.com/([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)/?(?:\\?.*|)$";
private static final String SHORT_TRACK_URL_REGEX = "^https://on.soundcloud\\.com/[a-zA-Z0-9-_]+/?(?:\\?.*|)$";
private static final String UNLISTED_URL_REGEX = "^(?:http://|https://|)(?:www\\.|)(?:m\\.|)soundcloud\\.com/([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)/s-([a-zA-Z0-9-_]+)(?:\\?.*|)$";
private static final String LIKED_URL_REGEX = "^(?:http://|https://|)(?:www\\.|)(?:m\\.|)soundcloud\\.com/([a-zA-Z0-9-_]+)/likes/?(?:\\?.*|)$";
private static final String LIKED_USER_URN_REGEX = "\"urn\":\"soundcloud:users:([0-9]+)\",\"username\":\"([^\"]+)\"";
Expand All @@ -56,6 +57,7 @@ public class SoundCloudAudioSourceManager implements AudioSourceManager, HttpCon

private static final Pattern mobileUrlPattern = Pattern.compile(MOBILE_URL_REGEX);
private static final Pattern trackUrlPattern = Pattern.compile(TRACK_URL_REGEX);
private static final Pattern shortTrackUrlPattern = Pattern.compile(SHORT_TRACK_URL_REGEX);
private static final Pattern unlistedUrlPattern = Pattern.compile(UNLISTED_URL_REGEX);
private static final Pattern likedUrlPattern = Pattern.compile(LIKED_URL_REGEX);
private static final Pattern likedUserUrnPattern = Pattern.compile(LIKED_USER_URN_REGEX);
Expand Down Expand Up @@ -120,6 +122,11 @@ public AudioItem loadItem(AudioPlayerManager manager, AudioReference reference)
reference = SoundCloudHelper.redirectMobileLink(httpInterfaceManager.getInterface(), reference);
}

Matcher shortTrackMatcher = shortTrackUrlPattern.matcher(reference.identifier);
if (shortTrackMatcher.matches()) {
reference = SoundCloudHelper.resolveShortTrackUrl(httpInterfaceManager.getInterface(), reference);
}

AudioItem track = processAsSingleTrack(reference);

if (track == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface;
import com.sedmelluq.discord.lavaplayer.tools.io.PersistentHttpStream;
import com.sedmelluq.discord.lavaplayer.track.AudioReference;
import org.apache.http.Header;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.protocol.HttpClientContext;

import java.io.IOException;
Expand Down Expand Up @@ -52,4 +55,19 @@ public static AudioReference redirectMobileLink(HttpInterface httpInterface, Aud
throw ExceptionTools.wrapUnfriendlyExceptions(e);
}
}

public static AudioReference resolveShortTrackUrl(HttpInterface httpInterface, AudioReference reference) {
HttpHead request = new HttpHead(reference.identifier);
request.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build());
try (CloseableHttpResponse response = httpInterface.execute(request)) {
Header header = response.getLastHeader("Location");
if (header == null) {
throw new FriendlyException("Unable to resolve Soundcloud short URL", SUSPICIOUS,
new IllegalStateException("Unable to locate canonical URL"));
}
return new AudioReference(header.getValue(), null);
} catch (Exception e) {
throw ExceptionTools.wrapUnfriendlyExceptions(e);
}
}
}

0 comments on commit 0106503

Please sign in to comment.