using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
namespace IF.Lastfm.Core.Objects
{
///
/// TODO Wiki, Images, Stream availability
///
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 TopTags { get; set; }
#endregion
///
/// Parses the given JToken into a track
///
/// A valid JToken
/// track equivalent to the JToken
/// If this method is used directly then the duration attribute will be parsed as MILLIseconds
public static Track ParseJToken(JToken token)
{
var t = new Track();
t.Name = token.Value("name");
t.Mbid = token.Value("mbid");
t.Url = new Uri(token.Value("url"), UriKind.Absolute);
t.ArtistName = token.SelectToken("artist").Value("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("duration");
t.Duration = TimeSpan.FromMilliseconds(secs);
return t;
}
///
/// Parses the given JToken into a track
///
/// A valid JToken
/// Name of the album this track belongs to
/// track equivalent to the JToken
/// If this method is used then the duration attribute will be parsed as seconds
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("duration");
t.Duration = TimeSpan.FromSeconds(secs);
return t;
}
}
}