diff --git a/IF.Lastfm.Core/Api/Commands/TrackApi/GetShoutsCommand.cs b/IF.Lastfm.Core/Api/Commands/TrackApi/GetShoutsCommand.cs new file mode 100644 index 0000000..cadda9e --- /dev/null +++ b/IF.Lastfm.Core/Api/Commands/TrackApi/GetShoutsCommand.cs @@ -0,0 +1,76 @@ +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.TrackApi +{ + internal class GetShoutsCommand : GetAsyncCommandBase> + { + public string TrackName { get; set; } + public string ArtistName { get; set; } + public bool Autocorrect { get; set; } + + public GetShoutsCommand(IAuth auth, string trackname, string artistname) : base(auth) + { + Method = "track.getShouts"; + TrackName = trackname; + ArtistName = artistname; + } + + public async override Task> ExecuteAsync() + { + var parameters = new Dictionary + { + {"track", TrackName}, + {"artist", ArtistName}, + {"autocorrect", Convert.ToInt32(Autocorrect).ToString()} + }; + + base.AddPagingParameters(parameters); + + var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); + Url = new Uri(apiUrl, UriKind.Absolute); + + return await ExecuteInternal(); + } + + 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"); + + var shoutsToken = jtoken.SelectToken("shout"); + + var shouts = new List(); + foreach (var shout in shoutsToken.Children()) + { + var s = Shout.ParseJToken(shout); + shouts.Add(s); + } + + var pageresponse = PageResponse.CreateSuccessResponse(shouts); + + var attrToken = jtoken.SelectToken("@attr"); + pageresponse.AddPageInfoFromJToken(attrToken); + + return pageresponse; + } + else + { + return PageResponse.CreateErrorResponse(error); + } + } + } +} diff --git a/IF.Lastfm.Core/Api/ITrackApi.cs b/IF.Lastfm.Core/Api/ITrackApi.cs index 0c9a22c..444cbaa 100644 --- a/IF.Lastfm.Core/Api/ITrackApi.cs +++ b/IF.Lastfm.Core/Api/ITrackApi.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using IF.Lastfm.Core.Api.Helpers; -using xBrainLab.Security.Cryptography; +using IF.Lastfm.Core.Objects; namespace IF.Lastfm.Core.Api { @@ -11,5 +11,13 @@ public interface ITrackApi Task ScrobbleAsync(Scrobble scrobble); Task ScrobbleAsync(IEnumerable scrobble); + + Task> GetShoutsForTrackAsync(string trackname, string artistname, + int page = 0, + int count = LastFm.DefaultPageLength); + + Task> GetShoutsForTrackWithMbidAsync(string mbid, + int page = 0, + int count = LastFm.DefaultPageLength); } } \ No newline at end of file diff --git a/IF.Lastfm.Core/Api/TrackApi.cs b/IF.Lastfm.Core/Api/TrackApi.cs index 41d229e..b53f35a 100644 --- a/IF.Lastfm.Core/Api/TrackApi.cs +++ b/IF.Lastfm.Core/Api/TrackApi.cs @@ -2,10 +2,10 @@ using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; +using IF.Lastfm.Core.Api.Commands.TrackApi; using IF.Lastfm.Core.Api.Enums; using IF.Lastfm.Core.Api.Helpers; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; +using IF.Lastfm.Core.Objects; namespace IF.Lastfm.Core.Api { @@ -59,5 +59,20 @@ public Task ScrobbleAsync(IEnumerable scrobble) { throw new NotImplementedException(); } + + public async Task> GetShoutsForTrackAsync(string trackname, string artistname, int page = 0, int count = LastFm.DefaultPageLength) + { + var command = new GetShoutsCommand(Auth, trackname, artistname) + { + Page = page, + Count = count, + }; + return await command.ExecuteAsync(); + } + + public Task> GetShoutsForTrackWithMbidAsync(string mbid, int page = 0, int count = LastFm.DefaultPageLength) + { + throw new NotImplementedException(); + } } } \ 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 87df63d..206dfb1 100644 --- a/IF.Lastfm.Core/IF.Lastfm.Core.csproj +++ b/IF.Lastfm.Core/IF.Lastfm.Core.csproj @@ -44,6 +44,7 @@ + @@ -109,9 +110,7 @@ ..\lib\xBrainLab.Security.Cryptography.dll - - - + diff --git a/IF.Lastfm.Core/Objects/Shout.cs b/IF.Lastfm.Core/Objects/Shout.cs index 4068587..e5873c7 100644 --- a/IF.Lastfm.Core/Objects/Shout.cs +++ b/IF.Lastfm.Core/Objects/Shout.cs @@ -1,6 +1,40 @@ -namespace IF.Lastfm.Core.Objects +using System; +using System.Globalization; +using Newtonsoft.Json.Linq; + +namespace IF.Lastfm.Core.Objects { public class Shout { + #region Properties + + public string Body { get; set; } + public string Author { get; set; } + public DateTime TimePosted { get; set; } + + #endregion + + public static Shout ParseJToken(JToken token) + { + var s = new Shout(); + + s.Body = token.Value("body"); + s.Author = token.Value("author"); + + + var provider = CultureInfo.InvariantCulture; + + var date = token.Value("date"); + DateTime time; + // Tue, 18 Jun 2013 17:39:50 + var success = DateTime.TryParseExact(date, "ddd, dd MMM yyyy HH:mm:ss", provider, DateTimeStyles.AssumeUniversal, out time); + + if (success) + { + s.TimePosted = time; + } + + return s; + } } } \ No newline at end of file