1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Core/IndexerSearch/PartialSeasonSearch.cs

103 lines
3.5 KiB
C#
Raw Normal View History

2013-03-07 06:45:36 +03:00
using System;
2013-01-13 11:24:48 +03:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NLog;
2013-03-07 06:45:36 +03:00
using NzbDrone.Core.DecisionEngine;
using NzbDrone.Core.Download;
2013-02-21 10:07:34 +03:00
using NzbDrone.Core.Indexers;
2013-01-13 11:24:48 +03:00
using NzbDrone.Core.Model;
using NzbDrone.Core.Model.Notification;
2013-03-07 06:45:36 +03:00
using NzbDrone.Core.ReferenceData;
using NzbDrone.Core.Tv;
2013-01-13 11:24:48 +03:00
2013-03-07 06:45:36 +03:00
namespace NzbDrone.Core.IndexerSearch
2013-01-13 11:24:48 +03:00
{
public class PartialSeasonSearch : IndexerSearchBase
2013-01-13 11:24:48 +03:00
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
2013-03-27 06:44:52 +03:00
public PartialSeasonSearch(IEpisodeService episodeService, IDownloadProvider downloadProvider, IIndexerService indexerService,
2013-03-07 06:45:36 +03:00
ISceneMappingService sceneMappingService, IDownloadDirector downloadDirector,
2013-02-26 05:20:42 +03:00
ISeriesRepository seriesRepository)
2013-03-07 04:51:47 +03:00
: base(seriesRepository, episodeService, downloadProvider, indexerService, sceneMappingService,
downloadDirector)
{
2013-01-13 11:24:48 +03:00
}
2013-01-13 21:29:53 +03:00
public PartialSeasonSearch()
{
}
2013-03-07 06:45:36 +03:00
public override List<EpisodeParseResult> PerformSearch(Series series, List<Episode> episodes, ProgressNotification notification)
2013-01-13 11:24:48 +03:00
{
2013-03-07 06:45:36 +03:00
var seasons = episodes.Select(c => c.SeasonNumber).Distinct().ToList();
2013-01-13 11:24:48 +03:00
2013-03-07 06:45:36 +03:00
if (seasons.Count > 1)
{
throw new ArgumentOutOfRangeException("episodes", "episode list contains episodes from more than one season");
}
2013-01-13 11:24:48 +03:00
2013-03-07 06:45:36 +03:00
var seasonNumber = seasons[0];
notification.CurrentMessage = String.Format("Looking for {0} - Season {1}", series.Title, seasonNumber);
2013-01-13 11:24:48 +03:00
var reports = new List<EpisodeParseResult>();
object reportsLock = new object();
var title = GetSearchTitle(series);
var prefixes = GetEpisodeNumberPrefixes(episodes.Select(e => e.EpisodeNumber));
2013-03-07 04:51:47 +03:00
foreach (var p in prefixes)
2013-01-13 11:24:48 +03:00
{
var prefix = p;
2013-02-21 10:07:34 +03:00
Parallel.ForEach(_indexerService.GetEnabledIndexers(), indexer =>
2013-01-13 11:24:48 +03:00
{
try
{
2013-03-07 04:51:47 +03:00
lock (reportsLock)
2013-01-13 11:24:48 +03:00
{
2013-03-07 06:45:36 +03:00
reports.AddRange(indexer.FetchPartialSeason(title, seasonNumber, prefix));
2013-01-13 11:24:48 +03:00
}
}
2013-03-07 04:51:47 +03:00
catch (Exception e)
2013-01-13 11:24:48 +03:00
{
logger.ErrorException(
String.Format(
"An error has occurred while searching for {0} Season {1:00} Prefix: {2} from: {3}",
2013-03-07 06:45:36 +03:00
series.Title, seasonNumber, prefix, indexer.Name),
2013-01-13 11:24:48 +03:00
e);
}
});
}
return reports;
}
2013-03-07 04:51:47 +03:00
public override bool IsEpisodeMatch(Series series, dynamic options, EpisodeParseResult episodeParseResult)
2013-01-13 11:24:48 +03:00
{
2013-03-07 04:51:47 +03:00
if (options.SeasonNumber != episodeParseResult.SeasonNumber)
2013-01-13 11:24:48 +03:00
{
logger.Trace("Season number does not match searched season number, skipping.");
2013-03-07 04:51:47 +03:00
return false;
2013-01-13 11:24:48 +03:00
}
2013-03-07 04:51:47 +03:00
return true;
2013-01-13 11:24:48 +03:00
}
private List<int> GetEpisodeNumberPrefixes(IEnumerable<int> episodeNumbers)
{
var results = new List<int>();
foreach (var i in episodeNumbers)
{
results.Add(i / 10);
}
return results.Distinct().ToList();
}
}
}