public ImportListFetchResult FetchSingleList(ImportListDefinition definition)
        {
            var result = new ImportListFetchResult();

            var importList = _importListFactory.GetInstance(definition);

            if (importList == null || !definition.Enable)
            {
                _logger.Debug("Import list {0} is not enabled. No Movies will be added");
                return(result);
            }

            var importListLocal = importList;

            try
            {
                var importListReports = importListLocal.Fetch();

                lock (result)
                {
                    _logger.Debug("Found {0} from {1}", importListReports.Movies.Count, importList.Name);

                    if (!importListReports.AnyFailure)
                    {
                        // TODO some opportunity to bulk map here if we had the tmdbIds
                        var listMovies = importListReports.Movies.Select(x =>
                        {
                            return(MapMovieReport(x));
                        }).Where(x => x.TmdbId > 0).ToList();

                        listMovies = listMovies.DistinctBy(x => x.TmdbId).ToList();

                        result.Movies.AddRange(listMovies);
                        _listMovieService.SyncMoviesForList(listMovies, importList.Definition.Id);
                    }

                    result.AnyFailure |= importListReports.AnyFailure;
                }
            }
            catch (Exception e)
            {
                _logger.Error(e, "Error during Import List Sync for list {0}", importList.Name);
            }

            _logger.Debug("Found {0} reports for list {1}", result.Movies.Count, importList.Name);

            return(result);
        }
Exemplo n.º 2
0
        private void ProcessReports(ImportListFetchResult listFetchResult)
        {
            listFetchResult.Movies = listFetchResult.Movies.DistinctBy(x =>
            {
                if (x.TmdbId != 0)
                {
                    return(x.TmdbId.ToString());
                }

                if (x.ImdbId.IsNotNullOrWhiteSpace())
                {
                    return(x.ImdbId);
                }

                return(x.Title);
            }).ToList();

            var listedMovies = listFetchResult.Movies.ToList();

            var importExclusions = _exclusionService.GetAllExclusions();
            var dbMovies         = _movieService.AllMovieTmdbIds();
            var moviesToAdd      = new List <Movie>();

            var groupedMovies = listedMovies.GroupBy(x => x.ListId);

            foreach (var list in groupedMovies)
            {
                var importList = _importListFactory.Get(list.Key);

                foreach (var movie in list)
                {
                    if (movie.TmdbId != 0)
                    {
                        ProcessMovieReport(importList, movie, importExclusions, dbMovies, moviesToAdd);
                    }
                }
            }

            if (moviesToAdd.Any())
            {
                _logger.Info($"Adding {moviesToAdd.Count} movies from your auto enabled lists to library");
            }

            _addMovieService.AddMovies(moviesToAdd, true);
        }
        public ImportListFetchResult Fetch()
        {
            var result = new ImportListFetchResult();

            var importLists = _importListFactory.Enabled();

            if (!importLists.Any())
            {
                _logger.Debug("No available import lists. check your configuration.");
                return(result);
            }

            _logger.Debug("Available import lists {0}", importLists.Count);

            var taskList    = new List <Task>();
            var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None);

            foreach (var importList in importLists)
            {
                _logger.ProgressInfo("Syncing Movies for List: {0}", importList.Name);

                var importListLocal = importList;
                var blockedLists    = _importListStatusService.GetBlockedProviders().ToDictionary(v => v.ProviderId, v => v);

                if (blockedLists.TryGetValue(importList.Definition.Id, out ImportListStatus blockedListStatus))
                {
                    _logger.Debug("Temporarily ignoring list {0} till {1} due to recent failures.", importList.Definition.Name, blockedListStatus.DisabledTill.Value.ToLocalTime());
                    result.AnyFailure |= true; //Ensure we don't clean if a list is down
                    continue;
                }

                var task = taskFactory.StartNew(() =>
                {
                    try
                    {
                        var importListReports = importListLocal.Fetch();

                        lock (result)
                        {
                            _logger.Debug("Found {0} from {1}", importListReports.Movies.Count, importList.Name);

                            if (!importListReports.AnyFailure)
                            {
                                // TODO some opportunity to bulk map here if we had the tmdbIds
                                var listMovies = importListReports.Movies.Select(x =>
                                {
                                    // Is it existing in result
                                    var movie = result.Movies.FirstOrDefault(r => r.TmdbId == x.TmdbId);

                                    if (movie != null)
                                    {
                                        movie.ListId = importList.Definition.Id;
                                    }

                                    return(movie ?? MapMovieReport(x));
                                }).Where(x => x.TmdbId > 0).ToList();

                                listMovies = listMovies.DistinctBy(x => x.TmdbId).ToList();
                                listMovies.ForEach(m => m.ListId = importList.Definition.Id);

                                result.Movies.AddRange(listMovies);
                                _listMovieService.SyncMoviesForList(listMovies, importList.Definition.Id);
                            }

                            result.AnyFailure |= importListReports.AnyFailure;
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.Error(e, "Error during Import List Sync for list {0}", importList.Name);
                    }
                }).LogExceptions();

                taskList.Add(task);
            }

            Task.WaitAll(taskList.ToArray());

            _logger.Debug("Found {0} reports for all lists", result.Movies.Count);

            return(result);
        }