diff --git a/src/IF.Lastfm.Core/Api/Commands/User/GetWeeklyArtistChartCommand.cs b/src/IF.Lastfm.Core/Api/Commands/User/GetWeeklyArtistChartCommand.cs new file mode 100644 index 0000000..c58c427 --- /dev/null +++ b/src/IF.Lastfm.Core/Api/Commands/User/GetWeeklyArtistChartCommand.cs @@ -0,0 +1,59 @@ +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.getWeeklyArtistChart")] + internal class GetWeeklyArtistChartCommand : GetAsyncCommandBase> + { + public string Username { get; set; } + public string To { get; set; } + public string From { get; set; } + + public GetWeeklyArtistChartCommand(ILastAuth auth, string username, string from, string to) + : base(auth) + { + Username = username; + From = from; + To = to; + } + + public override void SetParameters() + { + if(!From.Equals(null)) + { + Parameters.Add("from", From); + } + if(!To.Equals(null)) + { + Parameters.Add("to", To); + } + + Parameters.Add("user", Username); + 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); + var chartListToken = jtoken.SelectToken("weeklyartistchart"); + var itemsToken = chartListToken.SelectToken("artist"); + var pageInfoToken = chartListToken.SelectToken("@attr"); + + return PageResponse.CreateSuccessResponse(itemsToken, pageInfoToken, LastArtist.ParseJToken, LastPageResultsType.Attr); + } + + return LastResponse.CreateErrorResponse>(status); + } + } +} \ No newline at end of file diff --git a/src/IF.Lastfm.Core/Api/Commands/User/GetWeeklyChartsCommand.cs b/src/IF.Lastfm.Core/Api/Commands/User/GetWeeklyChartListCommand.cs similarity index 89% rename from src/IF.Lastfm.Core/Api/Commands/User/GetWeeklyChartsCommand.cs rename to src/IF.Lastfm.Core/Api/Commands/User/GetWeeklyChartListCommand.cs index 8686e8f..30a7cdf 100644 --- a/src/IF.Lastfm.Core/Api/Commands/User/GetWeeklyChartsCommand.cs +++ b/src/IF.Lastfm.Core/Api/Commands/User/GetWeeklyChartListCommand.cs @@ -9,11 +9,11 @@ namespace IF.Lastfm.Core.Api.Commands.User { [ApiMethodName("user.getWeeklyChartList")] - internal class GetWeeklyChartsCommand : GetAsyncCommandBase> + internal class GetWeeklyChartListCommand : GetAsyncCommandBase> { public string Username { get; set; } - public GetWeeklyChartsCommand(ILastAuth auth, string username) + public GetWeeklyChartListCommand(ILastAuth auth, string username) : base(auth) { Username = username; diff --git a/src/IF.Lastfm.Core/Api/IUserApi.cs b/src/IF.Lastfm.Core/Api/IUserApi.cs index b2936f1..ace2e68 100644 --- a/src/IF.Lastfm.Core/Api/IUserApi.cs +++ b/src/IF.Lastfm.Core/Api/IUserApi.cs @@ -41,5 +41,7 @@ Task> GetShoutsAsync(string username, Task> GetLovedTracks(string username, int pagenumber, int count); Task> GetWeeklyChartListAsync(string username); + + Task> GetWeeklyArtistChartAsync(string username, string to = null, string 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 f5985ab..78079df 100644 --- a/src/IF.Lastfm.Core/Api/UserApi.cs +++ b/src/IF.Lastfm.Core/Api/UserApi.cs @@ -134,11 +134,19 @@ public async Task> GetLovedTracks( } public async Task> GetWeeklyChartListAsync(string username) { - var command = new GetWeeklyChartsCommand(auth: Auth, username: username) + var command = new GetWeeklyChartListCommand(auth: Auth, username: username) { HttpClient = HttpClient }; return await command.ExecuteAsync(); - } + } + public async Task> GetWeeklyArtistChartAsync(string username, string from = null, string to = null) + { + var command = new GetWeeklyArtistChartCommand(auth: Auth, username: username, from: from, to: to) + { + HttpClient = HttpClient + }; + return await command.ExecuteAsync(); + } } } \ No newline at end of file