mirror of
https://github.com/Sarsoo/IF.Lastfm.git
synced 2024-10-17 07:13:09 +01:00
GetShoutsCommand
This commit is contained in:
parent
6c3e6d25ac
commit
1e18e999bf
76
IF.Lastfm.Core/Api/Commands/TrackApi/GetShoutsCommand.cs
Normal file
76
IF.Lastfm.Core/Api/Commands/TrackApi/GetShoutsCommand.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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<LastResponse> ScrobbleAsync(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);
|
||||
}
|
||||
}
|
@ -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<LastResponse> ScrobbleAsync(IEnumerable<Scrobble> scrobble)
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
@ -44,6 +44,7 @@
|
||||
<Compile Include="Api\Commands\AlbumApi\GetAbumInfoCommand.cs" />
|
||||
<Compile Include="Api\Commands\ArtistApi\GetArtistInfoCommand.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\GetTopAlbumsCommand.cs" />
|
||||
<Compile Include="Api\Helpers\ApiExtensions.cs" />
|
||||
@ -109,9 +110,7 @@
|
||||
<HintPath>..\lib\xBrainLab.Security.Cryptography.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Api\Commands\TrackApi\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.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" />
|
||||
|
@ -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<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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user