public static IEnumerable<SpotifyPlaylist> GetUsersFollowedPlaylists(SpotifyUser oSpotifyUser)
        {
            List<SpotifyPlaylist> Playlists = new List<SpotifyPlaylist>();
            using (SpotifyWebAPI oWebCLient = GetWebClient(oSpotifyUser))
            {
                Paging<SimplePlaylist> oPaging = oWebCLient.GetUserPlaylists(oSpotifyUser.Name);

                foreach (SimplePlaylist oSimplePlaylist in oPaging.Items)
                {
                    if (!oSimplePlaylist.Owner.Id.Equals(oSpotifyUser.Name))
                    {
                        Dictionary<int, SpotifyTrack> oTracks = GetTracksForPlaylist(oWebCLient, oSimplePlaylist, oSpotifyUser);
                        Playlists.Add(new SpotifyPlaylist(oSimplePlaylist, oTracks));
                    }
                }
                while (oPaging.Next != null)
                {
                    oPaging = oWebCLient.DownloadData<Paging<SimplePlaylist>>(oPaging.Next);
                    foreach (SimplePlaylist oSimplePlaylist in oPaging.Items)
                    {
                        if (!oSimplePlaylist.Owner.Id.Equals(oSpotifyUser.Name))
                        {
                            Dictionary<int, SpotifyTrack> oTracks = GetTracksForPlaylist(oWebCLient, oSimplePlaylist, oSpotifyUser);
                            Playlists.Add(new SpotifyPlaylist(oSimplePlaylist, oTracks));
                        }
                    }
                }
            }
            return Playlists;
        }
 public static string GetUsersEmail(SpotifyUser oSpotifyUser)
 {
     string sEmail;
     using (SpotifyWebAPI oWebCLient = GetWebClient(oSpotifyUser))
     {
         PrivateProfile oPrivateProfile = oWebCLient.GetPrivateProfile();
         sEmail = oPrivateProfile.Email;
     }
     return sEmail;
 }
        public static IEnumerable<SpotifyUser> GetSpotifyUsersAndTokens()
        {
            List<SpotifyUser> oUsers = new List<SpotifyUser>();
            DataSet ds = RelationalDatabase.ExecuteStoredProcedure("GetSpotifyUsersAndSpotifyTokens");
            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                SpotifyUser oUser = new SpotifyUser(dr);
                oUsers.Add(oUser);
                oUser = null;
            }
            return oUsers;
        }
        public static SpotifyUser GetSpotifyUserForGuid(string sUserGuid)
        {
            SpotifyUser oSpotifyUser = null;
            DataSet ds = RelationalDatabase.ExecuteStoredProcedure("GetSpotifyUsersAndSpotifyTokens");
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr["UserGuid"].ToString().ToUpper().Equals(sUserGuid.ToUpper()))
                {
                    oSpotifyUser = new SpotifyUser(dr);
                }

            }
            return oSpotifyUser;
        }
        private static Dictionary<int, SpotifyTrack> GetTracksForPlaylist(SpotifyWebAPI oWebCLient, SimplePlaylist oSimplePlaylist, SpotifyUser oSpotifyUser)
        {
            Dictionary<int, SpotifyTrack> oTracks = new Dictionary<int, SpotifyTrack>();
            Paging<PlaylistTrack> oPagingPlaylistTracks = oWebCLient.GetPlaylistTracks(oSimplePlaylist.Owner.Id, oSimplePlaylist.Id, market:"");
            int iPos = 0;
            foreach (PlaylistTrack oPlaylistTrack in oPagingPlaylistTracks.Items)
            {
                List<SpotifyArtist> oArtists = new List<SpotifyArtist>();
                oArtists.AddRange(GetArtistsForTrack(oPlaylistTrack));
                if (oPlaylistTrack.Track.Id == null)
                {
                    if (oPlaylistTrack.Track.Name != null && oPlaylistTrack.Track.Type != null)
                    {//Issues with some tracks having null id properties and the only way to deal with that is to try to hash an id myself
                        oPlaylistTrack.Track.Id = HashSlingingSlasher.HashString(oSimplePlaylist.Name + oSimplePlaylist.Type);
                    }

                }
                oTracks.Add(iPos, new SpotifyTrack(oPlaylistTrack, oArtists));
                iPos++;
            }
            while (oPagingPlaylistTracks.Next != null)
            {
                oPagingPlaylistTracks = oWebCLient.DownloadData<Paging<PlaylistTrack>>(oPagingPlaylistTracks.Next);
                foreach (PlaylistTrack oPlaylistTrack in oPagingPlaylistTracks.Items)
                {
                    List<SpotifyArtist> oArtists = new List<SpotifyArtist>();
                    oArtists.AddRange(GetArtistsForTrack(oPlaylistTrack));
                    if (oPlaylistTrack.Track.Id == null)
                    {
                        if (oPlaylistTrack.Track.Name != null && oPlaylistTrack.Track.Type != null)
                        {//Issues with some tracks having null id properties and the only way to deal with that is to try to hash an id myself
                            oPlaylistTrack.Track.Id = HashSlingingSlasher.HashString(oSimplePlaylist.Name + oSimplePlaylist.Type);
                        }

                    }
                    oTracks.Add(iPos, new SpotifyTrack(oPlaylistTrack, oArtists));
                    iPos++;
                }
            }

            return oTracks;
        }
        private static SpotifyWebAPI GetWebClient(SpotifyUser oSpotifyUser)
        {
            AutorizationCodeAuth oAuth = new AutorizationCodeAuth()
            {
                ClientId = _ClientPublic,
                RedirectUri= _RedirectUrl,
                Scope = Scope.UserReadPrivate | Scope.UserReadPrivate | Scope.PlaylistReadPrivate | Scope.UserLibraryRead |  Scope.UserReadPrivate | Scope.UserFollowRead
            };

            //oAuth.StartHttpServer();//Not sure if this is needed or not but what the hell why not!

            if (oSpotifyUser.AccessToken.AccessExpired)//The user has authorized us and was tokenized but the temp access token has expired
            {
                Token oToken = oAuth.RefreshToken(oSpotifyUser.RefreshToken.Code, _ClientPrivate);
                if (oToken.Error == null)
                {
                    oSpotifyUser.UpdateUserWithToken(oToken);
                }
            }

            //oAuth.StopHttpServer();
            SpotifyWebAPI oWebClient = new SpotifyWebAPI()
            {
                AccessToken = oSpotifyUser.AccessToken.Code,
                TokenType = oSpotifyUser.AccessToken.TokenType,
                UseAuth = true
            };

            return oWebClient;
        }
        public static void SaveUserAndAuthToDatabase(SpotifyUser oSpotifyUser)
        {

            DataSet ds = RelationalDatabase.ExecuteStoredProcedure("AddSpotifyUser",
                                                    new SqlParameter("iUserID", oSpotifyUser.ID),
                                                    new SqlParameter("sUserName", oSpotifyUser.Name),
                                                    new SqlParameter("sAuthCode", oSpotifyUser.UserAuth.Code),
                                                    new SqlParameter("dtAuth", oSpotifyUser.UserAuth.AuthDate)
                                               );
            oSpotifyUser.SessionGuid = ds.Tables[0].Rows[0]["SessionGuid"].ToString();


        }
Exemplo n.º 8
0
 public SpotifyUser AddSpotifyUser(PrivateProfile oPrivateProfile, string sAuthCode, Token oToken)
 {
     SpotifyUser oSpotifyUser = new SpotifyUser(oPrivateProfile, sAuthCode, oToken);
     return oSpotifyUser;
 }
Exemplo n.º 9
0
 public SpotifyUser AddSpotifyUser(string sProfileID, string sAuthCode, string sAccessCode, string sRefreshCode, string sAccessTokenType, int iExpiresIn, DateTime dtToken, DateTime dtAuth)
 {
     SpotifyUser oSpotifyUser = new SpotifyUser(sProfileID, sAuthCode, sAccessCode, sRefreshCode, sAccessTokenType, iExpiresIn, dtToken, dtAuth);
     return oSpotifyUser;
 }