From 2235ba075339bfedcf2da91b229bd4cd57bf0903 Mon Sep 17 00:00:00 2001 From: Rikki Tooley Date: Sun, 28 Jul 2013 00:07:15 +0100 Subject: [PATCH] album.getinfo, album.getshouts, artist.getshouts, user.getinfo, user.getshouts --- IF.Lastfm.Core/Api/AlbumApi.cs | 25 ++++---- .../AlbumApi/GetAlbumShoutsCommand.cs | 61 ++++++++++++++++++ .../ArtistApi/GetArtistShoutsCommand.cs | 25 +------- .../TrackApi/GetTrackShoutsCommand.cs | 25 +------- .../TrackApi/ScrobbleTracksCommand.cs | 52 ++++++++++++++++ .../Commands/UserApi/GetUserInfoCommand.cs | 53 ++++++++++++++++ .../Commands/UserApi/GetUserShoutsCommand.cs | 55 ++++++++++++++++ IF.Lastfm.Core/Api/Enums/LastStatsTimeSpan.cs | 7 +++ IF.Lastfm.Core/Api/IAlbumApi.cs | 27 ++++---- IF.Lastfm.Core/Api/ITrackApi.cs | 1 + IF.Lastfm.Core/Api/IUserApi.cs | 6 ++ IF.Lastfm.Core/Api/TrackApi.cs | 3 +- IF.Lastfm.Core/Api/UserApi.cs | 57 +++++------------ IF.Lastfm.Core/IF.Lastfm.Core.csproj | 4 ++ IF.Lastfm.Core/Objects/Album.cs | 10 ++- IF.Lastfm.Core/Objects/Shout.cs | 30 +++++++++ IF.Lastfm.Core/Objects/User.cs | 62 +++++++++++++++++++ 17 files changed, 387 insertions(+), 116 deletions(-) create mode 100644 IF.Lastfm.Core/Api/Commands/AlbumApi/GetAlbumShoutsCommand.cs create mode 100644 IF.Lastfm.Core/Api/Commands/TrackApi/ScrobbleTracksCommand.cs create mode 100644 IF.Lastfm.Core/Api/Commands/UserApi/GetUserInfoCommand.cs create mode 100644 IF.Lastfm.Core/Api/Commands/UserApi/GetUserShoutsCommand.cs create mode 100644 IF.Lastfm.Core/Objects/User.cs diff --git a/IF.Lastfm.Core/Api/AlbumApi.cs b/IF.Lastfm.Core/Api/AlbumApi.cs index 56519e5..f6905f9 100644 --- a/IF.Lastfm.Core/Api/AlbumApi.cs +++ b/IF.Lastfm.Core/Api/AlbumApi.cs @@ -1,15 +1,8 @@ using System; -using System.Collections.Generic; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Threading; using System.Threading.Tasks; using IF.Lastfm.Core.Api.Commands.AlbumApi; -using IF.Lastfm.Core.Api.Enums; using IF.Lastfm.Core.Api.Helpers; using IF.Lastfm.Core.Objects; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; namespace IF.Lastfm.Core.Api { @@ -37,12 +30,6 @@ public Task> GetBuyLinksForAlbumAsync(string artist, strin throw new NotImplementedException(); } - public Task> GetShoutsForAlbumAsync(string artist, string album, bool autocorrect = false, int page = 1, - int itemsPerPage = LastFm.DefaultPageLength) - { - throw new NotImplementedException(); - } - public Task> GetUserTagsForAlbumAsync(string artist, string album, string username, bool autocorrect = false) { throw new NotImplementedException(); @@ -57,5 +44,17 @@ public Task> SearchForAlbumAsync(string album, int page = 1, { throw new NotImplementedException(); } + + public async Task> GetShoutsAsync(string albumname, string artistname, bool autocorrect = false, int page = 1, int count = LastFm.DefaultPageLength) + { + var command = new GetAlbumShoutsCommand(Auth, albumname, artistname) + { + Page = page, + Autocorrect = autocorrect, + Count = count + }; + + return await command.ExecuteAsync(); + } } } \ No newline at end of file diff --git a/IF.Lastfm.Core/Api/Commands/AlbumApi/GetAlbumShoutsCommand.cs b/IF.Lastfm.Core/Api/Commands/AlbumApi/GetAlbumShoutsCommand.cs new file mode 100644 index 0000000..d5d1cbe --- /dev/null +++ b/IF.Lastfm.Core/Api/Commands/AlbumApi/GetAlbumShoutsCommand.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Threading.Tasks; +using IF.Lastfm.Core.Api.Enums; +using IF.Lastfm.Core.Api.Helpers; +using IF.Lastfm.Core.Objects; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace IF.Lastfm.Core.Api.Commands.AlbumApi +{ + internal class GetAlbumShoutsCommand : GetAsyncCommandBase> + { + public string AlbumName { get; set; } + public string ArtistName { get; set; } + public bool Autocorrect { get; set; } + + public GetAlbumShoutsCommand(IAuth auth, string albumname, string artistname) + : base(auth) + { + Method = "album.getShouts"; + AlbumName = albumname; + ArtistName = artistname; + } + + public override Uri BuildRequestUrl() + { + var parameters = new Dictionary + { + {"album", Uri.EscapeDataString(AlbumName)}, + {"artist", Uri.EscapeDataString(ArtistName)}, + {"autocorrect", Convert.ToInt32(Autocorrect).ToString()} + }; + + base.AddPagingParameters(parameters); + base.DisableCaching(parameters); + + var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); + return new Uri(apiUrl, UriKind.Absolute); + } + + public async override Task> HandleResponse(HttpResponseMessage response) + { + string json = await response.Content.ReadAsStringAsync(); + + LastFmApiError error; + if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode) + { + JToken jtoken = JsonConvert.DeserializeObject(json).SelectToken("shouts"); + + return Shout.ParsePageJToken(jtoken); + } + else + { + return PageResponse.CreateErrorResponse(error); + } + } + } +} \ No newline at end of file diff --git a/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistShoutsCommand.cs b/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistShoutsCommand.cs index 2342df3..c5c8fa2 100644 --- a/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistShoutsCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistShoutsCommand.cs @@ -45,30 +45,9 @@ public async override Task> HandleResponse(HttpResponseMessa LastFmApiError error; if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode) { - JToken jtoken = JsonConvert.DeserializeObject(json).SelectToken("shouts"); - var shoutsToken = jtoken.SelectToken("shout"); + var jtoken = JsonConvert.DeserializeObject(json).SelectToken("shouts"); - var pageresponse = PageResponse.CreateSuccessResponse(); - pageresponse.AddPageInfoFromJToken(jtoken.SelectToken("@attr")); - - var shouts = new List(); - if (shoutsToken != null && pageresponse.TotalItems > 0) - { - if (pageresponse.Page == pageresponse.TotalPages - && pageresponse.TotalItems % pageresponse.PageSize == 1) - { - // array notation isn't used on the api if there is only one shout. - shouts.Add(Shout.ParseJToken(shoutsToken)); - } - else - { - shouts.AddRange(shoutsToken.Children().Select(Shout.ParseJToken)); - } - } - - pageresponse.Content = shouts; - - return pageresponse; + return Shout.ParsePageJToken(jtoken); } else { diff --git a/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackShoutsCommand.cs b/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackShoutsCommand.cs index 0461912..7da6a5d 100644 --- a/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackShoutsCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackShoutsCommand.cs @@ -48,30 +48,9 @@ public async override Task> HandleResponse(HttpResponseMessa LastFmApiError error; if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode) { - JToken jtoken = JsonConvert.DeserializeObject(json).SelectToken("shouts"); - var shoutsToken = jtoken.SelectToken("shout"); - - var pageresponse = PageResponse.CreateSuccessResponse(); - pageresponse.AddPageInfoFromJToken(jtoken.SelectToken("@attr")); - - var shouts = new List(); - if (shoutsToken != null && pageresponse.TotalItems > 0) - { - if (pageresponse.Page == pageresponse.TotalPages - && pageresponse.TotalItems % pageresponse.PageSize == 1) - { - // array notation isn't used on the api if there is only one shout. - shouts.Add(Shout.ParseJToken(shoutsToken)); - } - else - { - shouts.AddRange(shoutsToken.Children().Select(Shout.ParseJToken)); - } - } + var jtoken = JsonConvert.DeserializeObject(json).SelectToken("shouts"); - pageresponse.Content = shouts; - - return pageresponse; + return Shout.ParsePageJToken(jtoken); } else { diff --git a/IF.Lastfm.Core/Api/Commands/TrackApi/ScrobbleTracksCommand.cs b/IF.Lastfm.Core/Api/Commands/TrackApi/ScrobbleTracksCommand.cs new file mode 100644 index 0000000..846bef9 --- /dev/null +++ b/IF.Lastfm.Core/Api/Commands/TrackApi/ScrobbleTracksCommand.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using IF.Lastfm.Core.Api.Helpers; +using IF.Lastfm.Core.Objects; +using Newtonsoft.Json.Linq; + +namespace IF.Lastfm.Core.Api.Commands.TrackApi +{ + internal class ScrobbleTracksCommand : PostAsyncCommandBase + { + public ScrobbleTracksCommand(IAuth auth, Scrobble scrobble) : base(auth) + { + ConstructInternal(auth, new[] { scrobble }); + } + + public ScrobbleTracksCommand(IAuth auth, IEnumerable scrobbles) : base(auth) + { + ConstructInternal(auth, scrobbles); + } + + private void ConstructInternal(IAuth auth, IEnumerable scrobbles) + { + Method = "track.scrobble"; + } + + public override Task ExecuteAsync() + { + var parameters = new Dictionary + { + {"artist", scrobble.Artist}, + {"album", scrobble.Album}, + {"track", scrobble.Track}, + {"albumArtist", scrobble.AlbumArtist}, + {"chosenByUser", scrobble.ChosenByUser.ToInt().ToString()}, + {"timestamp", scrobble.TimePlayed.ToUnixTimestamp().ToString()}, + {"sk", Auth.User.Token} + }; + + HttpContent post = new StringContent(); + + } + + public override Task HandleResponse(HttpResponseMessage response) + { + throw new NotImplementedException(); + } + } +} diff --git a/IF.Lastfm.Core/Api/Commands/UserApi/GetUserInfoCommand.cs b/IF.Lastfm.Core/Api/Commands/UserApi/GetUserInfoCommand.cs new file mode 100644 index 0000000..ce0e71c --- /dev/null +++ b/IF.Lastfm.Core/Api/Commands/UserApi/GetUserInfoCommand.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Threading.Tasks; +using IF.Lastfm.Core.Api.Enums; +using IF.Lastfm.Core.Api.Helpers; +using IF.Lastfm.Core.Objects; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace IF.Lastfm.Core.Api.Commands.UserApi +{ + internal class GetUserInfoCommand : GetAsyncCommandBase> + { + public string Username { get; set; } + + public GetUserInfoCommand(IAuth auth, string username) : base(auth) + { + Method = "user.getInfo"; + Username = username; + } + + public override Uri BuildRequestUrl() + { + var parameters = new Dictionary + { + {"user", Uri.EscapeDataString(Username)} + }; + + base.DisableCaching(parameters); + + var uristring = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); + return new Uri(uristring, UriKind.Absolute); + } + + public async override Task> HandleResponse(HttpResponseMessage response) + { + string json = await response.Content.ReadAsStringAsync(); + + LastFmApiError error; + if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode) + { + var jtoken = JsonConvert.DeserializeObject(json); + + return LastResponse.CreateSuccessResponse(User.ParseJToken(jtoken.SelectToken("user"))); + } + else + { + return LastResponse.CreateErrorResponse(error); + } + } + } +} \ No newline at end of file diff --git a/IF.Lastfm.Core/Api/Commands/UserApi/GetUserShoutsCommand.cs b/IF.Lastfm.Core/Api/Commands/UserApi/GetUserShoutsCommand.cs new file mode 100644 index 0000000..ff15ff5 --- /dev/null +++ b/IF.Lastfm.Core/Api/Commands/UserApi/GetUserShoutsCommand.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Threading.Tasks; +using IF.Lastfm.Core.Api.Enums; +using IF.Lastfm.Core.Api.Helpers; +using IF.Lastfm.Core.Objects; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace IF.Lastfm.Core.Api.Commands.UserApi +{ + internal class GetUserShoutsCommand : GetAsyncCommandBase> + { + public string Username { get; set; } + + public GetUserShoutsCommand(IAuth auth, string username) : base(auth) + { + Method = "user.getShouts"; + Username = username; + } + + public override Uri BuildRequestUrl() + { + var parameters = new Dictionary + { + {"user", Uri.EscapeDataString(Username)}, + }; + + base.AddPagingParameters(parameters); + base.DisableCaching(parameters); + + var uristring = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); + return new Uri(uristring, UriKind.Absolute); + } + + public async override Task> HandleResponse(HttpResponseMessage response) + { + string json = await response.Content.ReadAsStringAsync(); + + LastFmApiError error; + if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode) + { + JToken jtoken = JsonConvert.DeserializeObject(json).SelectToken("shouts"); + + return Shout.ParsePageJToken(jtoken); + } + else + { + return PageResponse.CreateErrorResponse(error); + } + } + } +} \ No newline at end of file diff --git a/IF.Lastfm.Core/Api/Enums/LastStatsTimeSpan.cs b/IF.Lastfm.Core/Api/Enums/LastStatsTimeSpan.cs index ab3c648..ac299bd 100644 --- a/IF.Lastfm.Core/Api/Enums/LastStatsTimeSpan.cs +++ b/IF.Lastfm.Core/Api/Enums/LastStatsTimeSpan.cs @@ -22,4 +22,11 @@ public enum LastStatsTimeSpan [ApiName("12month")] Year } + + public enum Gender + { + Other = 0, + Male, + Female + } } \ No newline at end of file diff --git a/IF.Lastfm.Core/Api/IAlbumApi.cs b/IF.Lastfm.Core/Api/IAlbumApi.cs index fe1a794..5a2c9e6 100644 --- a/IF.Lastfm.Core/Api/IAlbumApi.cs +++ b/IF.Lastfm.Core/Api/IAlbumApi.cs @@ -1,6 +1,4 @@ -using System.Collections; -using System.Collections.Generic; -using System.Threading.Tasks; +using System.Threading.Tasks; using IF.Lastfm.Core.Api.Helpers; using IF.Lastfm.Core.Objects; @@ -16,17 +14,24 @@ Task> GetBuyLinksForAlbumAsync(string artist, string album, CountryCode country, bool autocorrect = false); - - Task> GetShoutsForAlbumAsync(string artist, + + Task> GetUserTagsForAlbumAsync(string artist, string album, - bool autocorrect = false, + string username, + bool autocorrect = false); + + Task> GetTopTagsForAlbumAsync(string artist, + string album, + bool autocorrect = false); + + Task> SearchForAlbumAsync(string album, int page = 1, int itemsPerPage = LastFm.DefaultPageLength); - - Task> GetUserTagsForAlbumAsync(string artist, string album, string username, bool autocorrect = false); - Task> GetTopTagsForAlbumAsync(string artist, string album, bool autocorrect = false); - - Task> SearchForAlbumAsync(string album, int page = 1, int itemsPerPage = LastFm.DefaultPageLength); + Task> GetShoutsAsync(string albumname, + string artistname, + bool autocorrect = false, + int page = 1, + int count = LastFm.DefaultPageLength); } } \ No newline at end of file diff --git a/IF.Lastfm.Core/Api/ITrackApi.cs b/IF.Lastfm.Core/Api/ITrackApi.cs index 6697daf..905682e 100644 --- a/IF.Lastfm.Core/Api/ITrackApi.cs +++ b/IF.Lastfm.Core/Api/ITrackApi.cs @@ -14,6 +14,7 @@ public interface ITrackApi Task> GetShoutsForTrackAsync(string trackname, string artistname, + bool autocorrect = false, int page = 0, int count = LastFm.DefaultPageLength); diff --git a/IF.Lastfm.Core/Api/IUserApi.cs b/IF.Lastfm.Core/Api/IUserApi.cs index 23c5138..64f2164 100644 --- a/IF.Lastfm.Core/Api/IUserApi.cs +++ b/IF.Lastfm.Core/Api/IUserApi.cs @@ -23,5 +23,11 @@ Task> GetRecentScrobbles(string username, Task> GetRecentStations(string username, int pagenumber, int count = LastFm.DefaultPageLength); + + Task> GetShoutsAsync(string username, + int pagenumber, + int count = LastFm.DefaultPageLength); + + Task> GetInfoAsync(string username); } } \ No newline at end of file diff --git a/IF.Lastfm.Core/Api/TrackApi.cs b/IF.Lastfm.Core/Api/TrackApi.cs index b7d3009..23e8593 100644 --- a/IF.Lastfm.Core/Api/TrackApi.cs +++ b/IF.Lastfm.Core/Api/TrackApi.cs @@ -60,12 +60,13 @@ public Task ScrobbleAsync(IEnumerable scrobble) throw new NotImplementedException(); } - public async Task> GetShoutsForTrackAsync(string trackname, string artistname, int page = 0, int count = LastFm.DefaultPageLength) + public async Task> GetShoutsForTrackAsync(string trackname, string artistname, bool autocorrect = false, int page = 0, int count = LastFm.DefaultPageLength) { var command = new GetTrackShoutsCommand(Auth, trackname, artistname) { Page = page, Count = count, + Autocorrect = autocorrect }; return await command.ExecuteAsync(); } diff --git a/IF.Lastfm.Core/Api/UserApi.cs b/IF.Lastfm.Core/Api/UserApi.cs index f8d8760..5d0c8fc 100644 --- a/IF.Lastfm.Core/Api/UserApi.cs +++ b/IF.Lastfm.Core/Api/UserApi.cs @@ -1,14 +1,9 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; using System.Threading.Tasks; using IF.Lastfm.Core.Api.Commands.UserApi; using IF.Lastfm.Core.Api.Enums; using IF.Lastfm.Core.Api.Helpers; using IF.Lastfm.Core.Objects; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; namespace IF.Lastfm.Core.Api { @@ -68,48 +63,24 @@ public async Task> GetRecentStations(string username, int }; return await command.ExecuteAsync(); - - const string apiMethod = "user.getRecentStations"; + } - var methodParameters = new Dictionary - { - {"user", Auth.User.Username}, - {"page", pagenumber.ToString()}, - {"limit", count.ToString()}, - {"sk", Auth.User.Token} - }; + public async Task> GetShoutsAsync(string username, int pagenumber, int count = LastFm.DefaultPageLength) + { + var command = new GetUserShoutsCommand(Auth, username) + { + Page = pagenumber, + Count = count + }; - var apisig = Auth.GenerateMethodSignature(apiMethod, methodParameters); + return await command.ExecuteAsync(); + } - var postContent = LastFm.CreatePostBody(apiMethod, - Auth.ApiKey, - apisig, - methodParameters); + public async Task> GetInfoAsync(string username) + { + var command = new GetUserInfoCommand(Auth, username); - var httpClient = new HttpClient(); - var lastResponse = await httpClient.PostAsync(LastFm.ApiRoot, postContent); - var json = await lastResponse.Content.ReadAsStringAsync(); - - LastFmApiError error; - if (LastFm.IsResponseValid(json, out error) && lastResponse.IsSuccessStatusCode) - { - var jtoken = JsonConvert.DeserializeObject(json).SelectToken("recentstations"); - - var stationsToken = jtoken.SelectToken("station"); - - var stations = stationsToken.Children().Select(Station.ParseJToken).ToList(); - - var pageresponse = PageResponse.CreateSuccessResponse(stations); - - var attrToken = jtoken.SelectToken("@attr"); - pageresponse.AddPageInfoFromJToken(attrToken); - - return pageresponse; - } - else - { - return PageResponse.CreateErrorResponse(error); - } + return await command.ExecuteAsync(); } } } \ No newline at end of file diff --git a/IF.Lastfm.Core/IF.Lastfm.Core.csproj b/IF.Lastfm.Core/IF.Lastfm.Core.csproj index 2482db4..9a54879 100644 --- a/IF.Lastfm.Core/IF.Lastfm.Core.csproj +++ b/IF.Lastfm.Core/IF.Lastfm.Core.csproj @@ -42,6 +42,7 @@ + @@ -53,6 +54,8 @@ + + @@ -67,6 +70,7 @@ + diff --git a/IF.Lastfm.Core/Objects/Album.cs b/IF.Lastfm.Core/Objects/Album.cs index 2e5fe43..a3a3a04 100644 --- a/IF.Lastfm.Core/Objects/Album.cs +++ b/IF.Lastfm.Core/Objects/Album.cs @@ -49,10 +49,16 @@ internal static Album ParseJToken(JToken token) a.Url = new Uri(token.Value("url"), UriKind.Absolute); var tracksToken = token.SelectToken("tracks").SelectToken("track"); - a.Tracks = tracksToken.Children().Select(trackToken => Track.ParseJToken(trackToken, a.Name)); + if (tracksToken != null) + { + 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); + if (tagsToken != null) + { + a.TopTags = tagsToken.Children().Select(Tag.ParseJToken); + } return a; } diff --git a/IF.Lastfm.Core/Objects/Shout.cs b/IF.Lastfm.Core/Objects/Shout.cs index e5873c7..6ea2c4d 100644 --- a/IF.Lastfm.Core/Objects/Shout.cs +++ b/IF.Lastfm.Core/Objects/Shout.cs @@ -1,5 +1,8 @@ using System; +using System.Collections.Generic; using System.Globalization; +using System.Linq; +using IF.Lastfm.Core.Api.Helpers; using Newtonsoft.Json.Linq; namespace IF.Lastfm.Core.Objects @@ -36,5 +39,32 @@ public static Shout ParseJToken(JToken token) return s; } + + public static PageResponse ParsePageJToken(JToken token) + { + var shoutsToken = token.SelectToken("shout"); + + var pageresponse = PageResponse.CreateSuccessResponse(); + pageresponse.AddPageInfoFromJToken(token.SelectToken("@attr")); + + var shouts = new List(); + if (shoutsToken != null && pageresponse.TotalItems > 0) + { + if (pageresponse.Page == pageresponse.TotalPages + && pageresponse.TotalItems % pageresponse.PageSize == 1) + { + // array notation isn't used on the api if there is only one shout. + shouts.Add(Shout.ParseJToken(shoutsToken)); + } + else + { + shouts.AddRange(shoutsToken.Children().Select(Shout.ParseJToken)); + } + } + + pageresponse.Content = shouts; + + return pageresponse; + } } } \ No newline at end of file diff --git a/IF.Lastfm.Core/Objects/User.cs b/IF.Lastfm.Core/Objects/User.cs new file mode 100644 index 0000000..3f3dd0e --- /dev/null +++ b/IF.Lastfm.Core/Objects/User.cs @@ -0,0 +1,62 @@ +using System; +using IF.Lastfm.Core.Api.Enums; +using Newtonsoft.Json.Linq; + +namespace IF.Lastfm.Core.Objects +{ + public class User + { + #region Properties + + public string Name { get; set; } + public string FullName { get; set; } + public LastImageCollection Avatar { get; set; } + public string Id { get; set; } + public int Age { get; set; } + public string Country { get; set; } + public Gender Gender { get; set; } + public bool IsSubscriber { get; set; } + public int Playcount { get; set; } + public DateTime TimeRegistered { get; set; } + + #endregion + + /// + /// TODO + /// "gender": "m", + //"playcount": "79972", + //"playlists": "4", + //"bootstrap": "0", + //"registered": { + // "#text": "2002-11-20 11:50", + // "unixtime": "1037793040" + //}, + //"type": "alumni" + /// + /// + /// + internal static User ParseJToken(JToken token) + { + var u = new User(); + + u.Name = token.Value("name"); + u.FullName = token.Value("realname"); + u.Country = token.Value("country"); + u.Id = token.Value("id"); + + var subscribed = token.SelectToken("subscriber"); + if (subscribed != null) + { + u.IsSubscriber = Convert.ToBoolean(subscribed.Value()); + } + + var images = token.SelectToken("image"); + if (images != null) + { + u.Avatar = LastImageCollection.ParseJToken(images); + } + + return u; + } + } +} \ No newline at end of file