Skip to content

Commit

Permalink
add limit option for playlist & album sizes to spotify & apple music (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 authored Jan 13, 2023
1 parent ef1cd41 commit 412ba0c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 6 deletions.
4 changes: 4 additions & 0 deletions application.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ plugins:
clientId: "your client id"
clientSecret: "your client secret"
countryCode: "US" # the country code you want to use for filtering the artists top tracks. See https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
playlistLoadLimit: 6 # The number of pages at 100 tracks each
albumLoadLimit: 6 # The number of pages at 50 tracks each
applemusic:
countryCode: "US" # the country code you want to use for filtering the artists top tracks and language. See https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
mediaAPIToken: "..." # Can be used to bypass the auto token fetching which is likely to break again in the future
playlistLoadLimit: 6 # The number of pages at 300 tracks each
albumLoadLimit: 6 # The number of pages at 300 tracks each
deezer:
masterDecryptionKey: "your master decryption key" # the master key used for decrypting the deezer tracks. (yes this is not here you need to get it from somewhere else)
yandexmusic:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class AppleMusicSourceManager extends MirroringAudioSourceManager impleme
private static final Logger log = LoggerFactory.getLogger(AppleMusicSourceManager.class);
private final HttpInterfaceManager httpInterfaceManager = HttpClientTools.createDefaultThreadLocalManager();
private final String countryCode;
private int playlistPageLimit;
private int albumPageLimit;
private String token;
private String origin;
private Instant tokenExpire;
Expand All @@ -67,6 +69,14 @@ public AppleMusicSourceManager(String mediaAPIToken, String countryCode, AudioPl
}
}

public void setPlaylistPageLimit(int playlistPageLimit) {
this.playlistPageLimit = playlistPageLimit;
}

public void setAlbumPageLimit(int albumPageLimit) {
this.albumPageLimit = albumPageLimit;
}

