diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/soundcloud/SoundCloudAudioSourceManager.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/soundcloud/SoundCloudAudioSourceManager.java index 75ef1c0c..93e88e87 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/soundcloud/SoundCloudAudioSourceManager.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/soundcloud/SoundCloudAudioSourceManager.java @@ -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\":\"([^\"]+)\""; @@ -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); @@ -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) { diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/soundcloud/SoundCloudHelper.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/soundcloud/SoundCloudHelper.java index 61142a02..8ea5fc64 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/soundcloud/SoundCloudHelper.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/soundcloud/SoundCloudHelper.java @@ -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; @@ -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); + } + } }