Radarr/src/NzbDrone.Core/IndexerSearch/ReleaseSearchService.cs

151 lines
6.0 KiB
C#
Raw Normal View History

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;
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;
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 ISearchForReleases
2013-04-07 07:30:37 +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 ReleaseSearchService : ISearchForReleases
2013-04-07 07:30:37 +00:00
{
2013-09-24 23:42:55 +00:00
private readonly IIndexerFactory _indexerFactory;
2013-04-07 07:30:37 +00:00
private readonly IMakeDownloadDecision _makeDownloadDecision;
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;
public ReleaseSearchService(IIndexerFactory indexerFactory,
2013-06-19 02:08:29 +00:00
IMakeDownloadDecision makeDownloadDecision,
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;
_movieService = movieService;
2020-05-26 01:55:10 +00:00
_movieTranslationService = movieTranslationService;
_profileService = profileService;
2013-04-07 07:30:37 +00:00
_logger = logger;
}
public List<DownloadDecision> MovieSearch(int movieId, bool userInvokedSearch, bool interactiveSearch)
{
var movie = _movieService.GetMovie(movieId);
movie.MovieMetadata.Value.Translations = _movieTranslationService.GetAllTranslationsForMovieMetadata(movie.MovieMetadataId);
return MovieSearch(movie, userInvokedSearch, interactiveSearch);
}
public List<DownloadDecision> MovieSearch(Movie movie, bool userInvokedSearch, bool interactiveSearch)
{
var downloadDecisions = new List<DownloadDecision>();
var searchSpec = Get<MovieSearchCriteria>(movie, userInvokedSearch, interactiveSearch);
var decisions = Dispatch(indexer => indexer.Fetch(searchSpec), searchSpec);
downloadDecisions.AddRange(decisions);
return DeDupeDecisions(downloadDecisions);
}
2019-12-22 22:08:53 +00:00
private TSpec Get<TSpec>(Movie movie, bool userInvokedSearch, bool interactiveSearch)
where TSpec : SearchCriteriaBase, new()
{
var spec = new TSpec()
{
Movie = movie,
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.GetAllTranslationsForMovieMetadata(movie.MovieMetadataId);
2020-05-26 01:55:10 +00:00
var queryTranlations = new List<string>
{
2022-03-20 15:55:47 +00:00
movie.MovieMetadata.Value.Title,
movie.MovieMetadata.Value.OriginalTitle
2020-05-26 01:55:10 +00:00
};
2022-11-20 18:27:45 +00:00
// Add Translation of wanted languages to search query
2020-05-26 01:55:10 +00:00
foreach (var translation in translations.Where(a => wantedLanguages.Contains(a.Language)))
{
queryTranlations.Add(translation.Title);
}
spec.SceneTitles = queryTranlations.Distinct().Where(t => t.IsNotNullOrWhiteSpace()).ToList();
2020-05-26 01:55:10 +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
{
var indexers = criteriaBase.InteractiveSearch ?
_indexerFactory.InteractiveSearchEnabled() :
_indexerFactory.AutomaticSearchEnabled();
// Filter indexers to untagged indexers and indexers with intersecting tags
indexers = indexers.Where(i => i.Definition.Tags.Empty() || i.Definition.Tags.Intersect(criteriaBase.Movie.Tags).Any()).ToList();
2013-09-13 23:17:58 +00:00
var reports = new List<ReleaseInfo>();
2013-04-07 07:30:37 +00:00
_logger.ProgressInfo("Searching indexers for {0}. {1} active indexers", criteriaBase, indexers.Count);
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
{
var indexerLocal = indexer;
taskList.Add(taskFactory.StartNew(() =>
2013-04-07 07:30:37 +00:00
{
try
2013-04-07 07:30:37 +00:00
{
var indexerReports = searchAction(indexerLocal);
lock (reports)
{
reports.AddRange(indexerReports);
}
2013-04-07 07:30:37 +00:00
}
catch (Exception e)
{
_logger.Error(e, "Error while searching for {0}", criteriaBase);
}
2013-07-12 06:10:34 +00:00
}).LogExceptions());
}
Task.WaitAll(taskList.ToArray());
2013-04-07 07:30:37 +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
return _makeDownloadDecision.GetSearchDecision(reports, criteriaBase).ToList();
2013-04-07 07:30:37 +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
}