public void SaveAlbum()
 {
     try
     {
         var album = new LibrarySaveAlbumsRequest(new List <string> {
             _albumID
         });
         _spotify.Library.SaveAlbums(album);
         Console.WriteLine("Album Saved.");
     }
     catch (APIUnauthorizedException e)
     {
         Console.WriteLine("Error Status: " + e.Response);
         Console.WriteLine("Error Msg: " + e.Message);
     }
     catch (APIException e)
     {
         Console.WriteLine("Error Status: " + e.Response);
         Console.WriteLine("Error Msg: " + e.Message);
     }
     catch (System.ArgumentOutOfRangeException e)
     {
         Console.WriteLine("Song not found!");
     }
 }
Exemplo n.º 2
0
        private static async Task Start(string _accessToken)
        {
            var spotify = new SpotifyClient(_accessToken);

            SearchRequest  sReq;
            SearchResponse sRes;

            LibrarySaveAlbumsRequest librarySaveAlbumsRequest;
            FollowRequest            followRequest;

            List <string>    IDList      = new List <string>();
            HashSet <string> ArtistIDSet = new HashSet <string>();

            List <string> notAddedMusicList = new List <string>();
            List <string> addedMusicList    = new List <string>();

            SimpleAlbum album;

            for (int i = 1; i <= _artistAlbumPairs.Count; ++i)
            {
                sReq = new SearchRequest(SearchRequest.Types.Album, _artistAlbumPairs[i - 1].Key + " " + _artistAlbumPairs[i - 1].Value);
                sRes = spotify.Search.Item(sReq).Result; // maybe need await?

                if (sRes.Albums.Items.Count != 0)
                {
                    var FoundAlbums = sRes.Albums;

                    album = FoundAlbums.Items[0];

                    ArtistIDSet.Add(album.Artists[0].Id);

                    IDList.Add(album.Id);

                    Console.WriteLine("Added to library: " + album.Artists[0].Name + " - " + album.Name);

                    addedMusicList.Add(album.Artists[0].Name + " - " + album.Name);
                }
                else
                {
                    _notAddedArtistAlbumPairs.Add(_artistAlbumPairs[i - 1]);
                }

                if (i % 50 == 0)
                {
                    librarySaveAlbumsRequest = new LibrarySaveAlbumsRequest(IDList);
                    followRequest            = new FollowRequest(FollowRequest.Type.Artist, new List <string>(ArtistIDSet));

                    await spotify.Library.SaveAlbums(librarySaveAlbumsRequest);

                    await spotify.Follow.Follow(followRequest);

                    IDList      = new List <string>();
                    ArtistIDSet = new HashSet <string>();

                    System.Threading.Thread.Sleep(500);
                }

                if (i == _artistAlbumPairs.Count)
                {
                    if (IDList.Count != 0)
                    {
                        librarySaveAlbumsRequest = new LibrarySaveAlbumsRequest(IDList);
                        followRequest            = new FollowRequest(FollowRequest.Type.Artist, new List <string>(ArtistIDSet));

                        await spotify.Library.SaveAlbums(librarySaveAlbumsRequest);

                        await spotify.Follow.Follow(followRequest);

                        break;
                    }

                    break;
                }
            }

            if (_notAddedArtistAlbumPairs != null && _notAddedArtistAlbumPairs.Count != 0)
            {
                foreach (var pair in _notAddedArtistAlbumPairs)
                {
                    Console.WriteLine("Not added to library: " + pair.Key + " - " + pair.Value);
                }

                foreach (var pair in _notAddedArtistAlbumPairs)
                {
                    notAddedMusicList.Add(pair.Key + " - " + pair.Value);
                }
            }

            WriteListToFile(notAddedMusicList, "not_added.txt");
            WriteListToFile(addedMusicList, "added.txt");

            _server.Dispose();
            Environment.Exit(0);
        }