Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

port soundcloud short url to lavaplayer v2 #4

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}