GetShoutsCommand

This commit is contained in:
Rikki Tooley 2013-07-07 16:16:54 +01:00
parent 6c3e6d25ac
commit 1e18e999bf
5 changed files with 139 additions and 7 deletions

View File

@ -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<PageResponse<Shout>>
{
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<PageResponse<Shout>> ExecuteAsync()
{
var parameters = new Dictionary<string, string>
{
{"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<PageResponse<Shout>> HandleResponse(HttpResponseMessage response)
{
string json = await response.Content.ReadAsStringAsync();
LastFmApiError error;
if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode)
{
JToken jtoken = JsonConvert.DeserializeObject<JToken>(json).SelectToken("shouts");
var shoutsToken = jtoken.SelectToken("shout");
var shouts = new List<Shout>();
foreach (var shout in shoutsToken.Children())
{
var s = Shout.ParseJToken(shout);
shouts.Add(s);
}
var pageresponse = PageResponse<Shout>.CreateSuccessResponse(shouts);
var attrToken = jtoken.SelectToken("@attr");
pageresponse.AddPageInfoFromJToken(attrToken);
return pageresponse;
}
else
{
return PageResponse<Shout>.CreateErrorResponse(error);
}
}
}
}

View File

@ -1,7 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using IF.Lastfm.Core.Api.Helpers; using IF.Lastfm.Core.Api.Helpers;
using xBrainLab.Security.Cryptography; using IF.Lastfm.Core.Objects;
namespace IF.Lastfm.Core.Api namespace IF.Lastfm.Core.Api
{ {
@ -11,5 +11,13 @@ public interface ITrackApi
Task<LastResponse> ScrobbleAsync(Scrobble scrobble); Task<LastResponse> ScrobbleAsync(Scrobble scrobble);
Task<LastResponse> ScrobbleAsync(IEnumerable<Scrobble> scrobble); Task<LastResponse> ScrobbleAsync(IEnumerable<Scrobble> scrobble);
Task<PageResponse<Shout>> GetShoutsForTrackAsync(string trackname, string artistname,
int page = 0,
int count = LastFm.DefaultPageLength);
Task<PageResponse<Shout>> GetShoutsForTrackWithMbidAsync(string mbid,
int page = 0,
int count = LastFm.DefaultPageLength);
} }
} }

View File

@ -2,10 +2,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using IF.Lastfm.Core.Api.Commands.TrackApi;
using IF.Lastfm.Core.Api.Enums; using IF.Lastfm.Core.Api.Enums;
using IF.Lastfm.Core.Api.Helpers; using IF.Lastfm.Core.Api.Helpers;
using Newtonsoft.Json; using IF.Lastfm.Core.Objects;
using Newtonsoft.Json.Linq;
namespace IF.Lastfm.Core.Api namespace IF.Lastfm.Core.Api
{ {
@ -59,5 +59,20 @@ public Task<LastResponse> ScrobbleAsync(IEnumerable<Scrobble> scrobble)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public async Task<PageResponse<Shout>> 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<PageResponse<Shout>> GetShoutsForTrackWithMbidAsync(string mbid, int page = 0, int count = LastFm.DefaultPageLength)
{
throw new NotImplementedException();
}
} }
} }

View File

@ -44,6 +44,7 @@
<Compile Include="Api\Commands\AlbumApi\GetAbumInfoCommand.cs" /> <Compile Include="Api\Commands\AlbumApi\GetAbumInfoCommand.cs" />
<Compile Include="Api\Commands\ArtistApi\GetArtistInfoCommand.cs" /> <Compile Include="Api\Commands\ArtistApi\GetArtistInfoCommand.cs" />
<Compile Include="Api\Commands\GetAsyncCommandBase.cs" /> <Compile Include="Api\Commands\GetAsyncCommandBase.cs" />
<Compile Include="Api\Commands\TrackApi\GetShoutsCommand.cs" />
<Compile Include="Api\Commands\UserApi\GetRecentScrobblesCommand.cs" /> <Compile Include="Api\Commands\UserApi\GetRecentScrobblesCommand.cs" />
<Compile Include="Api\Commands\UserApi\GetTopAlbumsCommand.cs" /> <Compile Include="Api\Commands\UserApi\GetTopAlbumsCommand.cs" />
<Compile Include="Api\Helpers\ApiExtensions.cs" /> <Compile Include="Api\Helpers\ApiExtensions.cs" />
@ -109,9 +110,7 @@
<HintPath>..\lib\xBrainLab.Security.Cryptography.dll</HintPath> <HintPath>..\lib\xBrainLab.Security.Cryptography.dll</HintPath>
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup />
<Folder Include="Api\Commands\TrackApi\" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" /> <Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" /> <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.7\tools\Microsoft.Bcl.Build.targets" /> <Import Project="..\packages\Microsoft.Bcl.Build.1.0.7\tools\Microsoft.Bcl.Build.targets" />

View File

@ -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 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<string>("body");
s.Author = token.Value<string>("author");
var provider = CultureInfo.InvariantCulture;
var date = token.Value<string>("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;
}
} }
} }