private async Task CreatePlaylists(IEnumerable <IEnumerable <Track> > playlists, CreatePlaylistsCommand command) { var user = await spotify.UserProfile.Current(); var reversedPlaylists = playlists.Reverse(); foreach (var(playlist, index) in reversedPlaylists.Select((playlist, index) => (playlist, index))) { var playlistNumber = playlists.Count() - index; var playlistName = GeneratePlaylistName(command, playlistNumber); var playlistCreateRequest = new PlaylistCreateRequest(playlistName) { Public = !command.IsSecret, Description = command.Description }; var createdPlaylist = await spotify.Playlists.Create(user.Id, playlistCreateRequest); createdPlaylists.Add(createdPlaylist); for (var i = 0; i < (double)playlist.Count() / MaxItemsPerRequest; i++) { var request = new PlaylistAddItemsRequest(playlist.Skip(i * MaxItemsPerRequest) .Take(MaxItemsPerRequest) .Select(x => x.Uri) .ToList()); await spotify.Playlists.AddItems(createdPlaylist.Id, request); await taskManager.ReportProgress("Adding tracks"); } } await taskManager.ReportProgress("Complete", true); }
/// <summary> /// Add songs back to the playlist /// </summary> /// <param name="playlistUri">The playlist to add the songs to</param> /// <param name="songsToAdd">The songs to add</param> /// <param name="loops">How many loops of 100 this will take</param> private async Task AddSongsToPlaylistAsync(string playlistUri, List <string> songsToAdd, int loops) { Log(LogType.Info, "Shuffle", "Adding songs back in shuffled order..."); for (int i = 0; i <= loops; i++) { if (i == loops) { if (songsToAdd.Count > 0) { var addRequest = new PlaylistAddItemsRequest(songsToAdd); await Client.Playlists.AddItems(playlistUri, addRequest); Log(LogType.Info, "Shuffle", $"Added back {songsToAdd.Count} songs"); } } else { List <string> songsToAddThisLoop = songsToAdd.GetRange(0, 100); songsToAdd.RemoveRange(0, 100); var addRequest = new PlaylistAddItemsRequest(songsToAddThisLoop); await Client.Playlists.AddItems(playlistUri, addRequest); Log(LogType.Info, "Shuffle", "Added back 100 songs"); } await Task.Delay(50); } }
private async Task ReplacePlaylists(IDictionary <string, IEnumerable <Track> > playlists) { await taskManager.PreventCancellation(); foreach (var(playlistId, tracks) in playlists) { await spotify.Playlists.ReplaceItems(playlistId, new PlaylistReplaceItemsRequest(new List <string>())); await taskManager.ReportProgress("Replacing tracks"); for (var i = 0; i < (double)tracks.Count() / MaxItemsPerRequest; i++) { var request = new PlaylistAddItemsRequest(tracks.Skip(i * MaxItemsPerRequest) .Take(MaxItemsPerRequest) .Select(x => x.Uri) .ToList()); await spotify.Playlists.AddItems(playlistId, request); await taskManager.ReportProgress("Replacing tracks"); } } await taskManager.ReportProgress("Complete", true); }
public async Task <bool> AddToSpotifyPlaylist(string playlistId, IEnumerable <string> uris) { try { var spotify = await Authentication.GetSpotifyClientAsync(); PlaylistAddItemsRequest request = new PlaylistAddItemsRequest(uris.ToList()); await spotify.Playlists.AddItems(playlistId, request); return(true); } catch (Exception) { ViewModels.Helpers.DisplayDialog("Error", "An error occured, please try again"); return(false); } }
/// <summary> /// Creates a new playlist for current user. /// </summary> /// <param name="name"> /// The name of the new playlist. /// </param> /// <param name="tracks"> /// The tracks to add to the newly created playlist (Optional). /// </param> /// <returns></returns> public async Task <FullPlaylist> CreateSpotifyPlaylist(string name, string description, IEnumerable <Track> tracks = null, string base64Jpg = null) { try { var spotify = await Authentication.GetSpotifyClientAsync(); PlaylistCreateRequest request = new PlaylistCreateRequest(name); if (!string.IsNullOrEmpty(description)) { request.Description = description; } var playlist = await spotify.Playlists.Create(Profile.Id, request); if (tracks != null && tracks.Count() > 0) { var plRequest = new PlaylistAddItemsRequest(tracks.Select(c => c.Uri).ToList()); await spotify.Playlists.AddItems(playlist.Id, plRequest); } try { if (!string.IsNullOrEmpty(base64Jpg)) { await spotify.Playlists.UploadCover(playlist.Id, base64Jpg); //how to handle image data thats > 256kb? } } catch (Exception) { } return(await spotify.Playlists.Get(playlist.Id)); } catch (Exception) { ViewModels.Helpers.DisplayDialog("Error", "An error occured while creating your playlist"); return(null); } }
public async Task AddSongsToPlaylist(IList <string> tracksUris, SimplePlaylist playlist) { var songAddRequest = new PlaylistAddItemsRequest(tracksUris); await Client.Playlists.AddItems(playlist.Id, songAddRequest); }
public async Task <List <IPlaylistSyncError> > SyncToSpotify(MusicBeeSyncHelper mb, List <MusicBeePlaylist> mbPlaylistsToSync, bool includeFoldersInPlaylistName = false, bool includeZAtStartOfDatePlaylistName = true) { List <IPlaylistSyncError> errors = new List <IPlaylistSyncError>(); foreach (MusicBeePlaylist playlist in mbPlaylistsToSync) { // Use LINQ to check for a playlist with the same name // If there is one, clear it's contents, otherwise create one // Unless it's been deleted, in which case pretend it doesn't exist. // I'm not sure how to undelete a playlist, or even if you can string spotifyPlaylistName = null; if (includeFoldersInPlaylistName) { spotifyPlaylistName = playlist.Name; } else { spotifyPlaylistName = playlist.Name.Split('\\').Last(); } if (includeZAtStartOfDatePlaylistName) { // if it starts with a 2, it's a date playlist if (spotifyPlaylistName.StartsWith("2")) { spotifyPlaylistName = $"Z {spotifyPlaylistName}"; } } // If Spotify playlist with same name already exists, clear it. // Otherwise create one SimplePlaylist thisPlaylist = Playlists.FirstOrDefault(p => p.Name == spotifyPlaylistName); string thisPlaylistId; if (thisPlaylist != null) { var request = new PlaylistReplaceItemsRequest(new List <string>() { }); var success = await Spotify.Playlists.ReplaceItems(thisPlaylist.Id, request); if (!success) { Log("Error while trying to clear playlist before syncing new tracks"); return(errors); } thisPlaylistId = thisPlaylist.Id; } else { var request = new PlaylistCreateRequest(spotifyPlaylistName); FullPlaylist newPlaylist = await Spotify.Playlists.Create(Profile.Id, request); thisPlaylistId = newPlaylist.Id; } List <FullTrack> songsToAdd = new List <FullTrack>(); // And get the title and artist of each file, and add it to the GMusic playlist foreach (var song in playlist.Songs) { string title = song.Title; string artist = song.Artist; string album = song.Album; string artistEsc = EscapeChar(artist.ToLower()); string titleEsc = EscapeChar(title.ToLower()); string searchStr = $"artist:{artistEsc} track:{titleEsc}"; var request = new SearchRequest(SearchRequest.Types.Track, searchStr); SearchResponse search = await Spotify.Search.Item(request); if (search.Tracks == null || search.Tracks.Items == null) { Log($"Could not find track on Spotify '{searchStr}' for '{title}' by '{artist}'"); continue; } if (search.Tracks.Items.Count == 0) { Log($"Found 0 results on Spotify for: {searchStr} for '{title}' by '{artist}'"); continue; } // try to find track matching artist and title FullTrack trackToAdd = null; foreach (FullTrack track in search.Tracks.Items) { bool titleMatches = (track.Name.ToLower() == title.ToLower()); bool artistMatches = (track.Artists.Exists(a => a.Name.ToLower() == artist.ToLower())); bool albumMatches = (track.Album.Name.ToLower() == album.ToLower()); if (titleMatches && artistMatches && albumMatches) { trackToAdd = track; break; } else if ((titleMatches && artistMatches) || (titleMatches && albumMatches) || (artistMatches && albumMatches)) { // if two of them match, guessing this track is correct is // probably better than just using the firstordefault, but keep looping hoping for a better track trackToAdd = track; } else if (artistMatches && trackToAdd == null) { // if just the artist matches and we haven't found anything yet... this might be our best guess trackToAdd = track; } } if (trackToAdd == null) { trackToAdd = search.Tracks.Items.FirstOrDefault(); Log($"Didn't find a perfect match for {searchStr} for '{title}' by '{artist}', so using '{trackToAdd.Name}' by '{trackToAdd.Artists.FirstOrDefault().Name}' instead"); } songsToAdd.Add(trackToAdd); } List <string> uris = songsToAdd.ConvertAll(x => x.Uri); while (uris.Count > 0) { List <string> currUris = uris.Take(75).ToList(); if (currUris.Count == 0) { break; } uris.RemoveRange(0, currUris.Count); var request = new PlaylistAddItemsRequest(currUris); var resp = await Spotify.Playlists.AddItems(thisPlaylistId, request); if (resp == null) { Log("Error while trying to update playlist with track uris"); return(errors); } } } return(errors); }