Search in sources :

Example 1 with PlaylistItem

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

the class HibernatePlaylistMigrator method loadSpotifyItem.

private void loadSpotifyItem(XmlElement item, int index, SpotifyTrackBulkLoadingService spotifyBulkLoadingService, ShardManager shardManager, Playlist newPlaylist, Map<PlaylistItem, Integer> itemsWithIndex, SpotifyTrackKind kind) {
    String id = item.getAttribute("id").getValue();
    spotifyBulkLoadingService.add(createItem(id, kind), spotifyTrack -> {
        String addedUser = item.getAttribute("addedUser").getValue();
        String addedUserId = item.getAttribute("addedUserId").getValue();
        PlaylistItem playlistItem = spotifyTrack.exhaustiveMatch(track -> new Song(track, addedUser, addedUserId, newPlaylist, session), episode -> new Episode(episode, addedUser, addedUserId, newPlaylist));
        playlistItem.setAddedUser(item.getAttribute("addedUser").getValue());
        playlistItem.setAddedUserId(item.getAttribute("addedUserId").getValue());
        itemsWithIndex.put(playlistItem, index);
    });
}
Also used : Episode(net.robinfriedli.aiode.entities.Episode) Song(net.robinfriedli.aiode.entities.Song) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem)

Example 2 with PlaylistItem

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

the class HibernatePlaylistMigrator method perform.

@Override
public Map<Playlist, List<PlaylistItem>> perform() throws Exception {
    ShardManager shardManager = Aiode.get().getShardManager();
    SpotifyTrackBulkLoadingService spotifyBulkLoadingService = new SpotifyTrackBulkLoadingService(spotifyApi);
    List<XmlElement> playlists = context.query(tagName("playlist")).collect();
    Map<Playlist, List<PlaylistItem>> playlistMap = new HashMap<>();
    for (XmlElement playlist : playlists) {
        Playlist newPlaylist = new Playlist();
        newPlaylist.setName(playlist.getAttribute("name").getValue());
        newPlaylist.setCreatedUser(playlist.getAttribute("createdUser").getValue());
        newPlaylist.setCreatedUserId(playlist.getAttribute("createdUserId").getValue());
        newPlaylist.setGuild(guild.getName());
        newPlaylist.setGuildId(guild.getId());
        List<XmlElement> items = playlist.getSubElements();
        Map<PlaylistItem, Integer> itemsWithIndex = new HashMap<>();
        for (int i = 0; i < items.size(); i++) {
            XmlElement item = items.get(i);
            switch(item.getTagName()) {
                case "song":
                    loadSpotifyItem(item, i, spotifyBulkLoadingService, shardManager, newPlaylist, itemsWithIndex, TRACK);
                    break;
                case "episode":
                    loadSpotifyItem(item, i, spotifyBulkLoadingService, shardManager, newPlaylist, itemsWithIndex, EPISODE);
                    break;
                case "video":
                    Video video = new Video();
                    video.setId(item.getAttribute("id").getValue());
                    video.setTitle(item.getAttribute("title").getValue());
                    if (item.hasAttribute("redirectedSpotifyId")) {
                        video.setRedirectedSpotifyId(item.getAttribute("redirectedSpotifyId").getValue());
                    }
                    if (item.hasAttribute("spotifyTrackName")) {
                        video.setSpotifyTrackName(item.getAttribute("spotifyTrackName").getValue());
                    }
                    video.setPlaylist(newPlaylist);
                    video.setDuration(item.getAttribute("duration").getLong());
                    video.setAddedUser(item.getAttribute("addedUser").getValue());
                    video.setAddedUserId(item.getAttribute("addedUserId").getValue());
                    newPlaylist.getVideos().add(video);
                    itemsWithIndex.put(video, i);
                    break;
                case "urlTrack":
                    UrlTrack urlTrack = new UrlTrack();
                    urlTrack.setUrl(item.getAttribute("url").getValue());
                    urlTrack.setTitle(item.getAttribute("title").getValue());
                    urlTrack.setDuration(item.getAttribute("duration").getLong());
                    urlTrack.setAddedUser(item.getAttribute("addedUser").getValue());
                    urlTrack.setAddedUserId(item.getAttribute("addedUserId").getValue());
                    urlTrack.setPlaylist(newPlaylist);
                    newPlaylist.getUrlTracks().add(urlTrack);
                    itemsWithIndex.put(urlTrack, i);
                    break;
            }
        }
        SpotifyInvoker.create(spotifyApi).invoke(() -> {
            spotifyBulkLoadingService.perform();
            return null;
        });
        List<PlaylistItem> playlistItems = itemsWithIndex.keySet().stream().sorted(Comparator.comparing(itemsWithIndex::get)).collect(Collectors.toList());
        playlistMap.put(newPlaylist, playlistItems);
    }
    return playlistMap;
}
Also used : HashMap(java.util.HashMap) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) Playlist(net.robinfriedli.aiode.entities.Playlist) SpotifyTrackBulkLoadingService(net.robinfriedli.aiode.audio.spotify.SpotifyTrackBulkLoadingService) Video(net.robinfriedli.aiode.entities.Video) UrlTrack(net.robinfriedli.aiode.entities.UrlTrack) XmlElement(net.robinfriedli.jxp.api.XmlElement) List(java.util.List) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem)

