2018-11-23 07:03:32 +00:00
|
|
|
using System;
|
2017-03-04 02:32:52 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common.Instrumentation.Extensions;
|
|
|
|
using NzbDrone.Core.Download;
|
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
2018-03-14 20:41:36 +00:00
|
|
|
using NzbDrone.Core.Movies;
|
2017-03-04 02:32:52 +00:00
|
|
|
using NzbDrone.Core.Datastore;
|
|
|
|
using NzbDrone.Core.Queue;
|
|
|
|
using NzbDrone.Core.DecisionEngine;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.IndexerSearch
|
|
|
|
{
|
2017-03-04 16:40:38 +00:00
|
|
|
public class MovieSearchService : IExecute<MoviesSearchCommand>, IExecute<MissingMoviesSearchCommand>, IExecute<CutoffUnmetMoviesSearchCommand>
|
2017-03-04 02:32:52 +00:00
|
|
|
{
|
|
|
|
private readonly IMovieService _movieService;
|
2017-03-04 16:40:38 +00:00
|
|
|
private readonly IMovieCutoffService _movieCutoffService;
|
2017-03-04 02:32:52 +00:00
|
|
|
private readonly ISearchForNzb _nzbSearchService;
|
|
|
|
private readonly IProcessDownloadDecisions _processDownloadDecisions;
|
|
|
|
private readonly IQueueService _queueService;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
public MovieSearchService(IMovieService movieService,
|
2017-03-04 16:40:38 +00:00
|
|
|
IMovieCutoffService movieCutoffService,
|
2017-03-04 02:32:52 +00:00
|
|
|
ISearchForNzb nzbSearchService,
|
|
|
|
IProcessDownloadDecisions processDownloadDecisions,
|
|
|
|
IQueueService queueService,
|
|
|
|
Logger logger)
|
|
|
|
{
|
|
|
|
_movieService = movieService;
|
2017-03-04 16:40:38 +00:00
|
|
|
_movieCutoffService = movieCutoffService;
|
2017-03-04 02:32:52 +00:00
|
|
|
_nzbSearchService = nzbSearchService;
|
|
|
|
_processDownloadDecisions = processDownloadDecisions;
|
|
|
|
_queueService = queueService;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Execute(MoviesSearchCommand message)
|
|
|
|
{
|
|
|
|
var downloadedCount = 0;
|
|
|
|
foreach (var movieId in message.MovieIds)
|
|
|
|
{
|
|
|
|
var movies = _movieService.GetMovie(movieId);
|
|
|
|
|
|
|
|
if (!movies.Monitored)
|
|
|
|
{
|
|
|
|
_logger.Debug("Movie {0} is not monitored, skipping search", movies.Title);
|
2018-10-30 10:09:54 +00:00
|
|
|
continue;
|
2017-03-04 02:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var decisions = _nzbSearchService.MovieSearch(movieId, false);//_nzbSearchService.SeasonSearch(message.MovieId, season.SeasonNumber, false, message.Trigger == CommandTrigger.Manual);
|
|
|
|
downloadedCount += _processDownloadDecisions.ProcessDecisions(decisions).Grabbed.Count;
|
|
|
|
|
|
|
|
}
|
|
|
|
_logger.ProgressInfo("Movie search completed. {0} reports downloaded.", downloadedCount);
|
2017-02-11 13:48:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 02:32:52 +00:00
|
|
|
public void Execute(MissingMoviesSearchCommand message)
|
2017-02-11 13:48:02 +00:00
|
|
|
{
|
2018-11-23 07:03:32 +00:00
|
|
|
var pagingSpec = new PagingSpec<Movie>
|
2017-03-04 02:32:52 +00:00
|
|
|
{
|
|
|
|
Page = 1,
|
|
|
|
PageSize = 100000,
|
|
|
|
SortDirection = SortDirection.Ascending,
|
2018-11-23 07:03:32 +00:00
|
|
|
SortKey = "Id"
|
|
|
|
};
|
2017-03-04 02:32:52 +00:00
|
|
|
|
2018-11-23 07:03:32 +00:00
|
|
|
pagingSpec.FilterExpressions.Add(v => v.Monitored == true);
|
|
|
|
List<Movie> movies = _movieService.MoviesWithoutFiles(pagingSpec).Records.ToList();
|
2017-03-04 02:32:52 +00:00
|
|
|
|
|
|
|
var queue = _queueService.GetQueue().Select(q => q.Movie.Id);
|
|
|
|
var missing = movies.Where(e => !queue.Contains(e.Id)).ToList();
|
|
|
|
|
|
|
|
SearchForMissingMovies(missing, message.Trigger == CommandTrigger.Manual);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-04 16:40:38 +00:00
|
|
|
public void Execute(CutoffUnmetMoviesSearchCommand message)
|
|
|
|
{
|
2018-11-23 07:03:32 +00:00
|
|
|
var pagingSpec = new PagingSpec<Movie>
|
2017-03-04 16:40:38 +00:00
|
|
|
{
|
|
|
|
Page = 1,
|
|
|
|
PageSize = 100000,
|
|
|
|
SortDirection = SortDirection.Ascending,
|
2018-11-23 07:03:32 +00:00
|
|
|
SortKey = "Id"
|
|
|
|
};
|
|
|
|
|
|
|
|
pagingSpec.FilterExpressions.Add(v => v.Monitored == true);
|
|
|
|
|
|
|
|
List<Movie> movies = _movieCutoffService.MoviesWhereCutoffUnmet(pagingSpec).Records.ToList();
|
2017-03-04 16:40:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
var queue = _queueService.GetQueue().Select(q => q.Movie.Id);
|
|
|
|
var missing = movies.Where(e => !queue.Contains(e.Id)).ToList();
|
|
|
|
|
|
|
|
SearchForMissingMovies(missing, message.Trigger == CommandTrigger.Manual);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-04 02:32:52 +00:00
|
|
|
private void SearchForMissingMovies(List<Movie> movies, bool userInvokedSearch)
|
|
|
|
{
|
|
|
|
_logger.ProgressInfo("Performing missing search for {0} movies", movies.Count);
|
2017-02-11 13:48:02 +00:00
|
|
|
var downloadedCount = 0;
|
|
|
|
|
|
|
|
foreach (var movieId in movies.GroupBy(e => e.Id))
|
|
|
|
{
|
|
|
|
List<DownloadDecision> decisions;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
decisions = _nzbSearchService.MovieSearch(movieId.Key, userInvokedSearch);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
var message = String.Format("Unable to search for missing movie {0}", movieId.Key);
|
|
|
|
_logger.Error(ex, message);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var processed = _processDownloadDecisions.ProcessDecisions(decisions);
|
|
|
|
|
|
|
|
downloadedCount += processed.Grabbed.Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.ProgressInfo("Completed missing search for {0} movies. {1} reports downloaded.", movies.Count, downloadedCount);
|
2017-03-04 02:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|