GetResponseObject() публичный Метод

public GetResponseObject ( ) : JsonObject
Результат JsonObject
Пример #1
0
        public StationError FetchSessionKey()
        {
            if (authentication_token == null)
            {
                return(StationError.TokenNotAuthorized);
            }

            try {
                LastfmRequest get_session = new LastfmRequest("auth.getSession", RequestType.SessionRequest, ResponseFormat.Json);
                get_session.AddParameter("token", authentication_token);
                get_session.Send();
                var    response = get_session.GetResponseObject();
                object error_code;
                if (response.TryGetValue("error", out error_code))
                {
                    Log.WarningFormat("Lastfm error {0} : {1}", (int)error_code, (string)response["message"]);
                    return((StationError)error_code);
                }

                var session = (Hyena.Json.JsonObject)response["session"];
                UserName   = (string)session["name"];
                SessionKey = (string)session["key"];
                Subscriber = session["subscriber"].ToString().Equals("1");

                // The authentication token is only valid once, and for a limited time
                authentication_token = null;

                return(StationError.None);
            } catch (Exception e) {
                Log.Exception("Error in Lastfm.Account.FetchSessionKey", e);
                return(StationError.Unknown);
            }
        }
Пример #2
0
        public StationError RequestAuthorization()
        {
            LastfmRequest get_token = new LastfmRequest("auth.getToken", RequestType.Read, ResponseFormat.Json);

            get_token.Send();

            var    response = get_token.GetResponseObject();
            object error_code;

            if (response.TryGetValue("error", out error_code))
            {
                Log.WarningFormat("Lastfm error {0} : {1}", (int)error_code, (string)response["message"]);
                return((StationError)error_code);
            }

            authentication_token = (string)response["token"];
            Browser.Open(String.Format("http://www.last.fm/api/auth?api_key={0}&token={1}", LastfmCore.ApiKey, authentication_token));

            return(StationError.None);
        }
        void GetMoreInfo(TrackInfo track)
        {
            var request = new LastfmRequest ("track.getInfo");
            request.AddParameter ("artist", track.ArtistName);
            request.AddParameter ("track", track.TrackTitle);
            request.AddParameter ("mbid", track.MusicBrainzId);

            request.Send ();

            var response = request.GetResponseObject ();
            if (response == null)
                return;
            try
            {
                var json_track = (JsonObject)response["track"];
                //track.Duration = TimeSpan.FromMilliseconds (double.Parse ((string)json_track["duration"]));
                if (!json_track.ContainsKey("album"))
                    return;

                var json_album = (JsonObject)json_track["album"];
                if (json_album != null)
                {
                    var attr = (JsonObject)json_album["@attr"];
                    int pos = 0;
                    if (int.TryParse ((string)attr["position"], out pos)) {
                        track.TrackNumber = pos;
                    }
                    track.AlbumTitle = (string)json_album["title"];
                    track.AlbumMusicBrainzId = (string)json_album["mbid"];
                    track.AlbumArtist = (string)json_album["artist"];
                }
            } catch (Exception e)
            {
                Log.DebugException (e);
                response.Dump ();
            }
        }
        void FetchMetadata(TrackInfo track, int fpid)
        {
            var request = new LastfmRequest ("track.getFingerprintMetadata");
            request.AddParameter ("fingerprintid", fpid.ToString ());
            request.Send ();

            var response = request.GetResponseObject ();
            if (response == null)
                return;

            var json_tracks = (JsonObject)response["tracks"];
            var obj_track = json_tracks["track"];
            JsonObject json_track = null;

            if (obj_track is JsonArray)
                json_track = (JsonObject) (((JsonArray)obj_track)[0]);
            else if (obj_track is JsonObject)
                json_track = (JsonObject)obj_track;

            if (json_track !=null) {
                track.TrackTitle = (string)json_track["name"];
                track.MusicBrainzId = (string)json_track["mbid"];
                track.MoreInfoUri = new SafeUri ((string)json_track["url"]);

                var json_artist = (JsonObject)json_track["artist"];
                if (json_artist != null) {
                    track.ArtistName = (string)json_artist["name"];
                    track.ArtistMusicBrainzId = (string)json_artist["mbid"];
                }

                GetMoreInfo (track);

                track.Update ();

                if (track == ServiceManager.PlayerEngine.CurrentTrack)
                    ServiceManager.PlayerEngine.TrackInfoUpdated ();
            }
        }
