/// <summary> /// Send the now playing notification. /// </summary> /// <param name="track"> /// A <see cref="NowplayingTrack"/> /// </param> public void ReportNowplaying(NowplayingTrack track) { RequestParameters p = new RequestParameters(); p["api_key"] = session.APIKey; p["album"] = track.Album; p["artist"] = track.Artist; p["duration"] = ((int)track.Duration.TotalSeconds).ToString(); p["method"] = "track.updateNowPlaying"; p["sk"] = session.SessionKey; p["track"] = track.Title; string api_sig = ""; foreach (var item in p) { api_sig += item.Key + item.Value; } api_sig += session.APISecret; p["api_sig"] = Utilities.MD5(api_sig); Request request = new Request(new Uri("http://ws.audioscrobbler.com/2.0/"), p); // A BadSessionException occurs when another client has made a handshake // with this user's credentials, should redo a handshake and pass this // exception quietly. try { request.execute(); } catch (BadSessionException ex) { //this.doHandshake(); this.ReportNowplaying(track); } }
/// <summary> /// A handshake is performed at the beginning to retrieve the /// SessionID, SubmissionURL and NowplayingURL and also everytime the server /// reports that the SessionID is invalid. /// </summary> private void doHandshake() { Request request = new Request(new Uri("http://post.audioscrobbler.com:80/"), handshakeParameters); string[] response = request.execute().Split('\n'); SessionID = response[1]; NowplayingURL = new Uri(response[2]); SubmissionURL = new Uri(response[3]); }
/// <summary> /// The internal scrobble function, scrobbles pure request parameters. /// Could be for more than one track, as specified by Last.fm, but they recommend that /// only one track should be submitted at a time. /// </summary> /// <param name="parameters"> /// A <see cref="RequestParameters"/> /// </param> internal void Scrobble(RequestParameters parameters) { Initialize(); parameters["s"] = SessionID; Request request = new Request(this.SubmissionURL, parameters); // A BadSessionException occurs when another client has made a handshake // with this user's credentials, should redo a handshake and pass this // exception quietly. try { request.execute(); } catch (BadSessionException) { this.doHandshake(); this.Scrobble(parameters); } }
/// <summary> /// The internal scrobble function, scrobbles pure request parameters. /// Could be for more than one track, as specified by Last.fm, but they recommend that /// only one track should be submitted at a time. /// </summary> /// <param name="parameters"> /// A <see cref="RequestParameters"/> /// </param> internal void Scrobble(RequestParameters parameters) { //Initialize(); Request request = new Request(new Uri("http://ws.audioscrobbler.com/2.0/"), parameters); // A BadSessionException occurs when another client has made a handshake // with this user's credentials, should redo a handshake and pass this // exception quietly. try { request.execute(); } catch (BadSessionException) { //this.doHandshake(); this.Scrobble(parameters); } }
/// <summary> /// Send the now playing notification. /// </summary> /// <param name="track"> /// A <see cref="NowplayingTrack"/> /// </param> public void ReportNowplaying(NowplayingTrack track) { Initialize(); RequestParameters p = new RequestParameters(); p["s"] = SessionID; p["a"] = track.Artist; p["t"] = track.Title; p["b"] = track.Album; if (track.Duration.TotalSeconds == 0) { p["l"] = ""; } else { p["l"] = track.Duration.TotalSeconds.ToString(); } if (track.Number == 0) { p["n"] = ""; } else { p["n"] = track.Number.ToString(); } p["m"] = track.MBID; Request request = new Request(this.NowplayingURL, p); // A BadSessionException occurs when another client has made a handshake // with this user's credentials, should redo a handshake and pass this // exception quietly. try { request.execute(); } catch (BadSessionException) { this.doHandshake(); this.ReportNowplaying(track); } }
/// <summary> /// Send the now playing notification. /// </summary> /// <param name="track"> /// A <see cref="NowplayingTrack"/> /// </param> public void ReportNowplaying(NowplayingTrack track) { Initialize(); RequestParameters p = new RequestParameters(); p["s"] = SessionID; p["a"] = track.Artist; p["t"] = track.Title; p["b"] = track.Album; if (track.Duration.TotalSeconds == 0) p["l"] = ""; else p["l"] = track.Duration.TotalSeconds.ToString(); if (track.Number == 0) p["n"] = ""; else p["n"] = track.Number.ToString(); p["m"] = track.MBID; Request request = new Request(this.NowplayingURL, p); // A BadSessionException occurs when another client has made a handshake // with this user's credentials, should redo a handshake and pass this // exception quietly. try { request.execute(); } catch (BadSessionException) { this.doHandshake(); this.ReportNowplaying(track); } }