2018-03-14 20:41:36 +00:00
|
|
|
using System;
|
2013-04-07 07:30:37 +00:00
|
|
|
using System.Collections.Generic;
|
2019-12-22 22:08:53 +00:00
|
|
|
using System.Linq;
|
2013-04-07 07:30:37 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using NLog;
|
2020-07-08 13:24:40 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2014-07-23 23:43:54 +00:00
|
|
|
using NzbDrone.Common.Instrumentation.Extensions;
|
2019-12-22 22:08:53 +00:00
|
|
|
using NzbDrone.Common.TPL;
|
2013-04-07 07:30:37 +00:00
|
|
|
using NzbDrone.Core.DecisionEngine;
|
|
|
|
using NzbDrone.Core.Indexers;
|
2019-12-22 22:08:53 +00:00
|
|
|
using NzbDrone.Core.IndexerSearch.Definitions;
|
2018-03-14 20:41:36 +00:00
|
|
|
using NzbDrone.Core.Movies;
|
2020-05-26 01:55:10 +00:00
|
|
|
using NzbDrone.Core.Movies.Translations;
|
2019-12-22 22:08:53 +00:00
|
|
|
using NzbDrone.Core.Parser.Model;
|
2020-05-26 01:55:10 +00:00
|
|
|
using NzbDrone.Core.Profiles;
|
2013-04-07 07:30:37 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.IndexerSearch
|
|
|
|
{
|
|
|
|
public interface ISearchForNzb
|
|
|
|
{
|
2019-07-16 02:41:43 +00:00
|
|
|
List<DownloadDecision> MovieSearch(int movieId, bool userInvokedSearch, bool interactiveSearch);
|
|
|
|
List<DownloadDecision> MovieSearch(Movie movie, bool userInvokedSearch, bool interactiveSearch);
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class NzbSearchService : ISearchForNzb
|
|
|
|
{
|
2013-09-24 23:42:55 +00:00
|
|
|
private readonly IIndexerFactory _indexerFactory;
|
2013-04-07 07:30:37 +00:00
|
|
|
private readonly IMakeDownloadDecision _makeDownloadDecision;
|
2017-01-02 17:05:55 +00:00
|
|
|
private readonly IMovieService _movieService;
|
2020-05-26 01:55:10 +00:00
|
|
|
private readonly IMovieTranslationService _movieTranslationService;
|
|
|
|
private readonly IProfileService _profileService;
|
2013-04-07 07:30:37 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-09-24 23:42:55 +00:00
|
|
|
public NzbSearchService(IIndexerFactory indexerFactory,
|
2013-06-19 02:08:29 +00:00
|
|
|
IMakeDownloadDecision makeDownloadDecision,
|
2017-01-02 17:05:55 +00:00
|
|
|
IMovieService movieService,
|
2020-05-26 01:55:10 +00:00
|
|
|
IMovieTranslationService movieTranslationService,
|
|
|
|
IProfileService profileService,
|
2013-06-19 02:08:29 +00:00
|
|
|
Logger logger)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-09-24 23:42:55 +00:00
|
|
|
_indexerFactory = indexerFactory;
|
2013-04-07 07:30:37 +00:00
|
|
|
_makeDownloadDecision = makeDownloadDecision;
|
2017-01-02 17:05:55 +00:00
|
|
|
_movieService = movieService;
|
2020-05-26 01:55:10 +00:00
|
|
|
_movieTranslationService = movieTranslationService;
|
|
|
|
_profileService = profileService;
|
2013-04-07 07:30:37 +00:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2019-07-16 02:41:43 +00:00
|
|
|
public List<DownloadDecision> MovieSearch(int movieId, bool userInvokedSearch, bool interactiveSearch)
|
2017-01-02 17:05:55 +00:00
|
|
|
{
|
|
|
|
var movie = _movieService.GetMovie(movieId);
|
2020-08-05 03:00:21 +00:00
|
|
|
movie.Translations = _movieTranslationService.GetAllTranslationsForMovie(movie.Id);
|
2017-01-02 17:05:55 +00:00
|
|
|
|
2019-07-16 02:41:43 +00:00
|
|
|
return MovieSearch(movie, userInvokedSearch, interactiveSearch);
|
2017-01-02 17:05:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 02:41:43 +00:00
|
|
|
public List<DownloadDecision> MovieSearch(Movie movie, bool userInvokedSearch, bool interactiveSearch)
|
2017-01-02 17:05:55 +00:00
|
|
|
{
|
2021-01-31 03:47:06 +00:00
|
|
|
var downloadDecisions = new List<DownloadDecision>();
|
|
|
|
|
2019-07-16 02:41:43 +00:00
|
|
|
var searchSpec = Get<MovieSearchCriteria>(movie, userInvokedSearch, interactiveSearch);
|
2017-01-02 17:05:55 +00:00
|
|
|
|
2021-01-31 03:47:06 +00:00
|
|
|
var decisions = Dispatch(indexer => indexer.Fetch(searchSpec), searchSpec);
|
|
|
|
downloadDecisions.AddRange(decisions);
|
|
|
|
|
|
|
|
return DeDupeDecisions(downloadDecisions);
|
2017-01-02 17:05:55 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:08:53 +00:00
|
|
|
private TSpec Get<TSpec>(Movie movie, bool userInvokedSearch, bool interactiveSearch)
|
|
|
|
where TSpec : SearchCriteriaBase, new()
|
2017-01-02 17:05:55 +00:00
|
|
|
{
|
2017-01-03 23:33:16 +00:00
|
|
|
var spec = new TSpec()
|
|
|
|
{
|
|
|
|
Movie = movie,
|
2019-07-16 02:41:43 +00:00
|
|
|
UserInvokedSearch = userInvokedSearch,
|
|
|
|
InteractiveSearch = interactiveSearch
|
2019-12-22 22:08:53 +00:00
|
|
|
};
|
2020-05-26 01:55:10 +00:00
|
|
|
|
|
|
|
var wantedLanguages = _profileService.GetAcceptableLanguages(movie.ProfileId);
|
|
|
|
var translations = _movieTranslationService.GetAllTranslationsForMovie(movie.Id);
|
|
|
|
|
|
|
|
var queryTranlations = new List<string>
|
|
|
|
{
|
|
|
|
movie.Title,
|
|
|
|
movie.OriginalTitle
|
|
|
|
};
|
|
|
|
|
|
|
|
//Add Translation of wanted languages to search query
|
|
|
|
foreach (var translation in translations.Where(a => wantedLanguages.Contains(a.Language)))
|
|
|
|
{
|
|
|
|
queryTranlations.Add(translation.Title);
|
|
|
|
}
|
|
|
|
|
2020-07-08 13:24:40 +00:00
|
|
|
spec.SceneTitles = queryTranlations.Distinct().Where(t => t.IsNotNullOrWhiteSpace()).ToList();
|
2020-05-26 01:55:10 +00:00
|
|
|
|
2017-01-02 17:05:55 +00:00
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
|
2013-09-13 23:17:58 +00:00
|
|
|
private List<DownloadDecision> Dispatch(Func<IIndexer, IEnumerable<ReleaseInfo>> searchAction, SearchCriteriaBase criteriaBase)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2019-06-14 03:54:25 +00:00
|
|
|
var indexers = criteriaBase.InteractiveSearch ?
|
|
|
|
_indexerFactory.InteractiveSearchEnabled() :
|
|
|
|
_indexerFactory.AutomaticSearchEnabled();
|
|
|
|
|
2013-09-13 23:17:58 +00:00
|
|
|
var reports = new List<ReleaseInfo>();
|
2013-04-07 07:30:37 +00:00
|
|
|
|
2013-09-11 06:33:47 +00:00
|
|
|
_logger.ProgressInfo("Searching {0} indexers for {1}", indexers.Count, criteriaBase);
|
2013-05-30 01:35:26 +00:00
|
|
|
|
|
|
|
var taskList = new List<Task>();
|
|
|
|
var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None);
|
|
|
|
|
|
|
|
foreach (var indexer in indexers)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-05-30 01:35:26 +00:00
|
|
|
var indexerLocal = indexer;
|
|
|
|
|
|
|
|
taskList.Add(taskFactory.StartNew(() =>
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-05-30 01:35:26 +00:00
|
|
|
try
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-05-30 01:35:26 +00:00
|
|
|
var indexerReports = searchAction(indexerLocal);
|
|
|
|
|
|
|
|
lock (reports)
|
|
|
|
{
|
|
|
|
reports.AddRange(indexerReports);
|
|
|
|
}
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
2013-05-30 01:35:26 +00:00
|
|
|
catch (Exception e)
|
|
|
|
{
|
2019-06-14 03:54:25 +00:00
|
|
|
_logger.Error(e, "Error while searching for {0}", criteriaBase);
|
2013-05-30 01:35:26 +00:00
|
|
|
}
|
2013-07-12 06:10:34 +00:00
|
|
|
}).LogExceptions());
|
2013-05-30 01:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Task.WaitAll(taskList.ToArray());
|
2013-04-07 07:30:37 +00:00
|
|
|
|
2013-09-04 05:01:28 +00:00
|
|
|
_logger.Debug("Total of {0} reports were found for {1} from {2} indexers", reports.Count, criteriaBase, indexers.Count);
|
2013-04-07 07:30:37 +00:00
|
|
|
|
2013-06-06 14:42:23 +00:00
|
|
|
return _makeDownloadDecision.GetSearchDecision(reports, criteriaBase).ToList();
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
2021-01-31 03:47:06 +00:00
|
|
|
|
|
|
|
private List<DownloadDecision> DeDupeDecisions(List<DownloadDecision> decisions)
|
|
|
|
{
|
|
|
|
// De-dupe reports by guid so duplicate results aren't returned. Pick the one with the least rejections.
|
|
|
|
return decisions.GroupBy(d => d.RemoteMovie.Release.Guid).Select(d => d.OrderBy(v => v.Rejections.Count()).First()).ToList();
|
|
|
|
}
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
2013-09-13 23:17:58 +00:00
|
|
|
}
|