Search in sources :

Example 11 with PlaylistItem

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

the class AddCommand method addPlayables.

private void addPlayables(Playlist playlist, List<Playable> playables) {
    if ((getTask() != null && getTask().isTerminated()) || Thread.currentThread().isInterrupted()) {
        return;
    }
    Session session = getContext().getSession();
    List<PlaylistItem> items = Lists.newArrayList();
    invoke(() -> {
        playables.forEach(playable -> {
            if (playable instanceof HollowYouTubeVideo && ((HollowYouTubeVideo) playable).isCanceled()) {
                return;
            }
            items.add(playable.export(playlist, getContext().getUser(), session));
        });
        addToList(playlist, items);
    });
}
Also used : HollowYouTubeVideo(net.robinfriedli.aiode.audio.youtube.HollowYouTubeVideo) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) Session(org.hibernate.Session)

Example 12 with PlaylistItem

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

the class AddCommand method addToList.

protected void addToList(Playlist playlist, List<PlaylistItem> items) {
    if (items.isEmpty()) {
        throw new NoResultsFoundException("Result is empty!");
    }
    Session session = getContext().getSession();
    invoke(() -> items.forEach(item -> {
        item.add();
        session.persist(item);
    }));
}
Also used : PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) CommandManager(net.robinfriedli.aiode.command.CommandManager) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) AudioQueue(net.robinfriedli.aiode.audio.AudioQueue) Playable(net.robinfriedli.aiode.audio.Playable) Session(org.hibernate.Session) AbstractPlayableLoadingCommand(net.robinfriedli.aiode.command.commands.AbstractPlayableLoadingCommand) Aiode(net.robinfriedli.aiode.Aiode) List(java.util.List) Lists(com.google.common.collect.Lists) Playlist(net.robinfriedli.aiode.entities.Playlist) HollowYouTubeVideo(net.robinfriedli.aiode.audio.youtube.HollowYouTubeVideo) CommandContext(net.robinfriedli.aiode.command.CommandContext) BlockingTrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.BlockingTrackLoadingExecutor) SearchEngine(net.robinfriedli.aiode.util.SearchEngine) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) Session(org.hibernate.Session)

Example 13 with PlaylistItem

use of net.robinfriedli.aiode.entities.PlaylistItem 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 14 with PlaylistItem

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

the class InsertCommand method addToList.

@Override
protected void addToList(Playlist playlist, List<PlaylistItem> items) {
    if (!(targetIndex > 0 && targetIndex <= playlist.getSize())) {
        throw new InvalidCommandException(String.format("Invalid index: %d. Index is not within playlist of size %d. Use the add command to add items at the end of the list instead.", targetIndex, playlist.getSize()));
    }
    int actualIndex = targetIndex - 1;
    // move back all items that are at or after the target index
    List<PlaylistItem> itemsSorted = playlist.getItemsSorted(true);
    for (int i = actualIndex; i < itemsSorted.size(); i++) {
        PlaylistItem item = itemsSorted.get(i);
        item.setIndex(item.getIndex() + items.size());
    }
    // assign target indices
    for (int i = 0; i < items.size(); i++) {
        PlaylistItem item = items.get(i);
        item.setIndex(actualIndex + i);
    }
    super.addToList(playlist, items);
}
Also used : InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem)

Example 15 with PlaylistItem

use of net.robinfriedli.aiode.entities.PlaylistItem 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

PlaylistItem (net.robinfriedli.aiode.entities.PlaylistItem)18 Playlist (net.robinfriedli.aiode.entities.Playlist)10 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)6 Session (org.hibernate.Session)6 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)4 List (java.util.List)3 HollowYouTubeVideo (net.robinfriedli.aiode.audio.youtube.HollowYouTubeVideo)3 Song (net.robinfriedli.aiode.entities.Song)3 UrlTrack (net.robinfriedli.aiode.entities.UrlTrack)3 Video (net.robinfriedli.aiode.entities.Video)3 User (net.dv8tion.jda.api.entities.User)2 ShardManager (net.dv8tion.jda.api.sharding.ShardManager)2 AudioQueue (net.robinfriedli.aiode.audio.AudioQueue)2 Playable (net.robinfriedli.aiode.audio.Playable)2 SpringPropertiesConfig (net.robinfriedli.aiode.boot.SpringPropertiesConfig)2 CommandContext (net.robinfriedli.aiode.command.CommandContext)2 CommandManager (net.robinfriedli.aiode.command.CommandManager)2 CommandContribution (net.robinfriedli.aiode.entities.xml.CommandContribution)2 SearchEngine (net.robinfriedli.aiode.util.SearchEngine)2 Lists (com.google.common.collect.Lists)1