2013-04-15 03:41:39 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Parser
|
|
|
|
|
{
|
|
|
|
|
public interface IParsingService
|
|
|
|
|
{
|
|
|
|
|
LocalEpisode GetEpisodes(string fileName, Series series);
|
|
|
|
|
Series GetSeries(string title);
|
|
|
|
|
RemoteEpisode Map(ReportInfo indexerParseResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ParsingService : IParsingService
|
|
|
|
|
{
|
|
|
|
|
private readonly IEpisodeService _episodeService;
|
|
|
|
|
private readonly ISeriesService _seriesService;
|
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
public ParsingService(IEpisodeService episodeService, ISeriesService seriesService, Logger logger)
|
|
|
|
|
{
|
|
|
|
|
_episodeService = episodeService;
|
|
|
|
|
_seriesService = seriesService;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public LocalEpisode GetEpisodes(string fileName, Series series)
|
|
|
|
|
{
|
2013-04-23 08:14:55 +02:00
|
|
|
|
var parsedEpisodeInfo = Parser.ParseTitle(fileName);
|
2013-04-15 03:41:39 +02:00
|
|
|
|
|
2013-04-23 08:14:55 +02:00
|
|
|
|
if (parsedEpisodeInfo == null)
|
2013-04-15 03:41:39 +02:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-23 08:14:55 +02:00
|
|
|
|
var episodes = GetEpisodes(parsedEpisodeInfo, series);
|
2013-04-15 03:41:39 +02:00
|
|
|
|
|
|
|
|
|
if (!episodes.Any())
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new LocalEpisode
|
|
|
|
|
{
|
2013-04-23 08:14:55 +02:00
|
|
|
|
Quality = parsedEpisodeInfo.Quality,
|
2013-04-15 03:41:39 +02:00
|
|
|
|
Episodes = episodes,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Series GetSeries(string title)
|
|
|
|
|
{
|
2013-04-18 01:32:53 +02:00
|
|
|
|
var searchTitle = title;
|
|
|
|
|
|
2013-04-23 08:14:55 +02:00
|
|
|
|
var parsedEpisodeInfo = Parser.ParseTitle(title);
|
2013-04-18 01:32:53 +02:00
|
|
|
|
|
2013-04-23 08:14:55 +02:00
|
|
|
|
if (parsedEpisodeInfo != null)
|
2013-04-18 01:32:53 +02:00
|
|
|
|
{
|
2013-04-23 08:14:55 +02:00
|
|
|
|
searchTitle = parsedEpisodeInfo.SeriesTitle;
|
2013-04-18 01:32:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _seriesService.FindByTitle(searchTitle);
|
2013-04-15 03:41:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RemoteEpisode Map(ReportInfo indexerParseResult)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-23 08:14:55 +02:00
|
|
|
|
private List<Episode> GetEpisodes(ParsedEpisodeInfo parsedEpisodeInfo, Series series)
|
2013-04-15 03:41:39 +02:00
|
|
|
|
{
|
|
|
|
|
var result = new List<Episode>();
|
|
|
|
|
|
2013-04-23 08:14:55 +02:00
|
|
|
|
if (parsedEpisodeInfo.AirDate.HasValue)
|
2013-04-15 03:41:39 +02:00
|
|
|
|
{
|
|
|
|
|
if (series.SeriesType == SeriesTypes.Standard)
|
|
|
|
|
{
|
|
|
|
|
//Todo: Collect this as a Series we want to treat as a daily series, or possible parsing error
|
2013-04-23 08:14:55 +02:00
|
|
|
|
_logger.Warn("Found daily-style episode for non-daily series: {0}. {1}", series.Title, parsedEpisodeInfo.OriginalString);
|
2013-04-15 03:41:39 +02:00
|
|
|
|
return new List<Episode>();
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-23 08:14:55 +02:00
|
|
|
|
var episodeInfo = _episodeService.GetEpisode(series.Id, parsedEpisodeInfo.AirDate.Value);
|
2013-04-15 03:41:39 +02:00
|
|
|
|
|
|
|
|
|
if (episodeInfo != null)
|
|
|
|
|
{
|
|
|
|
|
result.Add(episodeInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-23 08:14:55 +02:00
|
|
|
|
if (parsedEpisodeInfo.EpisodeNumbers == null)
|
2013-04-15 03:41:39 +02:00
|
|
|
|
return result;
|
|
|
|
|
|
2013-04-23 08:14:55 +02:00
|
|
|
|
foreach (var episodeNumber in parsedEpisodeInfo.EpisodeNumbers)
|
2013-04-15 03:41:39 +02:00
|
|
|
|
{
|
|
|
|
|
Episode episodeInfo = null;
|
|
|
|
|
|
2013-04-23 08:14:55 +02:00
|
|
|
|
if (series.UseSceneNumbering && parsedEpisodeInfo.SceneSource)
|
2013-04-15 03:41:39 +02:00
|
|
|
|
{
|
2013-04-23 08:14:55 +02:00
|
|
|
|
episodeInfo = _episodeService.GetEpisode(series.Id, parsedEpisodeInfo.SeasonNumber, episodeNumber, true);
|
2013-04-15 03:41:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (episodeInfo == null)
|
|
|
|
|
{
|
2013-04-23 08:14:55 +02:00
|
|
|
|
episodeInfo = _episodeService.GetEpisode(series.Id, parsedEpisodeInfo.SeasonNumber, episodeNumber);
|
|
|
|
|
if (episodeInfo == null && parsedEpisodeInfo.AirDate != null)
|
2013-04-15 03:41:39 +02:00
|
|
|
|
{
|
2013-04-23 08:14:55 +02:00
|
|
|
|
episodeInfo = _episodeService.GetEpisode(series.Id, parsedEpisodeInfo.AirDate.Value);
|
2013-04-15 03:41:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (episodeInfo != null)
|
|
|
|
|
{
|
|
|
|
|
result.Add(episodeInfo);
|
|
|
|
|
|
|
|
|
|
if (series.UseSceneNumbering)
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Using Scene to TVDB Mapping for: {0} - Scene: {1}x{2:00} - TVDB: {3}x{4:00}",
|
|
|
|
|
series.Title,
|
|
|
|
|
episodeInfo.SceneSeasonNumber,
|
|
|
|
|
episodeInfo.SceneEpisodeNumber,
|
|
|
|
|
episodeInfo.SeasonNumber,
|
|
|
|
|
episodeInfo.EpisodeNumber);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-04-23 08:14:55 +02:00
|
|
|
|
_logger.Debug("Unable to find {0}", parsedEpisodeInfo);
|
2013-04-15 03:41:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|