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

                var foundMovies = await traktService.MovieSearch(list);

                var existingMovies = await traktService.GetMovies(list);

                var moviesToRemove = new List <ITraktMovie>();
                foreach (var existingMovie in existingMovies)
                {
                    if (!foundMovies.Contains(existingMovie, new TraktMovieComparer()))
                    {
                        moviesToRemove.Add(existingMovie);
                    }
                }

                var moviesToAdd = new List <ITraktMovie>();
                foreach (var foundMovie in foundMovies)
                {
                    if (!existingMovies.Contains(foundMovie, new TraktMovieComparer()))
                    {
                        moviesToAdd.Add(foundMovie);
                    }
                }

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

                if (moviesToRemove.Any())
                {
                    //Chunking to 100 items per list cause trakt api does not like 10000s of items
                    foreach (var moviesToRemoveChunk in moviesToRemove.ChunkBy(100))
                    {
                        await traktService.RemoveMovies(moviesToRemoveChunk, 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.MovieSearch(traktList);

                var existing = await _traktService.GetMovies(traktList);

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

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

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

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

                await _traktService.Update(traktList);
            }
        }
Exemple #3
0
        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.MovieSearch(traktList);

                var existing = await _traktService.GetMovies(traktList);

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

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

                if (remove.Any())
                {
                    foreach (var toRemoveChunk in remove.ChunkBy(_traktApiConfiguration.ChunkBy))
                    {
                        await _traktService.RemoveMovies(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());
            }
        }
        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.MovieSearch(traktList);

                    var existing = await _traktService.GetMovies(traktList);

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

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

                    if (remove.Any())
                    {
                        foreach (var toRemoveChunk in remove.ChunkBy(_traktApiConfiguration.ChunkBy).WithProgress(removeProgressBar))
                        {
                            await _traktService.RemoveMovies(toRemoveChunk, traktList);
                        }
                    }
                }
                else
                {
                    var add   = new List <ITraktMovie>();
                    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 cleanMovieName      = processedLine.Groups[1].Value.Trim();
                            var itemYearParseResult = int.TryParse(processedLine.Groups[2].Value, out var movieYear);
                            if (itemYearParseResult)
                            {
                                var itemResult = await _traktService.MovieSearch(traktList, cleanMovieName, movieYear);

                                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.AddMovies(toAddChunk, traktList);
                        }
                    }
                }
            }
            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 || ex is RefreshTokenBadRequestException ||
                         ex.Message.Contains("Response status code was 423"))
                {
                    traktList = await _traktRepository.Get(param);

                    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);
                    }

                    traktList.LastProcessed = DateTime.Now;

                    await _traktRepository.Update(traktList);
                }
            }

            if (queueNext)
            {
                BackgroundJob.Schedule <ProcessUserListsRecurringJob>(x => x.Execute(null), TimeSpan.FromSeconds(_traktApiConfiguration.DelayRequeue));
            }
        }