public async Task Execute(uint param)
        {
            try
            {
                var list = await traktService.Get(param, true);

                var found = await traktService.ShowSearch(list);

                var existing = await traktService.GetShows(list);

                var toRemove = new List <ITraktShow>();
                foreach (var existingShow in existing)
                {
                    if (!found.Contains(existingShow, new TraktShowComparer()))
                    {
                        toRemove.Add(existingShow);
                    }
                }

                var toAdd = new List <ITraktShow>();
                foreach (var foundShow in found)
                {
                    if (!existing.Contains(foundShow, new TraktShowComparer()))
                    {
                        toAdd.Add(foundShow);
                    }
                }

                if (toAdd.Any())
                {
                    //Chunking to 100 items per list cause trakt api does not like 10000s of items
                    foreach (var toAddChunk in toAdd.ChunkBy(100))
                    {
                        await traktService.AddShows(toAddChunk, list);
                    }
                }

                if (toRemove.Any())
                {
                    //Chunking to 100 items per list cause trakt api does not like 10000s of items
                    foreach (var toRemoveChunk in toRemove.ChunkBy(100))
                    {
                        await traktService.RemoveShows(toRemoveChunk, list);
                    }
                }

                list = await traktService.Get(list.Id, true);

                list.LastProcessed = DateTime.Now;

                await traktService.Update(list);
            }
            catch (TraktListNotFoundException)
            {
                await traktService.Delete(await traktService.Get(param));
            }
        }
        public async Task Execute(uint param)
        {
            try
            {
                traktList = await _traktService.Get(param, true);

                traktList.ScanState = ScanState.Updating;

                await _traktService.Update(traktList);

                var found = await _traktService.ShowSearch(traktList);

                var existing = await _traktService.GetShows(traktList);

                var remove = existing.Except(found, new TraktShowComparer()).ToList();
                var add    = found.Except(existing, new TraktShowComparer()).ToList();

                if (add.Any())
                {
                    foreach (var toAddChunk in add.ChunkBy(500))
                    {
                        await _traktService.AddShows(toAddChunk, traktList);
                    }
                }

                if (remove.Any())
                {
                    foreach (var toRemoveChunk in remove.ChunkBy(500))
                    {
                        await _traktService.RemoveShows(toRemoveChunk, traktList);
                    }
                }

                traktList.LastProcessed = DateTime.Now;
            }
            catch (TraktListNotFoundException)
            {
                await _traktService.Delete(new TraktList { Id = param });
            }
            finally
            {
                traktList.ScanState = ScanState.None;

                await _traktService.Update(traktList);
            }
        }
