use of net.robinfriedli.aiode.entities.UrlTrack 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;
}
use of net.robinfriedli.aiode.entities.UrlTrack in project aiode by robinfriedli.
the class PlayableFactory method createPlayables.
/**
* Creates Playables for a Collection of Objects; YouTube videos or Spotify Tracks.
*
* @param redirectSpotify if true the matching YouTube video is loaded to play the full track using
* {@link YouTubeService#redirectSpotify(HollowYouTubeVideo)}, else a {@link PlayableTrackWrapper} is created to play the
* preview mp3 provided by Spotify
* @param items the objects to create a Playable for
* @return the created Playables
*/
public List<Playable> createPlayables(boolean redirectSpotify, Collection<?> items) {
List<Playable> playables = Lists.newArrayList();
List<HollowYouTubeVideo> tracksToRedirect = Lists.newArrayList();
List<YouTubePlaylist> youTubePlaylistsToLoad = Lists.newArrayList();
try {
for (Object item : items) {
if (item instanceof Playable) {
playables.add((Playable) item);
} else if (item instanceof Track) {
handleTrack(SpotifyTrack.wrap((Track) item), redirectSpotify, tracksToRedirect, playables);
} else if (item instanceof Episode) {
handleTrack(SpotifyTrack.wrap((Episode) item), redirectSpotify, tracksToRedirect, playables);
} else if (item instanceof SpotifyTrack) {
handleTrack((SpotifyTrack) item, redirectSpotify, tracksToRedirect, playables);
} else if (item instanceof UrlTrack) {
playables.add(((UrlTrack) item).asPlayable());
} else if (item instanceof YouTubePlaylist) {
YouTubePlaylist youTubePlaylist = ((YouTubePlaylist) item);
playables.addAll(youTubePlaylist.getVideos());
youTubePlaylistsToLoad.add(youTubePlaylist);
} else if (item instanceof PlaylistSimplified) {
List<SpotifyTrack> t = SpotifyInvoker.create(spotifyService.getSpotifyApi()).invoke(() -> spotifyService.getPlaylistTracks((PlaylistSimplified) item));
for (SpotifyTrack track : t) {
handleTrack(track, redirectSpotify, tracksToRedirect, playables);
}
} else if (item instanceof AlbumSimplified) {
List<Track> t = invoker.invoke(() -> spotifyService.getAlbumTracks((AlbumSimplified) item));
for (Track track : t) {
handleTrack(SpotifyTrack.wrapIfNotNull(track), redirectSpotify, tracksToRedirect, playables);
}
} else if (item instanceof AudioTrack) {
playables.add(new UrlPlayable((AudioTrack) item));
} else if (item instanceof AudioPlaylist) {
List<Playable> convertedPlayables = ((AudioPlaylist) item).getTracks().stream().map(UrlPlayable::new).collect(Collectors.toList());
playables.addAll(convertedPlayables);
} else if (item != null) {
throw new UnsupportedOperationException("Unsupported playable " + item.getClass());
}
}
} catch (Exception e) {
throw new RuntimeException("Exception while creating Playables", e);
}
if (!tracksToRedirect.isEmpty() || !youTubePlaylistsToLoad.isEmpty()) {
trackLoadingExecutor.execute(new SpotifyTrackRedirectionRunnable(tracksToRedirect, youTubeService).andThen(new YouTubePlaylistPopulationRunnable(youTubePlaylistsToLoad, youTubeService)));
}
return playables;
}
use of net.robinfriedli.aiode.entities.UrlTrack in project aiode by robinfriedli.
the class VerifyPlaylistInterceptor method onDeleteChained.
@Override
public void onDeleteChained(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
if (entity instanceof PlaylistItem) {
PlaylistItem playlistItem = (PlaylistItem) entity;
Playlist playlist = playlistItem.getPlaylist();
if (playlistItem instanceof Song) {
playlist.getSongs().remove(playlistItem);
} else if (playlistItem instanceof Video) {
playlist.getVideos().remove(playlistItem);
} else if (playlistItem instanceof UrlTrack) {
playlist.getUrlTracks().remove(playlistItem);
}
}
}
Aggregations