Пример #5
0
        public StationError RequestAuthorization ()
        {
            try {
                LastfmRequest get_token = new LastfmRequest ("auth.getToken", RequestType.Read, ResponseFormat.Json);
                get_token.Send ();

                var response = get_token.GetResponseObject ();
                object error_code;
                if (response.TryGetValue ("error", out error_code)) {
                    Log.WarningFormat ("Lastfm error {0} : {1}", (int)error_code, (string)response["message"]);
                    return (StationError) error_code;
                }

                authentication_token = (string)response["token"];
                Browser.Open (String.Format ("http://www.last.fm/api/auth?api_key={0}&token={1}", LastfmCore.ApiKey, authentication_token));

                return StationError.None;
            } catch (Exception e) {
                Log.Exception ("Error in Lastfm.Account.RequestAuthorization", e);
                return StationError.Unknown;
            }
        }
Пример #6
0
        public StationError FetchSessionKey ()
        {
            if (authentication_token == null) {
                return StationError.TokenNotAuthorized;
            }

            try {
                LastfmRequest get_session = new LastfmRequest ("auth.getSession", RequestType.SessionRequest, ResponseFormat.Json);
                get_session.AddParameter ("token", authentication_token);
                get_session.Send ();
                var response = get_session.GetResponseObject ();
                object error_code;
                if (response.TryGetValue ("error", out error_code)) {
                    Log.WarningFormat ("Lastfm error {0} : {1}", (int)error_code, (string)response["message"]);
                    return (StationError) error_code;
                }

                var session = (Hyena.Json.JsonObject)response["session"];
                UserName = (string)session["name"];
                SessionKey = (string)session["key"];
                Subscriber = session["subscriber"].ToString ().Equals ("1");

                // The authentication token is only valid once, and for a limited time
                authentication_token = null;

                return StationError.None;
            } catch (Exception e) {
                Log.Exception ("Error in Lastfm.Account.FetchSessionKey", e);
                return StationError.Unknown;
            }
        }
        private void OnScrobbleResponse(IAsyncResult ar)
        {
            int nb_tracks_scrobbled = 0;

            try {
                current_scrobble_request.EndSend(ar);
                nb_tracks_scrobbled = (int)ar.AsyncState;
            } catch (Exception e) {
                Log.Exception("Failed to complete the scrobble request", e);
                state = State.Idle;
                return;
            }

            JsonObject response = null;

            try {
                response = current_scrobble_request.GetResponseObject();
            } catch (Exception e) {
                Log.Exception("Failed to process the scrobble response", e);
                state = State.Idle;
                return;
            }

            var error = current_scrobble_request.GetError();

            if (error == StationError.ServiceOffline || error == StationError.TemporarilyUnavailable)
            {
                Log.WarningFormat("Lastfm is temporarily unavailable: {0}", (string)response ["message"]);
                next_interval = DateTime.Now + new TimeSpan(0, 0, RETRY_SECONDS);
                hard_failures++;
                state = State.Idle;
            }
            else if (error != StationError.None)
            {
                // TODO: If error == StationError.InvalidSessionKey,
                // suggest to the user to (re)do the Last.fm authentication.
                hard_failures++;

                queue.RemoveInvalidTracks();

                // if there are still valid tracks in the queue then retransmit on the next interval
                if (queue.Count > 0)
                {
                    state = State.NeedTransmit;
                }
                else
                {
                    state = State.Idle;
                }
            }
            else
            {
                try {
                    var scrobbles      = (JsonObject)response["scrobbles"];
                    var scrobbles_attr = (JsonObject)scrobbles["@attr"];
                    Log.InformationFormat("Audioscrobbler upload succeeded: {0} accepted, {1} ignored",
                                          scrobbles_attr["accepted"], scrobbles_attr["ignored"]);

                    if (nb_tracks_scrobbled > 1)
                    {
                        var scrobble_array = (JsonArray)scrobbles["scrobble"];
                        foreach (JsonObject scrobbled_track in scrobble_array)
                        {
                            LogIfIgnored(scrobbled_track);
                        }
                    }
                    else
                    {
                        var scrobbled_track = (JsonObject)scrobbles["scrobble"];
                        LogIfIgnored(scrobbled_track);
                    }
                } catch (Exception) {
                    Log.Information("Audioscrobbler upload succeeded but unknown response received");
                    Log.Debug("Response received", response.ToString());
                }

                hard_failures = 0;

                // we succeeded, pop the elements off our queue
                queue.RemoveRange(0, nb_tracks_scrobbled);
                queue.Save();

                state = State.Idle;
            }
        }