Exemplo n.º 1
0
        public TrackResponse GetInfo(Track track, Authentication authentication, string userName)
        {
            Dictionary <string, string> parameters = new Dictionary <string, string>();

            parameters.Add(Track.ArtistNameParamName, track.ArtistName);
            parameters.Add(Track.TrackNameParamName, track.TrackName);

            if (!String.IsNullOrEmpty(userName))
            {
                parameters.Add("username", userName);
            }

            var request = LastFmApiUtils.AddRequiredParams(Method.POST, parameters, GetInfoMethodName, authentication);

            var response = LastFmApiUtils.LastFmRestClient.Execute <GetInfoResponse>(request);
            Uri url      = response.ResponseUri;

            LastFmApiUtils.checkResponce(response);

            if (response.Data != null && response.Data.track != null)
            {
                return(response.Data.track);
            }
            else
            {
                return(null);
            }
        }
        public TrackResponse GetTracks(Track track, Authentication authentication, string userName)
        {
            Dictionary <string, string> parameters = new Dictionary <string, string>();

            parameters.Add(Track.ArtistNameParamName, track.ArtistName);
            parameters.Add(Track.TrackNameParamName, track.TrackName);
            parameters.Add("limit", "1");
            parameters.Add("page", "1");

            if (!String.IsNullOrEmpty(userName))
            {
                parameters.Add("user", userName);
            }

            var request = LastFmApiUtils.AddRequiredParams(Method.POST, parameters, GetTracksMethodName, authentication);


            var response = LastFmApiUtils.LastFmRestClient.Execute <GetTracksResponse>(request);

            // TEST
            //request.Method = Method.GET;
            //var uri = LastFmApiUtils.LastFmRestClient.BuildUri(request);
            //Debug.WriteLine(uri);
            //Uri url = response.ResponseUri;
            LastFmApiUtils.checkResponce(response);

            if (response.Data != null && response.Data.tracks != null)
            {
                return(response.Data.tracks.track);
            }

            return(null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Fetch an unathorized request token for an API account
        /// </summary>
        public AuthenticationToken GetToken(Authentication authentication)
        {
            var parameters = new Dictionary <string, string>();
            var request    = LastFmApiUtils.AddRequiredParams(Method.GET, parameters, GetTokenMethodName, authentication, false);

            var response = LastFmApiUtils.LastFmRestClient.Execute <GetTokenResponse>(request);

            LastFmApiUtils.checkResponce(response);

            return(new AuthenticationToken(response.Data.token));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Notifies Last.fm that a user has started listening to a track.
        /// </summary>
        /// <param name="track">A <see cref="Track"/> DTO containing track details</param>
        /// <param name="authentication"><see cref="Authentication"/> object</param>
        /// <returns>A <see cref="ScrobbleResponse"/>DTO containing details of Last.FM's response</returns>
        /// <remarks>It is important to not use the corrections returned by the now playing service as input for the scrobble request,
        /// unless they have been explicitly approved by the user</remarks>
        public bool UpdateNowPlaying(Track track, Authentication authentication)
        {
            Dictionary <string, string> parameters = TrackToNameValueCollection(track);

            var request = LastFmApiUtils.AddRequiredParams(Method.POST, parameters, UpdateNowPlayingMethodName, authentication);

            // send request
            try
            {
                var response = LastFmApiUtils.LastFmRestClient.Execute <UpdateNowPlayingResponse>(request);
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Add a track-play to a user's profile
        /// </summary>
        /// <param name="track">A <see cref="Track"/> DTO containing track details</param>
        /// <param name="authentication"><see cref="Authentication"/> object</param>
        /// <returns>A <see cref="ScrobbleResponse"/>DTO containing details of Last.FM's response</returns>
        public bool Scrobble(Track track, Authentication authentication)
        {
            Dictionary <string, string> parameters = TrackToNameValueCollection(track);

            var request = LastFmApiUtils.AddRequiredParams(Method.POST, parameters, ScrobbleMethodName, authentication);

            // send request
            try
            {
                LastFmApiUtils.LastFmRestClient.Execute(request);
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Fetch a session key for a user
        /// </summary>
        public void GetSession(Authentication authentication, AuthenticationToken token)
        {
            var parameters = new Dictionary <string, string>();

            parameters.Add("token", token.Value);

            var request = LastFmApiUtils.AddRequiredParams(Method.GET, parameters, GetSessionMethodName, authentication);

            var response = LastFmApiUtils.LastFmRestClient.Execute <GetSessionResponse>(request);

            LastFmApiUtils.checkResponce(response);

            if (response.Data == null || response.Data.error != null)
            {
                throw new LastFmApiException("token error");
            }

            authentication.Session = new Session
            {
                Subscriber = response.Data.session.subscriber,
                Key        = response.Data.session.key,
                Username   = response.Data.session.name
            };
        }
        public List <Artist> GetSimilar(Track track, Authentication authentication)
        {
            Dictionary <string, string> parameters = new Dictionary <string, string>();

            parameters.Add(Track.ArtistNameParamName, track.ArtistName);
            parameters.Add("limit", "6");

            var request = LastFmApiUtils.AddRequiredParams(Method.POST, parameters, GetTracksMethodName, authentication);


            var response = LastFmApiUtils.LastFmRestClient.Execute <GetSimilarResponse>(request);

            LastFmApiUtils.checkResponce(response);

            if (response.Data != null && response.Data.similarartists != null)
            {
                if (response.Data.similarartists.artist.Count > 0)
                {
                    return(response.Data.similarartists.artist);
                }
            }

            return(null);
        }