diff --git a/src/IF.Lastfm.Core/Api/ArtistApi.cs b/src/IF.Lastfm.Core/Api/ArtistApi.cs index bdb9a457..74cb3da5 100644 --- a/src/IF.Lastfm.Core/Api/ArtistApi.cs +++ b/src/IF.Lastfm.Core/Api/ArtistApi.cs @@ -1,4 +1,4 @@ -using System.Net.Http; +using System.Net.Http; using IF.Lastfm.Core.Api.Helpers; using IF.Lastfm.Core.Objects; using System.Threading.Tasks; @@ -15,13 +15,12 @@ public ArtistApi(ILastAuth auth, HttpClient httpClient = null) Auth = auth; } - - - public async Task> GetInfoAsync(string artist, string bioLang = LastFm.DefaultLanguageCode, bool autocorrect = false) + public async Task> GetInfoAsync(string artist, string username = "", string bioLang = LastFm.DefaultLanguageCode, bool autocorrect = false) { var command = new GetInfoCommand(Auth) { ArtistName = artist, + Username = username, BioLanguage = bioLang, Autocorrect = autocorrect, HttpClient = HttpClient @@ -174,4 +173,4 @@ public async Task> SearchAsync(string artistname, int p return await command.ExecuteAsync(); } } -} \ No newline at end of file +} diff --git a/src/IF.Lastfm.Core/Api/Commands/Artist/GetInfoCommand.cs b/src/IF.Lastfm.Core/Api/Commands/Artist/GetInfoCommand.cs index 9b9b8ab5..56ad6574 100644 --- a/src/IF.Lastfm.Core/Api/Commands/Artist/GetInfoCommand.cs +++ b/src/IF.Lastfm.Core/Api/Commands/Artist/GetInfoCommand.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Net.Http; using System.Threading.Tasks; using IF.Lastfm.Core.Api.Enums; @@ -20,6 +20,8 @@ internal class GetInfoCommand : GetAsyncCommandBase> public bool Autocorrect { get; set; } + public string Username { get; set; } + public GetInfoCommand(ILastAuth auth) : base(auth) { } /// @@ -35,12 +37,17 @@ public override void SetParameters() { Parameters.Add("artist", ArtistName); } - + if (BioLanguage != null) { Parameters.Add("lang", BioLanguage); } - + + if (!string.IsNullOrWhiteSpace(Username)) + { + Parameters.Add("username", Username); + } + Parameters.Add("autocorrect", Convert.ToInt32(Autocorrect).ToString()); DisableCaching(); diff --git a/src/IF.Lastfm.Core/Api/Commands/User/GetTopTracksCommand.cs b/src/IF.Lastfm.Core/Api/Commands/User/GetTopTracksCommand.cs new file mode 100644 index 00000000..405fe15c --- /dev/null +++ b/src/IF.Lastfm.Core/Api/Commands/User/GetTopTracksCommand.cs @@ -0,0 +1,51 @@ +using System; +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.User +{ + [ApiMethodName("user.getTopTracks")] + internal class GetTopTracksCommand : GetAsyncCommandBase> + { + public string Username { get; private set; } + public LastStatsTimeSpan Period { get; set; } + + public GetTopTracksCommand(ILastAuth auth, string username) : base(auth) + { + Username = username; + } + + public override void SetParameters() + { + Parameters.Add("user", Username); + Parameters.Add("period", Period.ToString()); + + AddPagingParameters(); + DisableCaching(); + } + + public override async Task> HandleResponse(HttpResponseMessage response) + { + var json = await response.Content.ReadAsStringAsync(); + + LastResponseStatus status; + if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode) + { + var jtoken = JsonConvert.DeserializeObject(json).SelectToken("toptracks"); + var itemsToken = jtoken.SelectToken("track"); + var attrToken = jtoken.SelectToken("@attr"); + + return PageResponse.CreateSuccessResponse(itemsToken, attrToken, LastTrack.ParseJToken, LastPageResultsType.Attr); + } + else + { + return LastResponse.CreateErrorResponse>(status); + } + } + } +} diff --git a/src/IF.Lastfm.Core/Api/IArtistApi.cs b/src/IF.Lastfm.Core/Api/IArtistApi.cs index 65b460f8..5d71de7a 100644 --- a/src/IF.Lastfm.Core/Api/IArtistApi.cs +++ b/src/IF.Lastfm.Core/Api/IArtistApi.cs @@ -1,4 +1,4 @@ -using System.Threading.Tasks; +using System.Threading.Tasks; using IF.Lastfm.Core.Api.Helpers; using IF.Lastfm.Core.Objects; @@ -8,7 +8,7 @@ public interface IArtistApi { ILastAuth Auth { get; } - Task> GetInfoAsync(string artist, string bioLang = LastFm.DefaultLanguageCode, + Task> GetInfoAsync(string artist, string username = "", string bioLang = LastFm.DefaultLanguageCode, bool autocorrect = false); Task> GetInfoByMbidAsync(string mbid, string bioLang = LastFm.DefaultLanguageCode, diff --git a/src/IF.Lastfm.Core/Api/IUserApi.cs b/src/IF.Lastfm.Core/Api/IUserApi.cs index 26addec2..60d14bce 100644 --- a/src/IF.Lastfm.Core/Api/IUserApi.cs +++ b/src/IF.Lastfm.Core/Api/IUserApi.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Threading.Tasks; using IF.Lastfm.Core.Api.Enums; using IF.Lastfm.Core.Api.Helpers; @@ -24,10 +24,16 @@ Task> GetTopArtists(string username, int pagenumber = 0, int count = LastFm.DefaultPageLength); + Task> GetTopTags(string username, + int count = LastFm.DefaultPageLength); + Task> GetRecentScrobbles(string username, DateTimeOffset? from = null, DateTimeOffset? to = null, bool extendedResponse = false, int pagenumber = LastFm.DefaultPage, int count = LastFm.DefaultPageLength); + Task> GetTopTracks(string username, LastStatsTimeSpan period = LastStatsTimeSpan.Week, int pagenumber = LastFm.DefaultPage, + int count = LastFm.DefaultPageLength); + Task> GetRecentStations(string username, int pagenumber, int count = LastFm.DefaultPageLength); @@ -50,4 +56,4 @@ Task> GetShoutsAsync(string username, Task> GetWeeklyAlbumChartAsync(string username, double? to = null, double? from = null); } -} \ No newline at end of file +} diff --git a/src/IF.Lastfm.Core/Api/UserApi.cs b/src/IF.Lastfm.Core/Api/UserApi.cs index 9b2a3f10..ed42d068 100644 --- a/src/IF.Lastfm.Core/Api/UserApi.cs +++ b/src/IF.Lastfm.Core/Api/UserApi.cs @@ -1,4 +1,4 @@ -using IF.Lastfm.Core.Api.Enums; +using IF.Lastfm.Core.Api.Enums; using IF.Lastfm.Core.Api.Helpers; using IF.Lastfm.Core.Objects; using System; @@ -54,6 +54,19 @@ public async Task> GetTopArtists(string username, LastS return await command.ExecuteAsync(); } + public async Task> GetTopTracks(string username, LastStatsTimeSpan period = LastStatsTimeSpan.Week, int pageNumber = LastFm.DefaultPage, int count = LastFm.DefaultPageLength) + { + var command = new GetTopTracksCommand(Auth, username) + { + Page = pageNumber, + Count = count, + Period = period, + HttpClient = HttpClient + }; + + return await command.ExecuteAsync(); + } + /// /// Gets a list of recent scrobbled tracks for this user in reverse date order. /// @@ -181,4 +194,4 @@ public async Task> GetWeeklyAlbumChartAsync(string usern return await command.ExecuteAsync(); } } -} \ No newline at end of file +} diff --git a/src/IF.Lastfm.Core/Objects/LastStats.cs b/src/IF.Lastfm.Core/Objects/LastStats.cs index 0aaae737..5380f5b7 100644 --- a/src/IF.Lastfm.Core/Objects/LastStats.cs +++ b/src/IF.Lastfm.Core/Objects/LastStats.cs @@ -1,25 +1,27 @@ -using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Linq; namespace IF.Lastfm.Core.Objects { - public class LastStats : ILastfmObject - { - #region Properties + public class LastStats : ILastfmObject + { + #region Properties - public int Listeners { get; set; } - public int Plays { get; set; } + public int Listeners { get; set; } + public int Plays { get; set; } + public int? UserPlayCount { get; set; } - #endregion + #endregion - internal static LastStats ParseJToken(JToken token) - { - var stats = new LastStats - { - Listeners = token.Value("listeners"), - Plays = token.Value("plays") - }; + internal static LastStats ParseJToken(JToken token) + { + var stats = new LastStats + { + Listeners = token.Value("listeners"), + Plays = token.Value("playcount"), + UserPlayCount = token.Value("userplaycount") + }; - return stats; - } - } + return stats; + } + } }