Пример #3
0
        public async Task Execute(uint param, PerformContext context, bool queueNext = false, bool forceRefresh = false)
        {
            var addProgressBar    = context.WriteProgressBar();
            var removeProgressBar = context.WriteProgressBar();

            try
            {
                traktList = await _traktRepository.Get(param);

                traktList = await _traktService.Get(traktList);

                traktList.ScanState = ScanState.Updating;

                await _traktRepository.Update(traktList);

                if (string.IsNullOrWhiteSpace(traktList.ItemList))
                {
                    var found = await _traktService.ShowSearch(traktList);

                    var existing = await _traktService.GetShows(traktList);

                    var remove = existing.Except(found, new TraktShowComparer()).ToList();
                    var add    = found.Except(existing, new TraktShowComparer()).ToList();

                    if (add.Any())
                    {
                        foreach (var toAddChunk in add.ChunkBy(_traktApiConfiguration.ChunkBy).WithProgress(addProgressBar))
                        {
                            await _traktService.AddShows(toAddChunk, traktList);
                        }
                    }

                    if (remove.Any())
                    {
                        foreach (var toRemoveChunk in remove.ChunkBy(_traktApiConfiguration.ChunkBy).WithProgress(removeProgressBar))
                        {
                            await _traktService.RemoveShows(toRemoveChunk, traktList);
                        }
                    }
                }
                else
                {
                    var add   = new List <ITraktShow>();
                    var regex = new Regex(@"(.*)\(([0-9]{4})\)");

                    foreach (var line in traktList.ItemList.Split("\r\n"))
                    {
                        var processedLine = regex.Match(line);
                        if (processedLine.Success && processedLine.Groups.Count == 3)
                        {
                            var cleanShowName       = processedLine.Groups[1].Value.Trim();
                            var itemYearParseResult = int.TryParse(processedLine.Groups[2].Value, out var showYear);
                            if (itemYearParseResult)
                            {
                                var itemResult = await _traktService.ShowSearch(traktList, cleanShowName, showYear);

                                if (itemResult != null)
                                {
                                    add.Add(itemResult);
                                }

                                await Task.Delay(_traktApiConfiguration.DelayIdSearch);
                            }
                        }
                    }

                    if (add.Any())
                    {
                        foreach (var toAddChunk in add.ChunkBy(_traktApiConfiguration.ChunkBy).WithProgress(addProgressBar))
                        {
                            await _traktService.AddShows(toAddChunk, traktList);
                        }
                    }
                }

                traktList.LastProcessed = DateTime.Now;
            }
            catch (Exception ex)
            {
                if (ex is TraktListNotFoundException)
                {
                    if (traktList != null)
                    {
                        await _traktRepository.Delete(traktList);

                        traktList = null;
                    }
                    else
                    {
                        await _traktRepository.Delete(new TraktList { Id = param });
                    }
                }
                else if (ex is TraktAuthenticationOAuthException || ex is TraktAuthorizationException)
                {
                    traktList = await _traktRepository.Get(param);

                    traktList.LastProcessed = DateTime.Now;
                    traktList.Process       = false;
                }
                else if (ex is ArgumentOutOfRangeException)
                {
                }
                else
                {
                    throw ex;
                }
            }
            finally
            {
                if (traktList != null)
                {
                    traktList.ScanState = ScanState.None;

                    if (forceRefresh)
                    {
                        await _traktService.Update(traktList);
                    }

                    await _traktRepository.Update(traktList);
                }
            }

            if (queueNext)
            {
                BackgroundJob.Schedule <ProcessUserListsRecurringJob>(x => x.Execute(), TimeSpan.FromSeconds(_traktApiConfiguration.DelayRequeue));
            }
        }
        public async Task Execute(uint param, bool queueNext = false, bool forceRefresh = false)
        {
            try
            {
                traktList = await _traktRepository.Get(param);

                traktList = await _traktService.Get(traktList);

                traktList.ScanState = ScanState.Updating;

                await _traktRepository.Update(traktList);

                var found = await _traktService.ShowSearch(traktList);

                var existing = await _traktService.GetShows(traktList);

                var remove = existing.Except(found, new TraktShowComparer()).ToList();
                var add    = found.Except(existing, new TraktShowComparer()).ToList();

                if (add.Any())
                {
                    foreach (var toAddChunk in add.ChunkBy(_traktApiConfiguration.ChunkBy))
                    {
                        await _traktService.AddShows(toAddChunk, traktList);
                    }
                }

                if (remove.Any())
                {
                    foreach (var toRemoveChunk in remove.ChunkBy(_traktApiConfiguration.ChunkBy))
                    {
                        await _traktService.RemoveShows(toRemoveChunk, traktList);
                    }
                }

                traktList.LastProcessed = DateTime.Now;
            }
            catch (Exception ex)
            {
                if (ex is TraktListNotFoundException)
                {
                    if (traktList != null)
                    {
                        await _traktRepository.Delete(traktList);

                        traktList = null;
                    }
                    else
                    {
                        await _traktRepository.Delete(new TraktList { Id = param });
                    }
                }
                else if (ex is TraktAuthenticationOAuthException || ex is TraktAuthorizationException)
                {
                    traktList = await _traktRepository.Get(param);

                    traktList.LastProcessed = DateTime.Now;
                    traktList.Process       = false;
                }
                else
                {
                    throw ex;
                }
            }
            finally
            {
                if (traktList != null)
                {
                    traktList.ScanState = ScanState.None;

                    if (forceRefresh)
                    {
                        await _traktService.Update(traktList);
                    }

                    await _traktRepository.Update(traktList);
                }
            }

            if (queueNext)
            {
                BackgroundJob.Enqueue <ProcessUserListsRecurringJob>(x => x.Execute());
            }
        }