github twitter linkedin email rss
Last.fm API for a Windows Phone App – Scrobbling a Track
Jan 19, 2013
2 minutes read

As I discussed the basic of authentication in my previous post, the most important Last.fm feature that is added to Beem in itself is track scrobbling, which will allow you to keep records of what you listened to from your favorite music aggregation service. The implementation of the method used to send the track from the app to Last.fm is extremely similar to GetMobileSession.

public void ScrobbleTrack(string artist, string track, string sessionKey, Action<string> onCompletion)
{
  string currentTimestamp = DateHelper.GetUnixTimestamp();
  
  var parameters = new Dictionary<string, string>();
  parameters.Add("artist[0]", artist);
  parameters.Add("track[0]", track);
  parameters.Add("timestamp[0]", currentTimestamp);
  parameters.Add("method", "track.scrobble");
  parameters.Add("api_key", API_KEY);
  parameters.Add("sk", sessionKey);
  
  string signature = GetSignature(parameters);
  
  string comboUrl = string.Concat(CORE_URL, "?method=track.scrobble", "&api_key=", API_KEY,
  "&artist[0]=", artist, "&track[0]=", track, "&sk=", sessionKey,
  "&timestamp[0]=", currentTimestamp,
  "&api_sig=", signature);
  
  var client = new WebClient();
  client.UploadStringAsync(new Uri(comboUrl), string.Empty);
  client.UploadStringCompleted += (s, e) =>
  {
    try
    {
      onCompletion(e.Result);
    }
    catch (WebException ex)
    {
      HttpWebResponse response = (HttpWebResponse)ex.Response;
      using (StreamReader reader = new StreamReader(response.GetResponseStream()))
      {
        Debug.WriteLine(reader.ReadToEnd());
      }
    }
  };
}

The new required parameters here are the artist name, the track name, a UNIX-style timestamp and the session key that you obtained from the core authentication method. Although there is no method in C# to give you the UNIX timestamp right away, you can easily do it like this:

using System;
 
namespace Beem.Utility
{
  public static class DateHelper
  {
    public static string GetUnixTimestamp()
    {
      TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
      return ((int)t.TotalSeconds).ToString();
    }
  }
}

Also notice that the parameters for the track are sent in array format. Since I am only scrobbling one track at a time, I can use the index zero [0]. Your situation might be different. ScrobbleTrack can be invoked like this:

LastFmClient client = new LastFmClient();
client.ScrobbleTrack("Armin van Buuren", "In and Out Of Love", "SESSION_KEY", (s) =>
{
  Debug.WriteLine("Success!");
});

You should now see the track registered on Last.fm.


Back to posts