Пример #1
0
        public bool ModifyLike() //Return true if the song becomes liked. False otherwise.
        {
            FullTrack currentTrack = m_instance.GetLatestTrack();

            if (currentTrack != null)
            {
                List <string> currentID = new List <string> {
                    currentTrack.Id
                };

                List <bool> list = m_spotifyWebAPI.CheckSavedTracks(currentID).List;

                if (list.Count == 1)
                {
                    if (list[0]) // If liked, dislike.
                    {
                        m_spotifyWebAPI.RemoveSavedTracks(currentID);
                        return(false);
                    }
                    else // If disliked, like.
                    {
                        m_spotifyWebAPI.SaveTracks(currentID);
                        return(true);
                    }
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Пример #2
0
        public Boolean CheckTracks(string id)
        {
            ListResponse <bool> tracksSaved = _spotify.CheckSavedTracks(new List <String> {
                id
            });

            if (tracksSaved.List[0])
            {
                return(true);
            }
            return(false);
        }
Пример #3
0
        public async Task <bool> MigrateLibrary(string userToken)
        {
            // Lets handle the pagination of the response by calling it as a while loop
            var offsetValue = "";

            do
            {
                var appleLibraryResponse = await _appleMusicClient.GetUserLibrary(userToken, offsetValue);

                offsetValue = appleLibraryResponse.Next;
                List <string> songsToSave = new List <string>();
                foreach (var song in appleLibraryResponse.Data)
                {
                    // Attributes to search
                    var songTitle   = song.Attributes.Name;
                    var artistTitle = song.Attributes.ArtistName;

                    // Search result
                    var spotifySong = await _spotifyClient.SearchItemsAsync($"{songTitle},{artistTitle}"
                                                                            , SearchType.Track);

                    // Search result was empty continue to next song
                    if (!spotifySong.Tracks.Items.Any())
                    {
                        // Create a json file here to write all songs not found
                        continue;
                    }

                    // It's okay to use the first item in the list since we're going one at a time
                    var spotifySongId = spotifySong.Tracks.Items[0].Id;

                    var userAlreadyHasSong = _spotifyClient.CheckSavedTracks(new List <string>()
                    {
                        spotifySongId
                    }).List[0];
                    if (!userAlreadyHasSong)
                    {
                        songsToSave.Add(spotifySong.Tracks.Items[0].Id);
                    }
                }

                await _spotifyClient.SaveTracksAsync(songsToSave);

                Console.WriteLine(appleLibraryResponse);
            } while (!String.IsNullOrEmpty(offsetValue));

            return(true);
        }
Пример #4
0
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            // If we're looking to make the timer temporarily useless to reduce API rate call
            if (_pauseAPI)
            {
                return;
            }

            try
            {
                PlaybackContext context = _spotify.GetPlayingTrack();
                // Take control over the UI elements
                Dispatcher.Invoke(() =>
                {
                    if (context.Error == null && context.Item != null)
                    {
                        if (context.Item.Error == null)
                        {
                            Title.Content  = context.Item.Name;
                            Author.Content = context.Item.Artists[0].Name;

                            // Check to make sure the song changed before refreshing variables
                            if ((context.Item.Album.Images[0].Url != cover.Source.ToString()) && (context.Item.Album.Images[0].Url != null))
                            {
                                BitmapImage albumArt = new BitmapImage();
                                albumArt.BeginInit();
                                albumArt.UriSource = new Uri(context.Item.Album.Images[0].Url);
                                albumArt.EndInit();
                                cover.Source = albumArt;

                                // Sets to solid heart if we liked it already
                                _songID = context.Item.Id;
                                ListResponse <bool> tracksSaved = _spotify.CheckSavedTracks(new System.Collections.Generic.List <String> {
                                    _songID
                                });
                                if (tracksSaved.List[0])
                                {
                                    loveClick.Content = "♥";
                                }
                                else
                                {
                                    loveClick.Content = "♡";
                                }

                                // Gets the total length of the song
                                progress = context.Item.DurationMs;
                            }

                            // Animates the progress bar to fluidly move to position
                            DoubleAnimation animation = new DoubleAnimation();
                            animation.From            = progressBar.ActualWidth;
                            animation.To           = ((double)(context.ProgressMs) / (double)(progress)) * this.Width;
                            animation.Duration     = new Duration(TimeSpan.FromMilliseconds(250));
                            animation.FillBehavior = FillBehavior.Stop;
                            progressBar.BeginAnimation(Rectangle.WidthProperty, animation);
                            progressBar.Width = ((double)(context.ProgressMs) / (double)(progress)) * this.Width;
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("Inside- " + context.Error.Message);
                        }
                    }
                    else if (context.Error != null)
                    {
                        System.Diagnostics.Debug.WriteLine("Outside- " + context.Error.Message);

                        // If the issue is that the auth didn't go through, we try to set up a new connection
                        if (context.Error.Message == "Only valid bearer authentication supported")
                        {
                            _spotify = new SpotifyWebAPI()
                            {
                                AccessToken = (string)Application.Current.Properties["AccessToken"],
                                TokenType   = (string)Application.Current.Properties["TokenType"]
                            };
                        }
                        else if (context.Error.Message == "The access token expired")
                        {
                            Debug.WriteLine("Attempting to refresh token due to expiration");
                            spotAPI.Authenticate();
                            _spotify = new SpotifyWebAPI()
                            {
                                AccessToken = (string)Application.Current.Properties["AccessToken"],
                                TokenType   = (string)Application.Current.Properties["TokenType"]
                            };
                        }
                    }
                });
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine("Error main timer- " + err.Message);
            }
        }
Пример #5
0
        public void UpdateSpot()
        {
            if (initExcpt != null)
            {
                return;
            }

            if (api == null)
            {
                return;
            }

            try
            {
                initExcpt = null;

                var retrievedPlayback = api.GetPlayback();
                if (retrievedPlayback != null)
                {
                    cachedPlayback = retrievedPlayback;
                    if (cachedPlayback.HasError() && cachedPlayback.Error.Message == "The access token expired" && authorized)
                    {
                        authorized = false;
                        Authorize();
                    }
                }

                if (cachedPlayback != null && cachedPlayback.Item != null)
                {
                    var ListedItem = new List <string>(1);
                    ListedItem.Add(cachedPlayback.Item.Id);
                    var response = api.CheckSavedTracks(ListedItem);
                    if (response.List == null)
                    {
                        if (response.HasError() && response.Error.Message == "The access token expired" && authorized)
                        {
                            authorized = false;
                            Authorize();
                        }
                    }
                    else
                    {
                        cachedLikedTrack = response.List[0];
                    }

                    if (cachedPlayback.Context == null)
                    {
                    }
                    else if (cachedPlayback.Context.Type == "playlist")
                    {
                        var split       = cachedPlayback.Context.ExternalUrls["spotify"].Split('/');
                        var newPlaylist = api.GetPlaylist(split[4]);
                        if (newPlaylist != null && !newPlaylist.HasError())
                        {
                            cachedPlaylist = newPlaylist;
                        }
                    }
                    else if (cachedPlayback.Context.Type == "album")
                    {
                        var newAlbum = api.GetAlbum(cachedPlayback.Item.Album.Id);
                        if (newAlbum != null && !newAlbum.HasError())
                        {
                            cachedAlbum = newAlbum;
                        }

                        if (upNextAlbumTrack != null)
                        {
                            if (cachedPlayback.Item.TrackNumber + 1 > newAlbum.Tracks.Items.Count)
                            {   // just go back to 0 i guess?
                                upNextAlbumTrackFull = api.GetTrack(newAlbum.Tracks.Items[0].Id);
                            }
                            else
                            {
                                upNextAlbumTrackFull = api.GetTrack(newAlbum.Tracks.Items[cachedPlayback.Item.TrackNumber + 1].Id);
                            }
                        }
                        else
                        {
                            upNextAlbumTrackFull = null;
                        }
                    }
                    else if (cachedPlayback.Context.Type == "artist")
                    {
                        var split  = cachedPlayback.Context.ExternalUrls["spotify"].Split('/');
                        var region = RegionInfo.CurrentRegion;

                        upNextAlbumTrackFull = null;

                        var artistTracks = api.GetArtistsTopTracks(split[4], region.TwoLetterISORegionName);
                        var thisArtist   = api.GetArtist(split[4]);

                        if (thisArtist != null)
                        {
                            cachedArtist = thisArtist;
                        }

                        for (int i = 0; i < artistTracks.Tracks.Count; i++)
                        {
                            if (artistTracks.Tracks[i].Id == cachedPlayback.Item.Id)
                            {
                                if (i == artistTracks.Tracks.Count - 1)
                                {
                                    upNextAlbumTrackFull = artistTracks.Tracks[0];
                                    break;
                                }
                                else
                                {
                                    upNextAlbumTrackFull = artistTracks.Tracks[i + 1];
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            catch (AggregateException /*e*/)
            {
                //initExcpt = e; // This is common if the task was dropped
            }
        }