Search in sources :

Example 1 with YouTubeService

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);
}
Also used : SpotifyItemKind(net.robinfriedli.aiode.entities.SpotifyItemKind) SpotifyService(net.robinfriedli.aiode.audio.spotify.SpotifyService) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) PlayableTrackWrapper(net.robinfriedli.aiode.audio.spotify.PlayableTrackWrapper) Playable(net.robinfriedli.aiode.audio.Playable) SpotifyTrackKind(net.robinfriedli.aiode.audio.spotify.SpotifyTrackKind) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) PlaybackHistorySource(net.robinfriedli.aiode.entities.PlaybackHistorySource)

Example 2 with YouTubeService

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()));
    }
}
Also used : NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService)

Example 3 with YouTubeService

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;
    }
}
Also used : StringList(net.robinfriedli.stringlist.StringList) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Playable(net.robinfriedli.aiode.audio.Playable) Episode(se.michaelthelin.spotify.model_objects.specification.Episode) Callable(java.util.concurrent.Callable) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) Lists(com.google.common.collect.Lists) Playlist(net.robinfriedli.aiode.entities.Playlist) YouTubeVideo(net.robinfriedli.aiode.audio.youtube.YouTubeVideo) SearchEngine(net.robinfriedli.aiode.util.SearchEngine) Track(se.michaelthelin.spotify.model_objects.specification.Track) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) SpotifyUri(net.robinfriedli.aiode.audio.spotify.SpotifyUri) UnavailableResourceException(net.robinfriedli.aiode.exceptions.UnavailableResourceException) ArtistSimplified(se.michaelthelin.spotify.model_objects.specification.ArtistSimplified) CommandManager(net.robinfriedli.aiode.command.CommandManager) AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) IOException(java.io.IOException) AudioManager(net.robinfriedli.aiode.audio.AudioManager) NoSpotifyResultsFoundException(net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException) AudioItem(com.sedmelluq.discord.lavaplayer.track.AudioItem) UrlValidator(org.apache.commons.validator.routines.UrlValidator) Aiode(net.robinfriedli.aiode.Aiode) ShowSimplified(se.michaelthelin.spotify.model_objects.specification.ShowSimplified) List(java.util.List) AudioTrackLoader(net.robinfriedli.aiode.audio.AudioTrackLoader) SpotifyTrackResultHandler(net.robinfriedli.aiode.audio.spotify.SpotifyTrackResultHandler) SpotifyService(net.robinfriedli.aiode.audio.spotify.SpotifyService) CommandContext(net.robinfriedli.aiode.command.CommandContext) PlaylistSimplified(se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified) AlbumSimplified(se.michaelthelin.spotify.model_objects.specification.AlbumSimplified) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) TrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.TrackLoadingExecutor) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) Playable(net.robinfriedli.aiode.audio.Playable) UnavailableResourceException(net.robinfriedli.aiode.exceptions.UnavailableResourceException) YouTubeVideo(net.robinfriedli.aiode.audio.youtube.YouTubeVideo) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService)

Example 4 with YouTubeService

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;
    }
}
Also used : PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) Playable(net.robinfriedli.aiode.audio.Playable) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService)

Example 5 with YouTubeService

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);
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService)

Aggregations

YouTubeService (net.robinfriedli.aiode.audio.youtube.YouTubeService)8 YouTubePlaylist (net.robinfriedli.aiode.audio.youtube.YouTubePlaylist)4 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)4 Aiode (net.robinfriedli.aiode.Aiode)3 Playable (net.robinfriedli.aiode.audio.Playable)3 SpotifyTrack (net.robinfriedli.aiode.audio.spotify.SpotifyTrack)3 IOException (java.io.IOException)2 List (java.util.List)2 Callable (java.util.concurrent.Callable)2 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)2 PlayableFactory (net.robinfriedli.aiode.audio.PlayableFactory)2 UrlPlayable (net.robinfriedli.aiode.audio.UrlPlayable)2 YouTubeVideo (net.robinfriedli.aiode.audio.youtube.YouTubeVideo)2 CommandContext (net.robinfriedli.aiode.command.CommandContext)2 CommandManager (net.robinfriedli.aiode.command.CommandManager)2 Playlist (net.robinfriedli.aiode.entities.Playlist)2 CommandContribution (net.robinfriedli.aiode.entities.xml.CommandContribution)2 NoSpotifyResultsFoundException (net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException)2 UnavailableResourceException (net.robinfriedli.aiode.exceptions.UnavailableResourceException)2 SearchEngine (net.robinfriedli.aiode.util.SearchEngine)2