use of se.michaelthelin.spotify.model_objects.specification.Episode in project aiode by robinfriedli.
the class PlayableFactory method createPlayable.
/**
* Create a single playable for any url. If the url is either an open.spotify or YouTube URL this extracts the ID
* and uses the familiar methods to load the Playables, otherwise this method uses the {@link AudioTrackLoader}
* to load the {@link AudioTrack}s using lavaplayer and wraps them in {@link UrlPlayable}s
*/
@Nullable
public Playable createPlayable(String url, SpotifyApi spotifyApi, boolean redirectSpotify) throws IOException {
URI uri;
try {
uri = URI.create(url);
} catch (IllegalArgumentException e) {
throw new InvalidCommandException("'" + url + "' is not a valid URL");
}
if (uri.getHost().contains("youtube.com")) {
Map<String, String> parameterMap = getParameterMap(uri);
String videoId = parameterMap.get("v");
if (videoId != null) {
return youTubeService.getVideoForId(videoId);
} else {
throw new IllegalArgumentException("Detected YouTube URL but no video id provided");
}
} else if (uri.getHost().equals("youtu.be")) {
String[] parts = uri.getPath().split("/");
return youTubeService.requireVideoForId(parts[parts.length - 1]);
} else if (uri.getHost().equals("open.spotify.com")) {
StringList pathFragments = StringList.createWithRegex(uri.getPath(), "/");
SpotifyTrackKind kind;
String trackId;
if (pathFragments.contains("track")) {
trackId = pathFragments.tryGet(pathFragments.indexOf("track") + 1);
kind = SpotifyTrackKind.TRACK;
} else if (pathFragments.contains("episode")) {
trackId = pathFragments.tryGet(pathFragments.indexOf("episode") + 1);
kind = SpotifyTrackKind.EPISODE;
} else {
throw new IllegalArgumentException("Detected Spotify URL but no track id provided");
}
if (trackId == null) {
throw new InvalidCommandException("No track id provided");
}
try {
String accessToken = spotifyApi.clientCredentials().build().execute().getAccessToken();
spotifyApi.setAccessToken(accessToken);
if (kind == SpotifyTrackKind.TRACK) {
Track track = spotifyService.getTrack(trackId);
return createPlayable(redirectSpotify, track);
} else // noinspection ConstantConditions
if (kind == SpotifyTrackKind.EPISODE) {
Episode episode = spotifyService.getEpisode(trackId);
return createPlayable(redirectSpotify, episode);
} else {
throw new UnsupportedOperationException("unsupported open.spotify URL kind: " + kind);
}
} catch (IOException | SpotifyWebApiException | ParseException e) {
throw new RuntimeException("Exception during Spotify request", e);
} finally {
spotifyApi.setAccessToken(null);
}
} else {
AudioItem audioItem = audioTrackLoader.loadByIdentifier(uri.toString());
if (audioItem instanceof AudioTrack) {
return new UrlPlayable((AudioTrack) audioItem);
} else if (audioItem != null) {
throw new IllegalArgumentException("Loading provided url did not result in an AudioTrack but " + audioItem.getClass().toString());
} else {
return null;
}
}
}
Aggregations