use of org.apache.hc.core5.http.ParseException in project ascent_bot by FeroniK.
the class SpotifyAudioSource method getPlaylist.
public void getPlaylist(CommandContext ctx, String url) {
final Matcher res = SPOTIFY_PLAYLIST_REGEX.matcher(url);
if (!res.matches()) {
return;
}
try {
GuildMusicManager musicManager = PlayerManager.getInstance().getMusicManager(ctx.getGuild());
final Playlist spotifyPlaylist = this.spi.getPlaylist(res.group(res.groupCount())).build().execute();
List<PlaylistTrack> playlistTracks = List.of(spotifyPlaylist.getTracks().getItems());
if (playlistTracks.isEmpty()) {
return;
}
final int originalSize = playlistTracks.size();
if (originalSize > musicManager.scheduler.MAX_QUEUE_SIZE) {
playlistTracks = playlistTracks.subList(0, musicManager.scheduler.MAX_QUEUE_SIZE);
}
final List<Track> finalPlaylist = new ArrayList<>();
for (final PlaylistTrack playlistTrack : playlistTracks) {
if (playlistTrack.getIsLocal()) {
continue;
}
final IPlaylistItem item = playlistTrack.getTrack();
if (item instanceof Track) {
Track track = (Track) item;
finalPlaylist.add(track);
}
}
ctx.getChannel().sendMessageEmbeds(Embeds.createBuilder(null, "Spotify Playlist Loaded : Adding " + finalPlaylist.size() + " Tracks to the queue", null, null, null).build()).queue();
for (Track track : finalPlaylist) {
final String query = "ytsearch:" + track.getName() + " " + track.getArtists()[0].getName();
PlayerManager.getInstance().audioPlayerManager.loadItemOrdered(musicManager, query, new AudioLoadResultHandler() {
@Override
public void trackLoaded(AudioTrack track) {
}
@Override
public void playlistLoaded(AudioPlaylist playlist) {
boolean empty = playlist.getTracks().isEmpty();
if (empty) {
return;
}
AudioTrack audioTrack = playlist.getTracks().get(0);
audioTrack.setUserData(ctx.getAuthor().getIdLong());
musicManager.scheduler.queue(audioTrack);
}
@Override
public void noMatches() {
}
@Override
public void loadFailed(FriendlyException exception) {
}
});
}
} catch (IOException | SpotifyWebApiException | ParseException e) {
e.printStackTrace();
}
}
use of org.apache.hc.core5.http.ParseException in project aiode by robinfriedli.
the class SetRedirectedSpotifyTrackNameTask method perform.
@Override
public void perform(@Nullable JDA shard) throws Exception {
ClientCredentials clientCredentials = spotifyApi.clientCredentials().build().execute();
spotifyApi.setAccessToken(clientCredentials.getAccessToken());
File file = new File("src/main/resources/playlists.xml");
if (file.exists()) {
try (Context context = jxpBackend.getContext(file)) {
context.invoke(() -> {
try {
migrate(context, spotifyApi);
} catch (IOException | SpotifyWebApiException | ParseException e) {
throw new RuntimeException(e);
}
});
}
}
spotifyApi.setAccessToken(null);
}
use of org.apache.hc.core5.http.ParseException 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