Skip to content

Commit

Permalink
add support for spotify short links
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Nov 22, 2023
1 parent b9c5117 commit 097c18e
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class SpotifySourceManager extends MirroringAudioSourceManager implements
public static final String RECOMMENDATIONS_PREFIX = "sprec:";
public static final String PREVIEW_PREFIX = "spprev:";
public static final long PREVIEW_LENGTH = 30000;
public static final String SHARE_URL = "https://spotify.link/";
public static final int PLAYLIST_MAX_PAGE_ITEMS = 100;
public static final int ALBUM_MAX_PAGE_ITEMS = 50;
public static final String API_BASE = "https://api.spotify.com/v1/";
Expand Down Expand Up @@ -146,6 +147,21 @@ public AudioItem loadItem(String identifier, boolean preview) {
return this.getRecommendations(identifier.substring(RECOMMENDATIONS_PREFIX.length()).trim(), preview);
}

// If the identifier is a share URL, we need to follow the redirect to find out the real url behind it
if (identifier.startsWith(SHARE_URL)) {
var request = new HttpGet(identifier);
request.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build());
try (var response = this.httpInterfaceManager.getInterface().execute(request)) {
if (response.getStatusLine().getStatusCode() == 307) {
var location = response.getFirstHeader("Location").getValue();
if (location.startsWith("https://open.spotify.com/")) {
return this.loadItem(location, preview);
}
}
return null;
}
}

var matcher = URL_PATTERN.matcher(identifier);
if (!matcher.find()) {
return null;
Expand Down

0 comments on commit 097c18e

Please sign in to comment.