From 65448d6032dd90a177a5fb25bf2bc87969c85683 Mon Sep 17 00:00:00 2001 From: Rikki Tooley Date: Sun, 26 Apr 2015 19:36:17 +0100 Subject: [PATCH] Scrobbler.SendCachedScrobblesAsync() --- src/IF.Lastfm.Core/Scrobblers/IScrobbler.cs | 2 ++ .../Scrobblers/ScrobblerBase.cs | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/IF.Lastfm.Core/Scrobblers/IScrobbler.cs b/src/IF.Lastfm.Core/Scrobblers/IScrobbler.cs index d07a90b..5c18fcd 100644 --- a/src/IF.Lastfm.Core/Scrobblers/IScrobbler.cs +++ b/src/IF.Lastfm.Core/Scrobblers/IScrobbler.cs @@ -15,5 +15,7 @@ public interface IScrobbler Task ScrobbleAsync(Scrobble scrobble); Task ScrobbleAsync(IEnumerable scrobbles); + + Task SendCachedScrobblesAsync(); } } diff --git a/src/IF.Lastfm.Core/Scrobblers/ScrobblerBase.cs b/src/IF.Lastfm.Core/Scrobblers/ScrobblerBase.cs index 45c6f4e..a6fbaef 100644 --- a/src/IF.Lastfm.Core/Scrobblers/ScrobblerBase.cs +++ b/src/IF.Lastfm.Core/Scrobblers/ScrobblerBase.cs @@ -1,7 +1,6 @@ using IF.Lastfm.Core.Api; using IF.Lastfm.Core.Api.Commands.Track; using IF.Lastfm.Core.Api.Enums; -using IF.Lastfm.Core.Api.Helpers; using IF.Lastfm.Core.Helpers; using System; using System.Collections.Generic; @@ -26,13 +25,23 @@ protected ScrobblerBase(ILastAuth auth, HttpClient httpClient = null) : base(htt MaxBatchSize = 50; } - + public Task ScrobbleAsync(Scrobble scrobble) { return ScrobbleAsync(new[] {scrobble}); } - public async Task ScrobbleAsync(IEnumerable scrobbles) + public Task ScrobbleAsync(IEnumerable scrobbles) + { + return ScrobbleAsyncInternal(scrobbles); + } + + public Task SendCachedScrobblesAsync() + { + return ScrobbleAsyncInternal(Enumerable.Empty()); + } + + public async Task ScrobbleAsyncInternal(IEnumerable scrobbles) { var scrobblesList = scrobbles.ToList(); var cached = await GetCachedAsync(); @@ -43,7 +52,7 @@ public async Task ScrobbleAsync(IEnumerable scrobble } var batches = pending.Batch(MaxBatchSize); - var responses = new List(pending.Count / MaxBatchSize); + var responses = new List(pending.Count % MaxBatchSize); var responseExceptions = new List(); foreach(var batch in batches) { @@ -65,7 +74,7 @@ public async Task ScrobbleAsync(IEnumerable scrobble } ScrobbleResponse scrobblerResponse; - if (responses.All(r => r.Success)) + if (!responses.Any() || responses.All(r => r.Success)) { scrobblerResponse = new ScrobbleResponse(LastResponseStatus.Successful); }