@Override
public String getSourceName() {
return "applemusic";
Expand Down Expand Up @@ -187,13 +197,14 @@ public AudioItem getAlbum(String id, String countryCode) throws IOException {
var tracks = new ArrayList<AudioTrack>();
JsonBrowser page;
var offset = 0;
var pages = 0;
do {
page = this.getJson(API_BASE + "catalog/" + countryCode + "/albums/" + id + "/tracks?limit=" + MAX_PAGE_ITEMS + "&offset=" + offset);
offset += MAX_PAGE_ITEMS;

tracks.addAll(parseTracks(page));
}
while (page.get("next").text() != null);
while (page.get("next").text() != null && ++pages < albumPageLimit);

if (tracks.isEmpty()) {
return AudioReference.NO_TRACK;
Expand All @@ -211,13 +222,14 @@ public AudioItem getPlaylist(String id, String countryCode) throws IOException {
var tracks = new ArrayList<AudioTrack>();
JsonBrowser page;
var offset = 0;
var pages = 0;
do {
page = this.getJson(API_BASE + "catalog/" + countryCode + "/playlists/" + id + "/tracks?limit=" + MAX_PAGE_ITEMS + "&offset=" + offset);
offset += MAX_PAGE_ITEMS;

tracks.addAll(parseTracks(page));
}
while (page.get("next").text() != null);
while (page.get("next").text() != null && ++pages < playlistPageLimit);

if (tracks.isEmpty()) {
return AudioReference.NO_TRACK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class SpotifySourceManager extends MirroringAudioSourceManager implements
private final String clientId;
private final String clientSecret;
private final String countryCode;
private int playlistPageLimit = 6;
private int albumPageLimit = 6;
private String token;
private Instant tokenExpire;

Expand All @@ -76,6 +78,14 @@ public SpotifySourceManager(String clientId, String clientSecret, String country
this.countryCode = countryCode;
}

public void setPlaylistPageLimit(int playlistPageLimit) {
this.playlistPageLimit = playlistPageLimit;
}

public void setAlbumPageLimit(int albumPageLimit) {
this.albumPageLimit = albumPageLimit;
}

@Override
public String getSourceName() {
return "spotify";
Expand Down Expand Up @@ -176,6 +186,7 @@ public AudioItem getAlbum(String id) throws IOException {
var tracks = new ArrayList<AudioTrack>();
JsonBrowser page;
var offset = 0;
var pages = 0;
do {
page = this.getJson(API_BASE + "albums/" + id + "/tracks?limit=" + ALBUM_MAX_PAGE_ITEMS + "&offset=" + offset);
offset += ALBUM_MAX_PAGE_ITEMS;
Expand All @@ -184,7 +195,7 @@ public AudioItem getAlbum(String id) throws IOException {

tracks.addAll(this.parseTracks(tracksPage));
}
while (page.get("next").text() != null);
while (page.get("next").text() != null && ++pages < this.albumPageLimit);

if (tracks.isEmpty()) {
return AudioReference.NO_TRACK;
Expand All @@ -203,6 +214,7 @@ public AudioItem getPlaylist(String id) throws IOException {
var tracks = new ArrayList<AudioTrack>();
JsonBrowser page;
var offset = 0;
var pages = 0;
do {
page = this.getJson(API_BASE + "playlists/" + id + "/tracks?limit=" + PLAYLIST_MAX_PAGE_ITEMS + "&offset=" + offset);
offset += PLAYLIST_MAX_PAGE_ITEMS;
Expand All @@ -216,7 +228,7 @@ public AudioItem getPlaylist(String id) throws IOException {
}

}
while (page.get("next").text() != null);
while (page.get("next").text() != null && ++pages < this.playlistPageLimit);

if (tracks.isEmpty()) {
return AudioReference.NO_TRACK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class AppleMusicConfig {

private String countryCode = "us";
private String mediaAPIToken;
private int playlistLoadLimit;
private int albumLoadLimit;

public String getCountryCode() {
return this.countryCode;
Expand All @@ -26,4 +28,19 @@ public void setMediaAPIToken(String mediaAPIToken) {
this.mediaAPIToken = mediaAPIToken;
}

public int getPlaylistLoadLimit() {
return this.playlistLoadLimit;
}

public void setPlaylistLoadLimit(int playlistLoadLimit) {
this.playlistLoadLimit = playlistLoadLimit;
}

public int getAlbumLoadLimit() {
return this.albumLoadLimit;
}

public void setAlbumLoadLimit(int albumLoadLimit) {
this.albumLoadLimit = albumLoadLimit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,25 @@ public LavaSrcPlugin(LavaSrcConfig pluginConfig, SourcesConfig sourcesConfig, Sp
public AudioPlayerManager configure(AudioPlayerManager manager) {
if (this.sourcesConfig.isSpotify()) {
log.info("Registering Spotify audio source manager...");
manager.registerSourceManager(new SpotifySourceManager(this.pluginConfig.getProviders(), this.spotifyConfig.getClientId(), this.spotifyConfig.getClientSecret(), this.spotifyConfig.getCountryCode(), manager));
var spotifySourceManager = new SpotifySourceManager(this.pluginConfig.getProviders(), this.spotifyConfig.getClientId(), this.spotifyConfig.getClientSecret(), this.spotifyConfig.getCountryCode(), manager);
if (this.spotifyConfig.getPlaylistLoadLimit() > 0) {
spotifySourceManager.setPlaylistPageLimit(this.spotifyConfig.getPlaylistLoadLimit());
}
if (this.spotifyConfig.getAlbumLoadLimit() > 0) {
spotifySourceManager.setAlbumPageLimit(this.spotifyConfig.getAlbumLoadLimit());
}
manager.registerSourceManager(spotifySourceManager);
}
if (this.sourcesConfig.isAppleMusic()) {
log.info("Registering Apple Music audio source manager...");
manager.registerSourceManager(new AppleMusicSourceManager(this.pluginConfig.getProviders(), this.appleMusicConfig.getMediaAPIToken(), this.appleMusicConfig.getCountryCode(), manager));
var appleMusicSourceManager = new AppleMusicSourceManager(this.pluginConfig.getProviders(), this.appleMusicConfig.getMediaAPIToken(), this.appleMusicConfig.getCountryCode(), manager);
if (this.appleMusicConfig.getPlaylistLoadLimit() > 0) {
appleMusicSourceManager.setPlaylistPageLimit(this.appleMusicConfig.getPlaylistLoadLimit());
}
if (this.appleMusicConfig.getAlbumLoadLimit() > 0) {
appleMusicSourceManager.setAlbumPageLimit(this.appleMusicConfig.getAlbumLoadLimit());
}
manager.registerSourceManager(appleMusicSourceManager);
}
if (this.sourcesConfig.isDeezer()) {
log.info("Registering Deezer audio source manager...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class SpotifyConfig {
private String clientId;
private String clientSecret;
private String countryCode;
private int playlistLoadLimit;
private int albumLoadLimit;

public String getClientId() {
return this.clientId;
Expand All @@ -35,4 +37,20 @@ public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}

public int getPlaylistLoadLimit() {
return this.playlistLoadLimit;
}

public void setPlaylistLoadLimit(int playlistLoadLimit) {
this.playlistLoadLimit = playlistLoadLimit;
}

public int getAlbumLoadLimit() {
return this.albumLoadLimit;
}

public void setAlbumLoadLimit(int albumLoadLimit) {
this.albumLoadLimit = albumLoadLimit;
}

}

0 comments on commit 412ba0c

Please sign in to comment.