public static void UpdateTrackFromLastFm(Track track) { try { // load recent tracks from last.fm XElement playedTrack = XElement.Load("http://ws.audioscrobbler.com/2.0/?method=track.getInfo&artist=" + System.Web.HttpUtility.UrlEncode(track.Artist) + "&track=" + System.Web.HttpUtility.UrlEncode(track.TrackName) + "&api_key=" + ConfigurationManager.AppSettings["e3RadioLastFmApiKey"]); // Update track info track.LastFmLink = (string)playedTrack.Element("url"); foreach (var image in playedTrack.Elements("image")) { switch ((string)image.Attribute("size")) { case "small": track.PictureSmall = (string)image; break; case "medium": track.PictureMedium = (string)image; break; case "large": track.PictureLarge = (string)image; break; case "extralarge": track.PictureExtraLarge = (string)image; break; } } track.DateUpdated = DateTime.Now; track.Album = (string)playedTrack.Element("album").Element("title"); } catch { //e.g. track not found on last fm } }
/// <summary> /// Attempts to load track details from spotify uri /// </summary> /// <param name="track"></param> public static bool LookupTrackInfo(Track track) { try { // search for track using Spotify metadata API and get spotify URI System.Xml.Linq.XNamespace ns = "http://www.spotify.com/ns/music/1"; XElement data = XElement.Load("http://ws.spotify.com/lookup/1/?uri=" + System.Web.HttpUtility.UrlEncode(track.SpotifyUri)); track.TrackName = (string)data.Element(ns + "name"); track.Artist = (string)data.Element(ns + "artist").Element(ns + "name"); if (data.Element(ns + "album") != null) { track.Album = (string)data.Element(ns + "album").Element(ns + "name"); } track.Length = (int?)data.Element(ns + "length"); return true; } catch { // prolly rate limited return false; } }
/// <summary> /// Find a Track from it's Spotify URI. /// If it isn't in the DB we will load it's details and create it. /// </summary> /// <param name="db"></param> /// <param name="reqUri"></param> /// <returns></returns> public static Track GetOrCreateTrackBySpotifyUri(E3RadioEntities db, string reqUri) { // find existing track, if it has been played before var track = db.Tracks.FirstOrDefault(t => t.SpotifyUri == reqUri); if (track == null) { // Track is not in the database, load info from spotify/last.fm track = new Track() { SpotifyUri = reqUri, Likes = 0, Dislikes = 0, DateAdded = DateTime.Now }; // look up track info from spotify or die if (!LookupTrackInfo(track)) return null; // update additional track info (cover art) from last.fm LastFM.UpdateTrackFromLastFm(track); db.Tracks.Add(track); } else // Track is already in the database. { // Update fields we recently added from Spotify if (!track.Length.HasValue) LookupTrackInfo(track); // Try to get cover art if not set if (track.PictureSmall == null) LastFM.UpdateTrackFromLastFm(track); } return track; }
/// <summary> /// Format an individual track into a list of anonymous types /// ready to be serialised. /// </summary> /// <param name="track"></param> /// <returns></returns> public static object FormatTrack(Track track) { // Convert track into IQueryable so we can format it var tracks = new List<Data.Track>() { track }.AsQueryable(); return FormatTracks(tracks); }