Example 3 with PlaylistItem

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

the class VerifyPlaylistInterceptor method onSaveChained.

@Override
public void onSaveChained(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
    if (entity instanceof PlaylistItem) {
        PlaylistItem playlistItem = (PlaylistItem) entity;
        playlistItem.setOrdinal(ordinal);
        ++ordinal;
    }
}
Also used : PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem)

Example 4 with PlaylistItem

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

the class VerifyPlaylistInterceptor method preFlush.

@Override
public void preFlush(Iterator entities) {
    @SuppressWarnings("unchecked") Iterable<Object> iterable = () -> entities;
    Set<Playlist> playlistsToUpdate = StreamSupport.stream(iterable.spliterator(), false).filter(entity -> entity instanceof PlaylistItem).map(entity -> ((PlaylistItem) entity).getPlaylist()).collect(Collectors.toSet());
    if (!playlistsToUpdate.isEmpty()) {
        playlistsToUpdate.forEach(this::checkPlaylistSize);
        UpdatePlaylistItemIndicesTask task = new UpdatePlaylistItemIndicesTask(playlistsToUpdate, Comparator.comparing(PlaylistItem::getOrdinal));
        task.perform();
    }
    super.preFlush(entities);
}
Also used : PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) UpdatePlaylistItemIndicesTask(net.robinfriedli.aiode.persist.tasks.UpdatePlaylistItemIndicesTask) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Song(net.robinfriedli.aiode.entities.Song) Set(java.util.Set) Interceptor(org.hibernate.Interceptor) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Transaction(org.hibernate.Transaction) Playlist(net.robinfriedli.aiode.entities.Playlist) SpringPropertiesConfig(net.robinfriedli.aiode.boot.SpringPropertiesConfig) StreamSupport(java.util.stream.StreamSupport) Video(net.robinfriedli.aiode.entities.Video) Comparator(java.util.Comparator) UrlTrack(net.robinfriedli.aiode.entities.UrlTrack) Type(org.hibernate.type.Type) Playlist(net.robinfriedli.aiode.entities.Playlist) UpdatePlaylistItemIndicesTask(net.robinfriedli.aiode.persist.tasks.UpdatePlaylistItemIndicesTask) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem)

Example 5 with PlaylistItem

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

the class PlaylistViewHandler method getList.

private String getList(Playlist playlist) {
    StringBuilder listBuilder = new StringBuilder();
    List<PlaylistItem> playlistItems = playlist.getItemsSorted();
    for (int i = 0; i < playlistItems.size(); i++) {
        PlaylistItem item = playlistItems.get(i);
        listBuilder.append("<tr>").append(System.lineSeparator()).append("<td>").append(i + 1).append("</td>").append(System.lineSeparator()).append("<td>").append(item.display()).append("</td>").append(System.lineSeparator()).append("<td>").append(Util.normalizeMillis(item.getDuration())).append("</td>").append(System.lineSeparator()).append("</tr>").append(System.lineSeparator());
    }
    return listBuilder.toString();
}
Also used : 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