diff --git a/src/IF.Lastfm.Core/Api/Commands/Tag/GetTopAlbumsCommand.cs b/src/IF.Lastfm.Core/Api/Commands/Tag/GetTopAlbumsCommand.cs new file mode 100644 index 0000000..e174e1a --- /dev/null +++ b/src/IF.Lastfm.Core/Api/Commands/Tag/GetTopAlbumsCommand.cs @@ -0,0 +1,51 @@ +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.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.Tag +{ + [ApiMethodName("tag.getTopAlbums")] + internal class GetTopAlbumsCommand: GetAsyncCommandBase> + { + public string TagName { get; set; } + + public GetTopAlbumsCommand(ILastAuth auth, string tagName) : base(auth) + { + TagName = tagName; + } + + public override void SetParameters() + { + Parameters.Add("tag", TagName); + AddPagingParameters(); + DisableCaching(); + } + + public async override 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 resultsToken = jtoken.SelectToken("topalbums"); + var itemsToken = resultsToken.SelectToken("album"); + + return PageResponse.CreateSuccessResponse(itemsToken, resultsToken, LastAlbum.ParseJToken, LastPageResultsType.Attr); + } + else + { + return LastResponse.CreateErrorResponse>(status); + } + } + } +} diff --git a/src/IF.Lastfm.Core/Api/ITagApi.cs b/src/IF.Lastfm.Core/Api/ITagApi.cs index 35278ae..8ce94d6 100644 --- a/src/IF.Lastfm.Core/Api/ITagApi.cs +++ b/src/IF.Lastfm.Core/Api/ITagApi.cs @@ -8,5 +8,6 @@ public interface ITagApi { Task> GetSimilarAsync(string tagName); Task> GetInfoAsync(string tagName); + Task> GetTopAlbumsAsync(string tagName,int page,int itemsPerPage); } } \ No newline at end of file diff --git a/src/IF.Lastfm.Core/Api/TagApi.cs b/src/IF.Lastfm.Core/Api/TagApi.cs index 0273b73..df9a1eb 100644 --- a/src/IF.Lastfm.Core/Api/TagApi.cs +++ b/src/IF.Lastfm.Core/Api/TagApi.cs @@ -43,6 +43,20 @@ public Task> GetInfoAsync(string tagName) return command.ExecuteAsync(); } + /// + /// Get the top albums tagged by this tag, ordered by tag count. + /// + public Task> GetTopAlbumsAsync(string tagName, int page=1, int itemsPerPage= LastFm.DefaultPageLength) + { + var command = new GetTopAlbumsCommand(Auth, tagName) + { + Page = page, + Count = itemsPerPage + }; + + return command.ExecuteAsync(); + } + /// /// Fetches the top global tags on Last.fm, sorted by popularity (number of times used). /// diff --git a/src/IF.Lastfm.Core/IF.Lastfm.Core.csproj b/src/IF.Lastfm.Core/IF.Lastfm.Core.csproj index b44daa8..7460a2a 100644 --- a/src/IF.Lastfm.Core/IF.Lastfm.Core.csproj +++ b/src/IF.Lastfm.Core/IF.Lastfm.Core.csproj @@ -65,6 +65,7 @@ +