Search in sources :

Example 16 with Playlist

use of net.robinfriedli.aiode.entities.Playlist in project aiode by robinfriedli.

the class SearchCommand method listSpotifyList.

private void listSpotifyList() throws Exception {
    String commandBody = getCommandInput();
    if (commandBody.isBlank()) {
        throw new InvalidCommandException("Command may not be empty when searching spotify lists");
    }
    List<PlaylistSimplified> playlists;
    int limit = getArgumentValueWithTypeOrElse("select", Integer.class, 20);
    if (argumentSet("own")) {
        playlists = runWithLogin(() -> getSpotifyService().searchOwnPlaylist(getCommandInput(), limit));
    } else {
        playlists = runWithCredentials(() -> getSpotifyService().searchPlaylist(getCommandInput(), limit));
    }
    if (playlists.size() == 1) {
        PlaylistSimplified playlist = playlists.get(0);
        List<SpotifyTrack> tracks = runWithCredentials(() -> getSpotifyService().getPlaylistTracks(playlist));
        listTracks(tracks, playlist.getName(), playlist.getOwner().getDisplayName(), null, "playlist/" + playlist.getId());
    } else if (playlists.isEmpty()) {
        throw new NoSpotifyResultsFoundException(String.format("No Spotify playlist found for '%s'", getCommandInput()));
    } else {
        askQuestion(playlists, PlaylistSimplified::getName, p -> p.getOwner().getDisplayName());
    }
}
Also used : PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) StringList(net.robinfriedli.stringlist.StringList) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Session(org.hibernate.Session) Episode(se.michaelthelin.spotify.model_objects.specification.Episode) Callable(java.util.concurrent.Callable) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) User(net.dv8tion.jda.api.entities.User) Util(net.robinfriedli.aiode.util.Util) Playlist(net.robinfriedli.aiode.entities.Playlist) AbstractSourceDecidingCommand(net.robinfriedli.aiode.command.commands.AbstractSourceDecidingCommand) 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) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) UnavailableResourceException(net.robinfriedli.aiode.exceptions.UnavailableResourceException) ArtistSimplified(se.michaelthelin.spotify.model_objects.specification.ArtistSimplified) CommandManager(net.robinfriedli.aiode.command.CommandManager) Collection(java.util.Collection) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) IOException(java.io.IOException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Collectors(java.util.stream.Collectors) NoSpotifyResultsFoundException(net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) Aiode(net.robinfriedli.aiode.Aiode) ShowSimplified(se.michaelthelin.spotify.model_objects.specification.ShowSimplified) URLEncoder(java.net.URLEncoder) List(java.util.List) SpringPropertiesConfig(net.robinfriedli.aiode.boot.SpringPropertiesConfig) CommandContext(net.robinfriedli.aiode.command.CommandContext) PlaylistSimplified(se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified) EmbedTable(net.robinfriedli.aiode.util.EmbedTable) AlbumSimplified(se.michaelthelin.spotify.model_objects.specification.AlbumSimplified) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) NoSpotifyResultsFoundException(net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException) PlaylistSimplified(se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified)

Example 17 with Playlist

use of net.robinfriedli.aiode.entities.Playlist in project aiode by robinfriedli.

the class DeleteCommand method doRun.

@Override
public void doRun() {
    Session session = getContext().getSession();
    Playlist playlist = SearchEngine.searchLocalList(session, getCommandInput());
    if (playlist == null) {
        throw new NoResultsFoundException(String.format("No local list found for '%s'", getCommandInput()));
    }
    invoke(() -> {
        playlist.getItems().forEach(session::delete);
        session.delete(playlist);
    });
}
Also used : Playlist(net.robinfriedli.aiode.entities.Playlist) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) Session(org.hibernate.Session)

Example 18 with Playlist

use of net.robinfriedli.aiode.entities.Playlist in project aiode by robinfriedli.

the class ExportCommand method doRun.

@Override
public void doRun() {
    Session session = getContext().getSession();
    Guild guild = getContext().getGuild();
    AudioQueue queue = Aiode.get().getAudioManager().getQueue(guild);
    if (queue.isEmpty()) {
        throw new InvalidCommandException("Queue is empty");
    }
    Playlist existingPlaylist = SearchEngine.searchLocalList(session, getCommandInput());
    if (existingPlaylist != null) {
        throw new InvalidCommandException("Playlist " + getCommandInput() + " already exists");
    }
    List<Playable> tracks = queue.getTracks();
    User createUser = getContext().getUser();
    Playlist playlist = new Playlist(getCommandInput(), createUser, guild);
    invoke(() -> {
        session.persist(playlist);
        for (Playable track : tracks) {
            if (track instanceof HollowYouTubeVideo) {
                HollowYouTubeVideo video = (HollowYouTubeVideo) track;
                video.awaitCompletion();
                if (video.isCanceled()) {
                    continue;
                }
            }
            PlaylistItem export = track.export(playlist, getContext().getUser(), session);
            export.add();
            session.persist(export);
        }
    });
}
Also used : Playlist(net.robinfriedli.aiode.entities.Playlist) User(net.dv8tion.jda.api.entities.User) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Playable(net.robinfriedli.aiode.audio.Playable) HollowYouTubeVideo(net.robinfriedli.aiode.audio.youtube.HollowYouTubeVideo) Guild(net.dv8tion.jda.api.entities.Guild) AudioQueue(net.robinfriedli.aiode.audio.AudioQueue) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) Session(org.hibernate.Session)

