Beispiel #1
0
        public static Task<ScrobbleResponse> CreateSuccessResponse(string json)
        {
            var root = JObject.Parse(json);
            var scrobblesToken = root["scrobbles"]["scrobble"];
            var allItems = PageResponse<Scrobble>.ParseItemsToken(scrobblesToken, Scrobble.ParseJToken).ToList();
            var ignored = allItems.Where(s => !String.IsNullOrEmpty(s.IgnoredReason)).ToList();

            var acceptedCount = allItems.Count - ignored.Count;

            var response = new ScrobbleResponse(LastResponseStatus.Successful)
            {
                AcceptedCount = acceptedCount,
                Ignored = ignored
            };

            return Task.FromResult(response);
        }
Beispiel #2
0
        public static Task <ScrobbleResponse> CreateSuccessResponse(string json)
        {
            var root           = JObject.Parse(json);
            var scrobblesToken = root["scrobbles"]["scrobble"];
            var allItems       = PageResponse <Scrobble> .ParseItemsToken(scrobblesToken, Scrobble.ParseJToken).ToList();

            var ignored = allItems.Where(s => !String.IsNullOrEmpty(s.IgnoredReason)).ToList();

            var acceptedCount = allItems.Count - ignored.Count;

            var response = new ScrobbleResponse(LastResponseStatus.Successful)
            {
                AcceptedCount = acceptedCount,
                Ignored       = ignored
            };

            return(Task.FromResult(response));
        }
Beispiel #3
0
        public async Task <ScrobbleResponse> ScrobbleAsyncInternal(IEnumerable <Scrobble> scrobbles)
        {
            var scrobblesList = scrobbles.ToList();
            var cached        = await GetCachedAsync();

            var pending = scrobblesList.Concat(cached).OrderBy(s => s.TimePlayed).ToList();

            if (!pending.Any())
            {
                return(new ScrobbleResponse(LastResponseStatus.Successful));
            }

            var batches            = pending.Batch(MaxBatchSize);
            var responses          = new List <ScrobbleResponse>(pending.Count % MaxBatchSize);
            var responseExceptions = new List <Exception>();

            foreach (var batch in batches)
            {
                var command = new ScrobbleCommand(_auth, batch)
                {
                    HttpClient = HttpClient
                };

                try
                {
                    var response = await command.ExecuteAsync();

                    responses.Add(response);
                }
                catch (HttpRequestException httpEx)
                {
                    responseExceptions.Add(httpEx);
                }
            }

            ScrobbleResponse scrobblerResponse;

            if (!responses.Any() || responses.All(r => r.Success))
            {
                scrobblerResponse = new ScrobbleResponse(LastResponseStatus.Successful);
            }
            else
            {
                try
                {
                    var firstBadResponse       = responses.FirstOrDefault(r => !r.Success && r.Status != LastResponseStatus.Unknown);
                    var originalResponseStatus = firstBadResponse != null
                        ? firstBadResponse.Status
                        : LastResponseStatus.RequestFailed; // TODO check httpEx

                    var cacheStatus = await CacheAsync(scrobblesList, originalResponseStatus);

                    scrobblerResponse = new ScrobbleResponse(cacheStatus);
                }
                catch (Exception e)
                {
                    scrobblerResponse = new ScrobbleResponse(LastResponseStatus.CacheFailed)
                    {
                        Exception = e
                    };
                }
            }

            var ignoredScrobbles = responses.SelectMany(r => r.Ignored);

            scrobblerResponse.Ignored = ignoredScrobbles;

            AfterSend?.Invoke(this, scrobblerResponse);

            return(scrobblerResponse);
        }
