Пример #1
0
        private void createSpotifyPlaylists()
        {
            String fileName = fileNameTB.Text;

            if (fileName != null && fileName.Length > 0)
            {
                messageLbl.Text = "";
                XDocument doc = XDocument.Load(fileName);;

                List <XElement> tracksXml = doc.Root.Element("dict").Element("dict").Elements("dict").ToList();;

                List <FullTrack> spotifyTracks = new List <FullTrack>();
                for (int i = 0; i < tracksXml.Count; i++)
                {
                    XElement track  = tracksXml[i];
                    String   name   = getNode(track, "Name").Value;
                    String   artist = getNode(track, "Artist").Value;
                    String   album  = getNode(track, "Album").Value;
                    ProgressLbl.Text = String.Format("Searching for ({0}/{1} -> Name: {2}  Artist: {3}  Album: {4} ", i + 1, tracksXml.Count, name, artist, album);

                    FullTrack spotifyTrack = searchSpotify(name, artist, album);

                    if (spotifyTrack != null)
                    {
                        spotifyTracks.Add(spotifyTrack);
                    }
                    else
                    {
                        AddTrackToErrorLog(name, artist, album);
                    }
                }

                PrivateProfile profile = SpotifyAPI.GetPrivateProfile();
                if (!profile.HasError())
                {
                    String       playlistName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
                    FullPlaylist playlist     = SpotifyAPI.CreatePlaylist(profile.Id, playlistName);
                    if (!playlist.HasError())
                    {
                        messageLbl.Text += "\n\nPlaylist-URI: " + playlist.Uri;
                        foreach (FullTrack spotifyTrack in spotifyTracks)
                        {
                            SpotifyAPI.AddPlaylistTrack(playlist.Id, spotifyTrack.Uri);
                        }
                    }
                    else
                    {
                        messageLbl.Text += "\n\nERROR: " + playlist.Error.Message;
                    }
                }
                else
                {
                    messageLbl.Text += "\n\nERROR: " + profile.Error.Message;
                }
            }
            else
            {
                messageLbl.Text += "Please Choose a file";
            }
        }
        public void AllLibraryToPlaylist(SpotifyWebAPI spotify)
        {
            //saves all track ids to list

            var tracksUri = HelperFunctions.GetSavedTracksUris(spotify);

            Console.WriteLine($"Number of tracks: {tracksUri.Count}");


            // gets user id
            PrivateProfile user = spotify.GetPrivateProfile();

            if (user.HasError())
            {
                Console.WriteLine("\n\nProfileID not read properly. Cannot create playlist.\n\n");
            }
            var userId = user.Id;

            //creates new empty playlist
            FullPlaylist playlist = spotify.CreatePlaylist(userId, "Library to Playlist");

            //error notification
            Console.WriteLine(playlist.HasError()
                ? "/n/nError while creating playlist.\n\n"
                : "Playlist created and named \"Library to Playlist\"");


            //inserts playlist tracks to playlist
            HelperFunctions.InsertTracks(spotify, tracksUri, playlist.Id);

            Console.WriteLine("\nWarning: there may be less songs added to playlist than are present in library, because they might be not available");
            Console.WriteLine("\nTask ended. Press any key to exit. . .");
        }
Пример #3
0
        public async void Init()
        {
            var _clientId = "4dab4bc197084c7db90f8201c5990abe";
            var _secretId = "e5f5c68a50ff48068eb1bfea84cc57a4";

            AuthorizationCodeAuth auth =
                new AuthorizationCodeAuth(_clientId, _secretId, "http://localhost:4002", "http://localhost:4002",
                                          Scope.PlaylistReadPrivate | Scope.PlaylistReadCollaborative);

            auth.Start(); // Starts an internal HTTP Server
            auth.OpenBrowser();

            auth.AuthReceived += async(sender, payload) =>
            {
                auth.Stop();
                Token token = await auth.ExchangeCode(payload.Code);

                SpotifyWebAPI api = new SpotifyWebAPI()
                {
                    TokenType = token.TokenType, AccessToken = token.AccessToken
                };
                // Do requests with API client

                profile = await api.GetPrivateProfileAsync();

                if (!profile.HasError())
                {
                    Console.WriteLine(profile.DisplayName);
                }
            };
        }
