use of net.robinfriedli.aiode.audio.youtube.YouTubeService in project aiode by robinfriedli.
the class ChartsCommand method getTrackForRecord.
private Playable getTrackForRecord(Object[] record, Session session) throws Exception {
long sourceEntityPk = (Long) record[0];
PlaybackHistorySource sourceEntity = session.load(PlaybackHistorySource.class, sourceEntityPk);
Playable.Source source = sourceEntity.asEnum();
String id = (String) record[1];
Long spotifyItemKindPk = (Long) record[3];
SpotifyItemKind spotifyItemKind = spotifyItemKindPk != null ? session.load(SpotifyItemKind.class, spotifyItemKindPk) : null;
switch(source) {
case SPOTIFY:
return runWithCredentials(() -> {
if (spotifyItemKind == null) {
throw new IllegalStateException("spotifyItemKind cannot be null for PlaybackHistory entries of source SPOTIFY");
}
SpotifyTrackKind kind = spotifyItemKind.asEnum();
SpotifyService spotifyService = getSpotifyService();
SpotifyTrack track = kind.loadSingleItem(spotifyService, id);
if (track == null) {
return null;
}
return new PlayableTrackWrapper(track);
});
case YOUTUBE:
YouTubeService youTubeService = Aiode.get().getAudioManager().getYouTubeService();
try {
return youTubeService.getVideoForId(id);
} catch (FriendlyException e) {
return null;
}
case URL:
return playableFactory.createPlayable(id, getContext().getSpotifyApi(), false);
}
throw new UnsupportedOperationException("Unsupported source " + sourceEntity);
}
use of net.robinfriedli.aiode.audio.youtube.YouTubeService in project aiode by robinfriedli.
the class SearchCommand method listYouTubePlaylists.
private void listYouTubePlaylists() throws IOException {
YouTubeService youTubeService = Aiode.get().getAudioManager().getYouTubeService();
if (argumentSet("select")) {
int limit = getArgumentValueWithTypeOrElse("select", Integer.class, 10);
List<YouTubePlaylist> playlists = youTubeService.searchSeveralPlaylists(limit, getCommandInput());
if (playlists.size() == 1) {
listYouTubePlaylist(playlists.get(0));
} else if (playlists.isEmpty()) {
throw new NoResultsFoundException(String.format("No YouTube playlist found for '%s'", getCommandInput()));
} else {
askQuestion(playlists, YouTubePlaylist::getTitle, YouTubePlaylist::getChannelTitle);
}
} else {
listYouTubePlaylist(youTubeService.searchPlaylist(getCommandInput()));
}
}
use of net.robinfriedli.aiode.audio.youtube.YouTubeService 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.audio.youtube.YouTubeService in project aiode by robinfriedli.
the class AbstractPlayableLoadingCommand method loadYouTubeList.
private void loadYouTubeList(AudioManager audioManager) throws IOException {
YouTubeService youTubeService = audioManager.getYouTubeService();
PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), trackLoadingExecutor);
if (argumentSet("select")) {
int limit = getArgumentValueWithTypeOrElse("select", Integer.class, 10);
List<YouTubePlaylist> playlists = youTubeService.searchSeveralPlaylists(limit, getCommandInput());
if (playlists.size() == 1) {
YouTubePlaylist playlist = playlists.get(0);
List<Playable> playables = playableFactory.createPlayables(playlist);
handleResults(playables);
loadedYouTubePlaylist = playlist;
} else if (playlists.isEmpty()) {
throw new NoResultsFoundException(String.format("No YouTube playlist found for '%s'", getCommandInput()));
} else {
askQuestion(playlists, YouTubePlaylist::getTitle, YouTubePlaylist::getChannelTitle);
}
} else {
YouTubePlaylist youTubePlaylist = youTubeService.searchPlaylist(getCommandInput());
List<Playable> playables = playableFactory.createPlayables(youTubePlaylist);
handleResults(playables);
loadedYouTubePlaylist = youTubePlaylist;
}
}
use of net.robinfriedli.aiode.audio.youtube.YouTubeService in project aiode by robinfriedli.
the class YouTubeQuotaCommand method runAdmin.
@Override
public void runAdmin() {
YouTubeService youTubeService = Aiode.get().getAudioManager().getYouTubeService();
int atomic = youTubeService.getAtomicQuotaUsage();
int persistent = YouTubeService.getCurrentQuotaUsage(getContext().getSession(), LockModeType.NONE).getQuota();
int limit = Aiode.get().getSpringPropertiesConfig().requireApplicationProperty(Integer.class, "aiode.preferences.youtube_api_daily_quota");
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle("YouTube API quota usage");
embedBuilder.setDescription("Displays the current approximate usage of the daily YouTube API quota");
embedBuilder.addField("Current atomic value", String.valueOf(atomic), true);
embedBuilder.addField("Current persistent value", String.valueOf(persistent), true);
embedBuilder.addField("Daily limit", String.valueOf(limit), true);
sendMessage(embedBuilder);
}
Aggregations