Example 19 with Playlist

use of net.robinfriedli.aiode.entities.Playlist in project aiode by robinfriedli.

the class MoveCommand method doRun.

@Override
public void doRun() {
    Guild guild = getContext().getGuild();
    Session session = getContext().getSession();
    String playlistName = getArgumentValue("on");
    Playlist playlist = SearchEngine.searchLocalList(session, playlistName);
    if (playlist == null) {
        throw new NoResultsFoundException(String.format("No local playlist found for '%s'", playlistName));
    } else if (playlist.isEmpty()) {
        throw new InvalidCommandException("Playlist is empty");
    }
    String sourceIndex = getCommandInput();
    int targetIndex = getArgumentValueWithType("to", Integer.class);
    checkIndex(targetIndex, playlist);
    if (sourceIndex.contains("-")) {
        List<String> indices = Splitter.on("-").trimResults().omitEmptyStrings().splitToList(sourceIndex);
        if (indices.size() != 2) {
            throw new InvalidCommandException("Expected exactly two source indices but found " + indices.size());
        }
        int start = parse(indices.get(0));
        int end = parse(indices.get(1));
        checkIndex(start, playlist);
        checkIndex(end, playlist);
        if (start >= end) {
            throw new InvalidCommandException("End index needs to be greater than start");
        } else if (start == targetIndex) {
            throw new InvalidCommandException("Redundant move command. New and old index are the same.");
        } else if (targetIndex <= end && targetIndex >= start) {
            throw new InvalidCommandException("Target index is within to move range. Cannot move items.");
        }
        moveIndexRange(start - 1, end - 1, targetIndex - 1, playlist);
    } else {
        int index = parse(sourceIndex);
        checkIndex(index, playlist);
        if (index == targetIndex) {
            throw new InvalidCommandException("Redundant move command. New and old index are the same.");
        }
        moveSingleIndex(index - 1, targetIndex - 1, playlist);
    }
}
Also used : Playlist(net.robinfriedli.aiode.entities.Playlist) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Guild(net.dv8tion.jda.api.entities.Guild) Session(org.hibernate.Session)

Example 20 with Playlist

use of net.robinfriedli.aiode.entities.Playlist in project aiode by robinfriedli.

the class UpdatePlaylistItemIndicesTask method perform.

@Override
public Void perform() {
    for (Playlist playlist : playlistsToUpdate) {
        List<PlaylistItem> items = playlist.getItems();
        List<PlaylistItem> itemsOrdered = items.stream().filter(item -> item.getIndex() != null).sorted(Comparator.comparing(PlaylistItem::getIndex)).collect(Collectors.toList());
        List<PlaylistItem> addedItems = items.stream().filter(item -> item.getIndex() == null).sorted(sorter).collect(Collectors.toList());
        itemsOrdered.addAll(addedItems);
        for (int i = 0; i < itemsOrdered.size(); i++) {
            PlaylistItem item = itemsOrdered.get(i);
            if (item.getIndex() == null || item.getIndex() != i) {
                item.setIndex(i);
            }
        }
    }
    return null;
}
Also used : Playlist(net.robinfriedli.aiode.entities.Playlist) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem)

Aggregations

Playlist (net.robinfriedli.aiode.entities.Playlist)24 Session (org.hibernate.Session)15 PlaylistItem (net.robinfriedli.aiode.entities.PlaylistItem)12 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)10 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)10 List (java.util.List)8 Playable (net.robinfriedli.aiode.audio.Playable)5 User (net.dv8tion.jda.api.entities.User)4 Aiode (net.robinfriedli.aiode.Aiode)4 UrlTrack (net.robinfriedli.aiode.entities.UrlTrack)4 IOException (java.io.IOException)3 Collectors (java.util.stream.Collectors)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 AudioQueue (net.robinfriedli.aiode.audio.AudioQueue)3 PlayableFactory (net.robinfriedli.aiode.audio.PlayableFactory)3 CommandContext (net.robinfriedli.aiode.command.CommandContext)3 CommandManager (net.robinfriedli.aiode.command.CommandManager)3 Song (net.robinfriedli.aiode.entities.Song)3 CommandContribution (net.robinfriedli.aiode.entities.xml.CommandContribution)3 SearchEngine (net.robinfriedli.aiode.util.SearchEngine)3