Пример #4
0
        public JsonResult NowPlaying()
        {
            Dictionary <string, string> nowPlaying = new Dictionary <string, string>();

            if (User.Identity.IsAuthenticated)
            {
                string accessToken = HttpContext.GetTokenAsync("access_token").Result;

                try
                {
                    // For usage visit: https://github.com/JohnnyCrazy/SpotifyAPI-NET/
                    SpotifyWebAPI spotifyAPI = new SpotifyWebAPI
                    {
                        AccessToken = accessToken,
                        TokenType   = "Bearer"
                    };

                    PrivateProfile  profile = spotifyAPI.GetPrivateProfileAsync().Result;
                    PlaybackContext context = spotifyAPI.GetPlayback();
                    if (!profile.HasError())
                    {
                        nowPlaying.Add("SpotifyName", profile.DisplayName);
                        nowPlaying.Add("SpotifyAvatar", profile.Images.FirstOrDefault().Url);
                        nowPlaying.Add("SpotifyProfileUrl", "https://open.spotify.com/user/" + profile.Id);
                    }

                    if (context.Item != null && (context.IsPlaying))
                    {
                        nowPlaying.Add("SongName", context.Item.Name);
                        nowPlaying.Add("SongUrl", context.Item.Href);
                        nowPlaying.Add("ArtistName", context.Item.Artists.FirstOrDefault().Name);
                        nowPlaying.Add("ArtistUrl", context.Item.Artists.FirstOrDefault().Href);
                        nowPlaying.Add("NowPlaying", nowPlaying["SongName"] + " by " + nowPlaying["ArtistName"]);
                        nowPlaying.Add("AlbumArt", context.Item.Album.Images.FirstOrDefault().Url);
                        nowPlaying.Add("AlbumUrl", context.Item.Album.ExternalUrls["spotify"]);
                        nowPlaying.Add("IsPlaying", "true");
                    }
                    else
                    {
                        nowPlaying.Add("SongName", null);
                        nowPlaying.Add("SongUrl", null);
                        nowPlaying.Add("ArtistName", null);
                        nowPlaying.Add("ArtistUrl", null);
                        nowPlaying.Add("NowPlaying", null);
                        nowPlaying.Add("AlbumArt", null);
                        nowPlaying.Add("AlbumUrl", null);
                        nowPlaying.Add("IsPlaying", "false");
                    }
                    GeniusService geniusService = new GeniusService(nowPlaying["SongName"], nowPlaying["ArtistName"]);
                    nowPlaying.Add("Lyrics", geniusService.Lyrics);
                    nowPlaying.Add("LyricsContribution", geniusService.Url);
                }
                catch (Exception ex)
                {
                    // show the users some error
                }
            }
            return(new JsonResult(nowPlaying));
        }
Пример #5
0
        public static async void Example()
        {
            SpotifyWebAPI api = new SpotifyWebAPI
            {
                AccessToken = "BQBd2F1mg89VXRW6A8cDsGl6sKOmkh6semAAA7mcN18SicdVKJnJcbTPuLP6uMoQW5H9WeqNxi15KMuydiomGpELG6H62MKGXwovcujcRPrAfOGtonfV82ZJfyhsGU8Bh2SRJfNY-0JjQTpKb9sewcy0ANtWqESK8iEkhdxi9P__NHaLk7PfudYaYaHB3_U2NvfoE0UJyS39YDLSAcp63C5f_X3PcMihzO7h8Xs3fMQXjMF-h7_O8baPozJfVQmVviibHs6KJzOarIBjYtykRtS4JjB7Q393syM",
                TokenType   = "Bearer"
            };

            PrivateProfile profile = await api.GetPrivateProfileAsync();

            if (!profile.HasError())
            {
                Console.WriteLine(profile.DisplayName);
            }
        }
Пример #6
0
        internal static ProfileResponse MapProfile(PrivateProfile profile)
        {
            if (profile == null)
            {
                return(new ProfileResponse("Could not reach Spotify services"));
            }

            if (profile.HasError())
            {
                return(new ProfileResponse(profile.Error.Message));
            }

            return(new ProfileResponse(
                       displayName: profile.DisplayName,
                       username: profile.Id));
        }
Пример #7
0
        /// <summary>
        /// Gets the account name by token.
        /// </summary>
        public static async Task <PrivateProfile> GetPrivateAccount(string token)
        {
            SpotifyWebAPI api = new SpotifyWebAPI
            {
                UseAuth     = true,
                AccessToken = token,
                TokenType   = "Bearer"
            };

            PrivateProfile profile = await api.GetPrivateProfileAsync();

            if (!profile.HasError())
            {
                return(profile);
            }

            return(null);
        }
Пример #8
0
        public static async void ConectSpotify()
        {
            SpotifyWebAPI api = new SpotifyWebAPI
            {
                AccessToken = "494597653c5a45f0a29e8cd9f7e8e82c",
                TokenType   = "5f4174f319c040d7b532e8c057c2e26c"
            };

            PrivateProfile profile = await api.GetPrivateProfileAsync();

            if (!profile.HasError())
            {
                Console.WriteLine(profile.DisplayName);
            }
            else
            {
                Console.WriteLine(profile.Error.Message.ToString());
            }
        }
Пример #9
0
        public static void Main()
        {
            auth = new ImplicitGrantAuth(
                _clientId,
                "http://localhost:4002/callback",
                "http://localhost:4002/callback",
                Scope.UserReadPrivate
                );
            auth.AuthReceived += (sender, payload) =>
            {
                auth.Stop(); // `sender` is also the auth instance
                _spotify = new SpotifyWebAPI()
                {
                    TokenType   = payload.TokenType,
                    AccessToken = payload.AccessToken
                };
                // Do requests with API client
            };
            auth.Start(); // Starts an internal HTTP Server
            auth.OpenBrowser();



            PrivateProfile profile = _spotify.GetPrivateProfile();

            if (profile.HasError())
            {
                Console.WriteLine("Error Status: " + profile.Error.Status);
                Console.WriteLine("Error Msg: " + profile.Error.Message);
            }


            FullTrack track = _spotify.GetTrack("3FFjdo3CSKqeGx3nlN0WWv?si=4xDdGmMaTJ-FpPDdQi8MRA");

            Console.WriteLine(track.Name);        //Yeay! We just printed a tracks name.
            Console.WriteLine(track.Popularity);  //Yeay! We just printed a tracks name.
            Console.WriteLine(track.TrackNumber); //Yeay! We just printed a tracks name.
            Console.ReadLine();
        }