mirror of
https://github.com/Sarsoo/IF.Lastfm.git
synced 2024-10-16 23:13:07 +01:00
Scrobbler.SendCachedScrobblesAsync()
This commit is contained in:
parent
3e899833c2
commit
65448d6032
@ -15,5 +15,7 @@ public interface IScrobbler
|
|||||||
Task<ScrobbleResponse> ScrobbleAsync(Scrobble scrobble);
|
Task<ScrobbleResponse> ScrobbleAsync(Scrobble scrobble);
|
||||||
|
|
||||||
Task<ScrobbleResponse> ScrobbleAsync(IEnumerable<Scrobble> scrobbles);
|
Task<ScrobbleResponse> ScrobbleAsync(IEnumerable<Scrobble> scrobbles);
|
||||||
|
|
||||||
|
Task<ScrobbleResponse> SendCachedScrobblesAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
using IF.Lastfm.Core.Api;
|
using IF.Lastfm.Core.Api;
|
||||||
using IF.Lastfm.Core.Api.Commands.Track;
|
using IF.Lastfm.Core.Api.Commands.Track;
|
||||||
using IF.Lastfm.Core.Api.Enums;
|
using IF.Lastfm.Core.Api.Enums;
|
||||||
using IF.Lastfm.Core.Api.Helpers;
|
|
||||||
using IF.Lastfm.Core.Helpers;
|
using IF.Lastfm.Core.Helpers;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -26,13 +25,23 @@ protected ScrobblerBase(ILastAuth auth, HttpClient httpClient = null) : base(htt
|
|||||||
|
|
||||||
MaxBatchSize = 50;
|
MaxBatchSize = 50;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<ScrobbleResponse> ScrobbleAsync(Scrobble scrobble)
|
public Task<ScrobbleResponse> ScrobbleAsync(Scrobble scrobble)
|
||||||
{
|
{
|
||||||
return ScrobbleAsync(new[] {scrobble});
|
return ScrobbleAsync(new[] {scrobble});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ScrobbleResponse> ScrobbleAsync(IEnumerable<Scrobble> scrobbles)
|
public Task<ScrobbleResponse> ScrobbleAsync(IEnumerable<Scrobble> scrobbles)
|
||||||
|
{
|
||||||
|
return ScrobbleAsyncInternal(scrobbles);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<ScrobbleResponse> SendCachedScrobblesAsync()
|
||||||
|
{
|
||||||
|
return ScrobbleAsyncInternal(Enumerable.Empty<Scrobble>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ScrobbleResponse> ScrobbleAsyncInternal(IEnumerable<Scrobble> scrobbles)
|
||||||
{
|
{
|
||||||
var scrobblesList = scrobbles.ToList();
|
var scrobblesList = scrobbles.ToList();
|
||||||
var cached = await GetCachedAsync();
|
var cached = await GetCachedAsync();
|
||||||
@ -43,7 +52,7 @@ public async Task<ScrobbleResponse> ScrobbleAsync(IEnumerable<Scrobble> scrobble
|
|||||||
}
|
}
|
||||||
|
|
||||||
var batches = pending.Batch(MaxBatchSize);
|
var batches = pending.Batch(MaxBatchSize);
|
||||||
var responses = new List<ScrobbleResponse>(pending.Count / MaxBatchSize);
|
var responses = new List<ScrobbleResponse>(pending.Count % MaxBatchSize);
|
||||||
var responseExceptions = new List<Exception>();
|
var responseExceptions = new List<Exception>();
|
||||||
foreach(var batch in batches)
|
foreach(var batch in batches)
|
||||||
{
|
{
|
||||||
@ -65,7 +74,7 @@ public async Task<ScrobbleResponse> ScrobbleAsync(IEnumerable<Scrobble> scrobble
|
|||||||
}
|
}
|
||||||
|
|
||||||
ScrobbleResponse scrobblerResponse;
|
ScrobbleResponse scrobblerResponse;
|
||||||
if (responses.All(r => r.Success))
|
if (!responses.Any() || responses.All(r => r.Success))
|
||||||
{
|
{
|
||||||
scrobblerResponse = new ScrobbleResponse(LastResponseStatus.Successful);
|
scrobblerResponse = new ScrobbleResponse(LastResponseStatus.Successful);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user