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());
}
}
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);
});
}
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);
}
});
}
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);
}
}
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;
}
Aggregations