Beispiel #1
0
        public async Task <IActionResult> Delete(DeleteListViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var list = await _traktRepository.Get(model.Id);

            if (list == null)
            {
                return(RedirectToAction(nameof(My)));
            }

            if (list.Owner.UserName == User.Identity.Name)
            {
                try
                {
                    await _traktService.Delete(list);
                }
                catch (TraktListNotFoundException)
                {
                }

                await _traktRepository.Delete(list);
            }

            return(RedirectToAction(nameof(My)));
        }
Beispiel #2
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());
            }
        }
Beispiel #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));
            }
        }