Beispiel #4
0
        public async Task <ScrobbleResponse> ScrobbleAsyncInternal(IEnumerable <Scrobble> scrobbles)
        {
            var scrobblesList = new ReadOnlyCollection <Scrobble>(scrobbles.ToList());
            var cached        = await GetCachedAsync();

            var pending = scrobblesList.Concat(cached).OrderBy(s => s.TimePlayed).ToList();

            if (!pending.Any())
            {
                return(new ScrobbleResponse(LastResponseStatus.Successful));
            }

            var batches            = pending.Batch(MaxBatchSize);
            var responses          = new List <ScrobbleResponse>(pending.Count % MaxBatchSize);
            var responseExceptions = new List <Exception>();

            foreach (var batch in batches)
            {
                var command = new ScrobbleCommand(Auth, batch)
                {
                    HttpClient = HttpClient
                };

                try
                {
                    var response = await command.ExecuteAsync();

                    var acceptedMap = new HashSet <Scrobble>(pending);
                    foreach (var ignored in response.Ignored)
                    {
                        acceptedMap.Remove(ignored);
                    }

                    await RemoveFromCacheAsync(acceptedMap);

                    responses.Add(response);
                }
                catch (HttpRequestException httpEx)
                {
                    responseExceptions.Add(httpEx);
                }
            }

            ScrobbleResponse scrobblerResponse;

            if (!responses.Any() || responses.All(r => r.Success))
            {
                scrobblerResponse = new ScrobbleResponse(LastResponseStatus.Successful);
            }
            else
            {
                var firstBadResponse       = responses.FirstOrDefault(r => !r.Success && r.Status != LastResponseStatus.Unknown);
                var originalResponseStatus = firstBadResponse?.Status ?? LastResponseStatus.RequestFailed; // TODO check httpEx

                var cacheStatus = await CacheAsync(scrobblesList, originalResponseStatus);

                scrobblerResponse = new ScrobbleResponse(cacheStatus);
            }

            scrobblerResponse.Ignored       = responses.SelectMany(r => r.Ignored);
            scrobblerResponse.AcceptedCount = responses.Sum(r => r.AcceptedCount);

            AfterSend?.Invoke(this, scrobblerResponse);

            return(scrobblerResponse);
        }
Beispiel #5
0
        public async Task<ScrobbleResponse> ScrobbleAsyncInternal(IEnumerable<Scrobble> scrobbles)
        {
            var scrobblesList = scrobbles.ToList();
            var cached = await GetCachedAsync();
            var pending = scrobblesList.Concat(cached).OrderBy(s => s.TimePlayed).ToList();
            if (!pending.Any())
            {
                return new ScrobbleResponse(LastResponseStatus.Successful);
            }

            var batches = pending.Batch(MaxBatchSize);
            var responses = new List<ScrobbleResponse>(pending.Count % MaxBatchSize);
            var responseExceptions = new List<Exception>();
            foreach(var batch in batches)
            {
                var command = new ScrobbleCommand(_auth, batch)
                {
                    HttpClient = HttpClient
                };

                try
                {
                    var response = await command.ExecuteAsync();

                    responses.Add(response);
                }
                catch (HttpRequestException httpEx)
                {
                    responseExceptions.Add(httpEx);
                }
            }

            ScrobbleResponse scrobblerResponse;
            if (!responses.Any() || responses.All(r => r.Success))
            {
                scrobblerResponse = new ScrobbleResponse(LastResponseStatus.Successful);
            }
            else
            {
                try
                {
                    var firstBadResponse = responses.FirstOrDefault(r => !r.Success && r.Status != LastResponseStatus.Unknown);
                    var originalResponseStatus = firstBadResponse != null
                        ? firstBadResponse.Status
                        : LastResponseStatus.RequestFailed; // TODO check httpEx

                    var cacheStatus = await CacheAsync(scrobblesList, originalResponseStatus);

                    scrobblerResponse = new ScrobbleResponse(cacheStatus);
                }
                catch (Exception e)
                {
                    scrobblerResponse = new ScrobbleResponse(LastResponseStatus.CacheFailed)
                    {
                        Exception = e
                    };
                }
            }

            var ignoredScrobbles = responses.SelectMany(r => r.Ignored);
            scrobblerResponse.Ignored = ignoredScrobbles;

            return scrobblerResponse;
        }