use of net.robinfriedli.aiode.exceptions.UnavailableResourceException in project aiode by robinfriedli.
the class YouTubeService method loadDurationsAsync.
private void loadDurationsAsync(List<HollowYouTubeVideo> videos) {
// ids have already been loaded in other thread
List<String> videoIds = Lists.newArrayList();
for (HollowYouTubeVideo hollowYouTubeVideo : videos) {
String id;
try {
id = hollowYouTubeVideo.getVideoId();
} catch (UnavailableResourceException e) {
continue;
}
videoIds.add(id);
}
if (videoIds.isEmpty()) {
return;
}
EagerFetchQueue.submitFetch(() -> {
try {
Map<String, Long> durationMillis = getDurationMillis(videoIds);
for (HollowYouTubeVideo video : videos) {
Long duration;
try {
duration = durationMillis.get(video.getVideoId());
} catch (UnavailableResourceException e) {
continue;
}
video.setDuration(duration != null ? duration : 0);
}
} catch (IOException e) {
throw new RuntimeException("Exception occurred while loading durations", e);
}
});
}
use of net.robinfriedli.aiode.exceptions.UnavailableResourceException in project aiode by robinfriedli.
the class SpotifyRedirectService method redirectTrack.
public void redirectTrack(HollowYouTubeVideo youTubeVideo) throws IOException {
SpotifyTrack spotifyTrack = youTubeVideo.getRedirectedSpotifyTrack();
if (spotifyTrack == null) {
throw new IllegalArgumentException(youTubeVideo.toString() + " is not a placeholder for a redirected Spotify Track");
}
// early exit to avoid duplicate loading of Playables that have been loaded prioritised by invoking Playable#fetchNow
if (youTubeVideo.isDone()) {
return;
}
youTubeVideo.markLoading();
String spotifyTrackId = spotifyTrack.getId();
Optional<SpotifyRedirectIndex> persistedSpotifyRedirectIndex;
if (!Strings.isNullOrEmpty(spotifyTrackId)) {
persistedSpotifyRedirectIndex = queryExistingIndex(session, spotifyTrackId);
} else {
persistedSpotifyRedirectIndex = Optional.empty();
}
if (persistedSpotifyRedirectIndex.isPresent()) {
SpotifyRedirectIndex spotifyRedirectIndex = persistedSpotifyRedirectIndex.get();
YouTubeVideo video = youTubeService.getVideoForId(spotifyRedirectIndex.getYouTubeId());
if (video != null) {
try {
youTubeVideo.setId(video.getVideoId());
youTubeVideo.setDuration(video.getDuration());
} catch (UnavailableResourceException e) {
// never happens for YouTubeVideoImpl instances
throw new RuntimeException(e);
}
youTubeVideo.setTitle(spotifyTrack.getDisplay());
runUpdateTask(spotifyTrackId, (index, session) -> index.setLastUsed(LocalDate.now()));
return;
} else {
runUpdateTask(spotifyTrackId, (index, session) -> session.delete(index));
}
}
youTubeService.redirectSpotify(youTubeVideo);
if (!youTubeVideo.isCanceled() && !Strings.isNullOrEmpty(spotifyTrack.getId())) {
SINGE_THREAD_EXECUTOR_SERVICE.execute(() -> StaticSessionProvider.consumeSession(otherThreadSession -> {
try {
String videoId = youTubeVideo.getVideoId();
SpotifyRedirectIndex spotifyRedirectIndex = new SpotifyRedirectIndex(spotifyTrack.getId(), videoId, spotifyTrack.getKind(), otherThreadSession);
// check again if the index was not created by other thread
if (queryExistingIndex(otherThreadSession, spotifyTrackId).isEmpty()) {
invoker.invoke(() -> otherThreadSession.persist(spotifyRedirectIndex));
}
} catch (UnavailableResourceException e) {
logger.warn("Tried creating a SpotifyRedirectIndex for an unavailable Track");
} catch (Exception e) {
logger.error("Exception while creating SpotifyRedirectIndex", e);
}
}));
}
}
use of net.robinfriedli.aiode.exceptions.UnavailableResourceException in project aiode by robinfriedli.
the class AbstractPlayableLoadingCommand method loadYouTubeVideo.
private void loadYouTubeVideo(AudioManager audioManager) throws IOException {
YouTubeService youTubeService = audioManager.getYouTubeService();
if (argumentSet("select")) {
int limit = getArgumentValueWithTypeOrElse("select", Integer.class, 10);
List<YouTubeVideo> youTubeVideos = youTubeService.searchSeveralVideos(limit, getCommandInput());
if (youTubeVideos.size() == 1) {
Playable playable = youTubeVideos.get(0);
handleResults(Lists.newArrayList(playable));
loadedTrack = playable;
} else if (youTubeVideos.isEmpty()) {
throw new NoResultsFoundException(String.format("No YouTube video found for '%s'", getCommandInput()));
} else {
askQuestion(youTubeVideos, youTubeVideo -> {
try {
return youTubeVideo.getDisplay();
} catch (UnavailableResourceException e) {
// Unreachable since only HollowYouTubeVideos might get interrupted
throw new RuntimeException(e);
}
});
}
} else {
YouTubeVideo youTubeVideo = youTubeService.searchVideo(getCommandInput());
handleResults(Lists.newArrayList(youTubeVideo));
loadedTrack = youTubeVideo;
}
}
use of net.robinfriedli.aiode.exceptions.UnavailableResourceException in project aiode by robinfriedli.
the class SearchCommand method searchYouTubeVideo.
private void searchYouTubeVideo() throws UnavailableResourceException, IOException {
YouTubeService youTubeService = Aiode.get().getAudioManager().getYouTubeService();
if (argumentSet("select")) {
int limit = getArgumentValueWithTypeOrElse("select", Integer.class, 10);
List<YouTubeVideo> youTubeVideos = youTubeService.searchSeveralVideos(limit, getCommandInput());
if (youTubeVideos.size() == 1) {
listYouTubeVideo(youTubeVideos.get(0));
} else if (youTubeVideos.isEmpty()) {
throw new NoResultsFoundException(String.format("No YouTube videos found for '%s'", getCommandInput()));
} else {
askQuestion(youTubeVideos, youTubeVideo -> {
try {
return youTubeVideo.getDisplay();
} catch (UnavailableResourceException e) {
// Unreachable since only HollowYouTubeVideos might get cancelled
throw new RuntimeException(e);
}
});
}
} else {
listYouTubeVideo(youTubeService.searchVideo(getCommandInput()));
}
}
use of net.robinfriedli.aiode.exceptions.UnavailableResourceException in project aiode by robinfriedli.
the class QueueIterator method playNext.
void playNext() {
if (isReplaced) {
return;
}
// don't skip over more than 10 items to avoid a frozen queue
if (attemptCount.incrementAndGet() > 10) {
MessageChannel communicationChannel = playback.getCommunicationChannel();
if (communicationChannel != null) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setColor(Color.RED);
embedBuilder.setDescription("Queue contains too many unplayable tracks subsequently for automatic skipping. You can skip to the next valid track manually.");
messageService.sendTemporary(embedBuilder.build(), communicationChannel);
}
playback.stop();
// reset just in case, even though the same QueueIterator instance will currently never be used again as the
// user now either has to skip manually or start a new playback, creating a new iterator
resetAttemptCount();
return;
}
Playable track = queue.getCurrent();
AudioItem result = null;
AudioTrack cachedTracked = track.getCached();
if (cachedTracked != null) {
result = cachedTracked.makeClone();
}
if (result == null) {
String playbackUrl;
try {
playbackUrl = track.getPlaybackUrl();
} catch (UnavailableResourceException e) {
iterateQueue(playback, queue, true);
return;
}
try {
result = audioTrackLoader.loadByIdentifier(playbackUrl);
} catch (FriendlyException e) {
sendError(track, e);
iterateQueue(playback, queue, true);
return;
}
}
if (result != null) {
if (result instanceof AudioTrack) {
AudioTrack audioTrack = (AudioTrack) result;
track.setCached(audioTrack);
audioTrack.setUserData(track);
playback.getAudioPlayer().playTrack(audioTrack);
currentlyPlaying = track;
} else {
throw new UnsupportedOperationException("Expected an AudioTrack or Throwable but got " + result.getClass());
}
} else {
iterateQueue(playback, queue);
}
}
Aggregations