use of fredboat.audio.queue.PlaylistInfo in project FredBoat by Frederikam.
the class SpotifyAPIWrapper method getPlaylistDataBlocking.
/**
* Returns some data on a spotify playlist, currently it's name and tracks total.
*
* @param userId Spotify user id of the owner of the requested playlist
* @param playlistId Spotify playlist identifier
* @return an array containing information about the requested spotify playlist
*/
public PlaylistInfo getPlaylistDataBlocking(String userId, String playlistId) throws IOException, JSONException {
refreshTokenIfNecessary();
JSONObject jsonPlaylist = BotController.Companion.getHTTP().get(URL_SPOTIFY_API + "/v1/users/" + userId + "/playlists/" + playlistId).auth("Bearer " + accessToken).asJson();
// https://developer.spotify.com/web-api/object-model/#playlist-object-full
String name = jsonPlaylist.getString("name");
int tracks = jsonPlaylist.getJSONObject("tracks").getInt("total");
return new PlaylistInfo(tracks, name, PlaylistInfo.Source.SPOTIFY);
}
use of fredboat.audio.queue.PlaylistInfo in project FredBoat by Frederikam.
the class SpotifyPlaylistSourceManager method loadItem.
@Override
public AudioItem loadItem(final DefaultAudioPlayerManager manager, final AudioReference ar) {
String[] data = parse(ar.identifier);
if (data == null)
return null;
final String spotifyUser = data[0];
final String spotifyListId = data[1];
PlaylistInfo plData;
try {
plData = spotifyAPIWrapper.getPlaylistDataBlocking(spotifyUser, spotifyListId);
} catch (Exception e) {
log.warn("Could not retrieve playlist " + spotifyListId + " of user " + spotifyUser, e);
throw new FriendlyException("Couldn't load playlist. Either Spotify is down or the playlist does not exist.", FriendlyException.Severity.COMMON, e);
}
String playlistName = plData.getName();
if (playlistName == null || "".equals(playlistName))
playlistName = "Spotify Playlist";
int tracksTotal = plData.getTotalTracks();
final List<AudioTrack> trackList = new ArrayList<>();
final List<String> trackListSearchTerms;
try {
trackListSearchTerms = spotifyAPIWrapper.getPlaylistTracksSearchTermsBlocking(spotifyUser, spotifyListId);
} catch (Exception e) {
log.warn("Could not retrieve tracks for playlist " + spotifyListId + " of user " + spotifyUser, e);
throw new FriendlyException("Couldn't load playlist. Either Spotify is down or the playlist does not exist.", FriendlyException.Severity.COMMON, e);
}
log.info("Retrieved playlist data for " + playlistName + " from Spotify, loading up " + tracksTotal + " tracks");
// build a task list
List<CompletableFuture<AudioTrack>> taskList = new ArrayList<>();
for (final String s : trackListSearchTerms) {
// remove all punctuation
final String query = s.replaceAll(TrackSearcher.PUNCTUATION_REGEX, "");
CompletableFuture<AudioTrack> f = CompletableFuture.supplyAsync(() -> searchSingleTrack(query), loader);
taskList.add(f);
}
// build a tracklist from that task list
for (CompletableFuture<AudioTrack> futureTrack : taskList) {
try {
final AudioTrack audioItem = futureTrack.get();
if (audioItem == null) {
// skip the track if we couldn't find it
continue;
}
trackList.add(audioItem);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
} catch (ExecutionException ignored) {
// this is fine, loop will go for the next item
}
}
return new BasicAudioPlaylist(playlistName, trackList, null, true);
}
Aggregations