Search in sources :

Example 6 with Playlist

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

the class AbstractPlayableLoadingCommand method loadLocalList.

private void loadLocalList(AudioManager audioManager) throws Exception {
    Playlist playlist = SearchEngine.searchLocalList(getContext().getSession(), getCommandInput());
    if (playlist == null) {
        throw new NoResultsFoundException(String.format("No local playlist found for '%s'", getCommandInput()));
    }
    List<Object> items = runWithCredentials(() -> playlist.getTracks(getContext().getSpotifyApi()));
    if (items.isEmpty()) {
        throw new NoResultsFoundException("Playlist is empty");
    }
    PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), trackLoadingExecutor);
    List<Playable> playables = playableFactory.createPlayables(shouldRedirectSpotify(), items);
    handleResults(playables);
    loadedLocalList = playlist;
}
Also used : PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) Playlist(net.robinfriedli.aiode.entities.Playlist) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) Playable(net.robinfriedli.aiode.audio.Playable)

Example 7 with Playlist

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

the class CreateCommand method doRun.

@Override
public void doRun() {
    Session session = getContext().getSession();
    Playlist existingPlaylist = SearchEngine.searchLocalList(session, getCommandInput());
    if (existingPlaylist != null) {
        throw new InvalidCommandException("Playlist " + getCommandInput() + " already exists");
    }
    Playlist playlist = new Playlist(getCommandInput(), getContext().getUser(), getContext().getGuild());
    invoke(() -> session.persist(playlist));
}
Also used : Playlist(net.robinfriedli.aiode.entities.Playlist) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Session(org.hibernate.Session)

Example 8 with Playlist

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

the class EmptyCommand 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 aiode playlist found for '%s'", getCommandInput()));
    }
    if (playlist.isEmpty()) {
        throw new InvalidCommandException("Playlist is already empty");
    }
    invoke(() -> {
        for (PlaylistItem item : playlist.getItems()) {
            session.delete(item);
        }
    });
}
Also used : Playlist(net.robinfriedli.aiode.entities.Playlist) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) Session(org.hibernate.Session)

Example 9 with Playlist

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

the class RemoveCommand method doRun.

@Override
public void doRun() {
    Session session = getContext().getSession();
    String playlistName = getArgumentValue("from");
    Playlist playlist = SearchEngine.searchLocalList(session, playlistName);
    if (playlist == null) {
        throw new NoResultsFoundException(String.format("No local list found for '%s'", playlistName));
    } else if (playlist.isEmpty()) {
        throw new InvalidCommandException("Playlist is empty");
    }
    if (argumentSet("index")) {
        StringList indices = StringList.createWithRegex(getCommandInput(), "-");
        if (indices.size() == 1 || indices.size() == 2) {
            indices.applyForEach(String::trim);
            if (indices.size() == 1) {
                int index = parse(indices.get(0));
                checkIndex(index, playlist);
                invoke(() -> session.delete(playlist.getItemsSorted().get(index - 1)));
            } else {
                int start = parse(indices.get(0));
                int end = parse(indices.get(1));
                checkIndex(start, playlist);
                checkIndex(end, playlist);
                if (end <= start) {
                    throw new InvalidCommandException("End index needs to be greater than start.");
                }
                invoke(() -> playlist.getItemsSorted().subList(start - 1, end).forEach(session::delete));
            }
        } else {
            throw new InvalidCommandException("Expected one or two indices but found " + indices.size());
        }
    } else {
        List<PlaylistItem> playlistItems = SearchEngine.searchPlaylistItems(playlist, getCommandInput());
        if (playlistItems.size() == 1) {
            invoke(() -> session.delete(playlistItems.get(0)));
        } else if (playlistItems.isEmpty()) {
            throw new NoResultsFoundException(String.format("No tracks found for '%s' on list '%s'", getCommandInput(), playlistName));
        } else {
            askQuestion(playlistItems, PlaylistItem::display, item -> valueOf(item.getIndex() + 1));
        }
    }
}
Also used : PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) StringList(net.robinfriedli.stringlist.StringList) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) CommandManager(net.robinfriedli.aiode.command.CommandManager) Collection(java.util.Collection) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Session(org.hibernate.Session) List(java.util.List) Playlist(net.robinfriedli.aiode.entities.Playlist) AbstractCommand(net.robinfriedli.aiode.command.AbstractCommand) CommandContext(net.robinfriedli.aiode.command.CommandContext) SearchEngine(net.robinfriedli.aiode.util.SearchEngine) String(java.lang.String) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) Playlist(net.robinfriedli.aiode.entities.Playlist) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) StringList(net.robinfriedli.stringlist.StringList) String(java.lang.String) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) Session(org.hibernate.Session)

Example 10 with Playlist

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

the class MigratePlaylistsTask method migrateFile.

private void migrateFile(Session session, Context context, Guild guild, SpotifyApi spotifyApi) throws Exception {
    HibernatePlaylistMigrator hibernatePlaylistMigrator = new HibernatePlaylistMigrator(context, guild, spotifyApi, session);
    Map<Playlist, List<PlaylistItem>> playlistMap = hibernatePlaylistMigrator.perform();
    for (Playlist playlist : playlistMap.keySet()) {
        playlistMap.get(playlist).forEach(item -> {
            item.add();
            session.persist(item);
        });
        session.persist(playlist);
    }
    File directory = new File("./resources/archive");
    if (!directory.exists()) {
        directory.mkdir();
    }
    File file = context.getFile();
    Objects.requireNonNull(file);
    Files.move(file, new File(directory.getPath() + "/" + file.getName()));
}
Also used : Playlist(net.robinfriedli.aiode.entities.Playlist) HibernatePlaylistMigrator(net.robinfriedli.aiode.persist.tasks.HibernatePlaylistMigrator) List(java.util.List) File(java.io.File)

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