mirror of
https://github.com/Sarsoo/IF.Lastfm.git
synced 2024-10-17 07:13:09 +01:00
Tag and track parsing
This commit is contained in:
parent
8f2fd9c10f
commit
f15f6adbb3
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
@ -31,8 +32,6 @@ public class Album
|
||||
/// <summary>
|
||||
/// TODO datetime parsing
|
||||
/// TODO images
|
||||
/// TODO tags
|
||||
/// TODO tracks
|
||||
/// </summary>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
@ -49,6 +48,12 @@ public static Album ParseJToken(JToken token)
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,25 @@
|
||||
namespace IF.Lastfm.Core.Objects
|
||||
using System;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace IF.Lastfm.Core.Objects
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
||||
#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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user