Tag and track parsing

This commit is contained in:
Rikki Tooley 2013-06-13 16:29:52 +01:00
parent 8f2fd9c10f
commit f15f6adbb3
3 changed files with 95 additions and 4 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@ -31,8 +32,6 @@ public class Album
/// <summary> /// <summary>
/// TODO datetime parsing /// TODO datetime parsing
/// TODO images /// TODO images
/// TODO tags
/// TODO tracks
/// </summary> /// </summary>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
@ -49,6 +48,12 @@ public static Album ParseJToken(JToken token)
a.Url = new Uri(token.Value<string>("url"), UriKind.Absolute); a.Url = new Uri(token.Value<string>("url"), UriKind.Absolute);
var tracksToken = token.SelectToken("tracks").SelectToken("track");
a.Tracks = tracksToken.Children().Select(trackToken => Track.ParseJToken(trackToken, a.Name));
var tagsToken = token.SelectToken("toptags").SelectToken("tag");
a.TopTags = tagsToken.Children().Select(Tag.ParseJToken);
return a; return a;
} }
} }

View File

@ -1,6 +1,25 @@
namespace IF.Lastfm.Core.Objects using System;
using Newtonsoft.Json.Linq;
namespace IF.Lastfm.Core.Objects
{ {
public class Tag public class Tag
{ {
#region Properties
public string Name { get; set; }
public Uri Url { get; set; }
#endregion
public static Tag ParseJToken(JToken token)
{
var t = new Tag();
t.Name = token.Value<string>("name");
t.Url = new Uri(token.Value<string>("url"), UriKind.Absolute);
return t;
}
} }
} }

View File

@ -1,6 +1,73 @@
namespace IF.Lastfm.Core.Objects using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
namespace IF.Lastfm.Core.Objects
{ {
/// <summary>
/// TODO Wiki, Images, Stream availability
/// </summary>
public class Track public class Track
{ {
#region Properties
public string Name { get; set; }
public TimeSpan Duration { get; set; }
public string Mbid { get; set; }
public string ArtistName { get; set; }
public Uri Url { get; set; }
public string AlbumName { get; set; }
public int? ListenerCount { get; set; }
public int? TotalPlayCount { get; set; }
public IEnumerable<Tag> TopTags { get; set; }
#endregion
/// <summary>
/// Parses the given JToken into a track
/// </summary>
/// <param name="token">A valid JToken</param>
/// <returns>track equivalent to the JToken</returns>
/// <remarks>If this method is used directly then the duration attribute will be parsed as MILLIseconds</remarks>
public static Track ParseJToken(JToken token)
{
var t = new Track();
t.Name = token.Value<string>("name");
t.Mbid = token.Value<string>("mbid");
t.Url = new Uri(token.Value<string>("url"), UriKind.Absolute);
t.ArtistName = token.SelectToken("artist").Value<string>("name");
var tagsToken = token.SelectToken("toptags").SelectToken("tag");
t.TopTags = tagsToken.Children().Select(Tag.ParseJToken);
// api returns milliseconds when track.getInfo is called directly
var secs = token.Value<double>("duration");
t.Duration = TimeSpan.FromMilliseconds(secs);
return t;
}
/// <summary>
/// Parses the given JToken into a track
/// </summary>
/// <param name="token">A valid JToken</param>
/// <param name="albumName">Name of the album this track belongs to</param>
/// <returns>track equivalent to the JToken</returns>
/// <remarks>If this method is used then the duration attribute will be parsed as seconds</remarks>
public static Track ParseJToken(JToken token, string albumName)
{
var t = ParseJToken(token);
t.AlbumName = albumName;
// the api returns seconds for this value when not track.getInfo
var secs = token.Value<double>("duration");
t.Duration = TimeSpan.FromSeconds(secs);
return t;
}
} }
} }