Exemple #1
0
        public async Task <List <IPlaylistSyncError> > SyncToMusicBee(MusicBeeSyncHelper mb, List <SimplePlaylist> playlists)
        {
            List <IPlaylistSyncError> errors = new List <IPlaylistSyncError>();

            // Go through each playlist we want to sync in turn
            foreach (SimplePlaylist playlist in playlists)
            {
                // Create an empty list for this playlist's local songs
                List <MusicBeeSong> mbPlaylistSongs = new List <MusicBeeSong>();

                // Get all tracks for playlist
                var fp = await Spotify.Playlists.Get(playlist.Id);

                var allTracks = await Spotify.PaginateAll(fp.Tracks);

                var tracks = new List <FullTrack>();
                foreach (var t in allTracks)
                {
                    if (t.Track is FullTrack track)
                    {
                        tracks.Add(track);
                    }
                }

                foreach (FullTrack track in tracks)
                {
                    string       artistStr  = track.Artists.FirstOrDefault().Name.ToLower();
                    string       titleStr   = track.Name.ToLower();
                    MusicBeeSong thisMbSong = mb.Songs.FirstOrDefault(s => s.Artist.ToLower() == artistStr && s.Title.ToLower() == titleStr);
                    if (thisMbSong != null)
                    {
                        mbPlaylistSongs.Add(thisMbSong);
                    }
                    else
                    {
                        errors.Add(new UnableToFindSpotifyTrackError()
                        {
                            AlbumName       = track.Album.Name,
                            ArtistName      = track.Artists.FirstOrDefault().Name,
                            PlaylistName    = playlist.Name,
                            TrackName       = track.Name,
                            SearchedService = false
                        });
                    }
                }

                //mbAPI expects a string array of song filenames to create a playlist
                string[] mbPlaylistSongFiles = new string[mbPlaylistSongs.Count];
                int      i = 0;
                foreach (MusicBeeSong song in mbPlaylistSongs)
                {
                    mbPlaylistSongFiles[i] = song.Filename;
                    i++;
                }

                // Now we need to delete any existing playlist with matching name
                MusicBeePlaylist localPlaylist = mb.Playlists.FirstOrDefault(p => p.Name == playlist.Name);
                if (localPlaylist != null)
                {
                    mb.MbApiInterface.Playlist_DeletePlaylist(localPlaylist.mbName);
                }

                // Create the playlist locally
                string playlistRelativeDir = "";
                string playlistName        = playlist.Name;

                // if it's a date playlist, remove first Z
                if (playlistName.StartsWith("Z "))
                {
                    playlistName = playlistName.Skip(2).ToString();
                }

                string[] itemsInPath = playlist.Name.Split('\\');
                if (itemsInPath.Length > 1)
                {
                    // Creates a playlist at top level directory
                    mb.MbApiInterface.Playlist_CreatePlaylist("", playlistName, mbPlaylistSongFiles);
                }

                mb.MbApiInterface.Playlist_CreatePlaylist(playlistRelativeDir, playlistName, mbPlaylistSongFiles);
            }

            // Get the local playlists again
            mb.RefreshMusicBeePlaylists();

            return(errors);
        }
Exemple #2
0
        public async Task <List <IPlaylistSyncError> > SyncToMusicBee(MusicBeeSyncHelper mb, List <Playlist> playlists)
        {
            List <IPlaylistSyncError> errors = new List <IPlaylistSyncError>();

            // Go through each playlist we want to sync in turn
            foreach (Playlist playlist in playlists)
            {
                // Create an empty list for this playlist's local songs
                List <MusicBeeSong> mbPlaylistSongs = new List <MusicBeeSong>();

                // For each entry in the playlist we're syncing, get the song from the GMusic library we've downloaded,
                // Get the song Title and Artist and then look it up in the list of local songs.
                // If we find it, add it to the list of local songs
                foreach (PlaylistEntry entry in playlist.Songs)
                {
                    Track thisSong = GoogleSongsFetched.FirstOrDefault(s => s.Id == entry.TrackID || s.NID == entry.TrackID);

                    // if we couldn't find it, attempt to fetch it by trackID and cache it
                    if (IsTrackInvalid(thisSong))
                    {
                        thisSong = await api.GetTrackAsync(entry.TrackID);

                        if (IsTrackInvalid(thisSong))
                        {
                            errors.Add(new UnableToFindGooglePlaylistEntryError()
                            {
                                GpmPlaylistName     = playlist.Name,
                                GpmPlaylistPosition = entry.AbsolutePosition,
                                GpmTrackId          = entry.TrackID,
                            });
                        }
                        else
                        {
                            GoogleSongsFetched.Add(thisSong);
                        }
                    }

                    if (!IsTrackInvalid(thisSong))
                    {
                        MusicBeeSong thisMbSong = mb.Songs.FirstOrDefault(s => s.Artist == thisSong.Artist && s.Title == thisSong.Title && s.Album == thisSong.Album);
                        if (thisMbSong != null)
                        {
                            mbPlaylistSongs.Add(thisMbSong);
                        }
                        else
                        {
                            errors.Add(new UnableToFindGPMTrackError()
                            {
                                AlbumName       = thisSong.Album,
                                ArtistName      = thisSong.Artist,
                                PlaylistName    = playlist.Name,
                                TrackName       = thisSong.Title,
                                SearchedService = false,
                            });
                        }
                    }
                }

                //mbAPI expects a string array of song filenames to create a playlist
                string[] mbPlaylistSongFiles = new string[mbPlaylistSongs.Count];
                int      i = 0;
                foreach (MusicBeeSong song in mbPlaylistSongs)
                {
                    mbPlaylistSongFiles[i] = song.Filename;
                    i++;
                }

                // Now we need to delete any existing playlist with matching name
                MusicBeePlaylist localPlaylist = mb.Playlists.FirstOrDefault(p => p.Name == playlist.Name);
                if (localPlaylist != null)
                {
                    mb.MbApiInterface.Playlist_DeletePlaylist(localPlaylist.mbName);
                }

                // Create the playlist locally
                string playlistRelativeDir = "";
                string playlistName        = playlist.Name;

                string[] itemsInPath = playlist.Name.Split('\\');
                if (itemsInPath.Length > 1)
                {
                    // Creates a playlist at top level directory
                    mb.MbApiInterface.Playlist_CreatePlaylist("", playlistName, mbPlaylistSongFiles);
                }

                mb.MbApiInterface.Playlist_CreatePlaylist(playlistRelativeDir, playlistName, mbPlaylistSongFiles);
            }

            // Get the local playlists again
            mb.RefreshMusicBeePlaylists();

            return(errors);
        }