2013-04-28 19:46:13 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-04-15 01:41:39 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
2013-09-14 00:41:14 +00:00
|
|
|
|
using NzbDrone.Common;
|
2013-04-15 01:41:39 +00:00
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Parser
|
|
|
|
|
{
|
|
|
|
|
public interface IParsingService
|
|
|
|
|
{
|
2013-07-13 08:11:04 +00:00
|
|
|
|
LocalEpisode GetEpisodes(string filename, Series series);
|
2013-07-23 05:50:32 +00:00
|
|
|
|
LocalEpisode GetEpisodes(string filename, Series series, bool sceneSource);
|
2013-04-15 01:41:39 +00:00
|
|
|
|
Series GetSeries(string title);
|
2013-08-19 05:53:26 +00:00
|
|
|
|
RemoteEpisode Map(ParsedEpisodeInfo parsedEpisodeInfo, int tvRageId);
|
2013-04-15 01:41:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ParsingService : IParsingService
|
|
|
|
|
{
|
|
|
|
|
private readonly IEpisodeService _episodeService;
|
|
|
|
|
private readonly ISeriesService _seriesService;
|
2013-09-14 00:41:14 +00:00
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-04-15 01:41:39 +00:00
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
2013-09-14 00:41:14 +00:00
|
|
|
|
public ParsingService(IEpisodeService episodeService,
|
|
|
|
|
ISeriesService seriesService,
|
|
|
|
|
IDiskProvider diskProvider,
|
|
|
|
|
Logger logger)
|
2013-04-15 01:41:39 +00:00
|
|
|
|
{
|
|
|
|
|
_episodeService = episodeService;
|
|
|
|
|
_seriesService = seriesService;
|
2013-09-14 00:41:14 +00:00
|
|
|
|
_diskProvider = diskProvider;
|
2013-04-15 01:41:39 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-13 08:11:04 +00:00
|
|
|
|
public LocalEpisode GetEpisodes(string filename, Series series)
|
2013-07-23 05:50:32 +00:00
|
|
|
|
{
|
|
|
|
|
return GetEpisodes(filename, series, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LocalEpisode GetEpisodes(string filename, Series series, bool sceneSource)
|
2013-04-15 01:41:39 +00:00
|
|
|
|
{
|
2013-07-13 08:11:04 +00:00
|
|
|
|
var parsedEpisodeInfo = Parser.ParsePath(filename);
|
2013-04-15 01:41:39 +00:00
|
|
|
|
|
2013-04-23 06:14:55 +00:00
|
|
|
|
if (parsedEpisodeInfo == null)
|
2013-04-15 01:41:39 +00:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-23 05:50:32 +00:00
|
|
|
|
var episodes = GetEpisodes(parsedEpisodeInfo, series, sceneSource);
|
2013-04-15 01:41:39 +00:00
|
|
|
|
|
|
|
|
|
if (!episodes.Any())
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new LocalEpisode
|
2013-07-23 05:50:32 +00:00
|
|
|
|
{
|
|
|
|
|
Series = series,
|
|
|
|
|
Quality = parsedEpisodeInfo.Quality,
|
|
|
|
|
Episodes = episodes,
|
|
|
|
|
Path = filename,
|
2013-09-14 00:41:14 +00:00
|
|
|
|
ParsedEpisodeInfo = parsedEpisodeInfo,
|
|
|
|
|
ExistingFile = _diskProvider.IsParent(series.Path, filename)
|
2013-07-23 05:50:32 +00:00
|
|
|
|
};
|
2013-04-15 01:41:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Series GetSeries(string title)
|
|
|
|
|
{
|
2013-04-17 23:32:53 +00:00
|
|
|
|
var searchTitle = title;
|
|
|
|
|
|
2013-04-23 06:14:55 +00:00
|
|
|
|
var parsedEpisodeInfo = Parser.ParseTitle(title);
|
2013-04-17 23:32:53 +00:00
|
|
|
|
|
2013-04-23 06:14:55 +00:00
|
|
|
|
if (parsedEpisodeInfo != null)
|
2013-04-17 23:32:53 +00:00
|
|
|
|
{
|
2013-04-23 06:14:55 +00:00
|
|
|
|
searchTitle = parsedEpisodeInfo.SeriesTitle;
|
2013-04-17 23:32:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _seriesService.FindByTitle(searchTitle);
|
2013-04-15 01:41:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-19 05:53:26 +00:00
|
|
|
|
public RemoteEpisode Map(ParsedEpisodeInfo parsedEpisodeInfo, int tvRageId)
|
2013-04-15 01:41:39 +00:00
|
|
|
|
{
|
2013-04-28 19:46:13 +00:00
|
|
|
|
var remoteEpisode = new RemoteEpisode
|
|
|
|
|
{
|
|
|
|
|
ParsedEpisodeInfo = parsedEpisodeInfo,
|
|
|
|
|
};
|
2013-04-28 00:25:28 +00:00
|
|
|
|
|
2013-04-28 19:46:13 +00:00
|
|
|
|
var series = _seriesService.FindByTitle(parsedEpisodeInfo.SeriesTitle);
|
2013-04-28 00:25:28 +00:00
|
|
|
|
|
2013-08-19 05:53:26 +00:00
|
|
|
|
if (series == null && tvRageId > 0)
|
|
|
|
|
{
|
|
|
|
|
series = _seriesService.FindByTvRageId(tvRageId);
|
|
|
|
|
//TODO: If series is found by TvRageId, we should report it as a scene naming exception, since it will fail to import
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-28 00:25:28 +00:00
|
|
|
|
if (series == null)
|
|
|
|
|
{
|
2013-04-28 19:46:13 +00:00
|
|
|
|
_logger.Trace("No matching series {0}", parsedEpisodeInfo.SeriesTitle);
|
|
|
|
|
return remoteEpisode;
|
2013-04-28 00:25:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-28 19:46:13 +00:00
|
|
|
|
remoteEpisode.Series = series;
|
2013-07-23 05:50:32 +00:00
|
|
|
|
remoteEpisode.Episodes = GetEpisodes(parsedEpisodeInfo, series, true);
|
2013-04-28 00:25:28 +00:00
|
|
|
|
|
|
|
|
|
return remoteEpisode;
|
2013-04-15 01:41:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-23 05:50:32 +00:00
|
|
|
|
private List<Episode> GetEpisodes(ParsedEpisodeInfo parsedEpisodeInfo, Series series, bool sceneSource)
|
2013-04-15 01:41:39 +00:00
|
|
|
|
{
|
|
|
|
|
var result = new List<Episode>();
|
|
|
|
|
|
2013-04-23 06:14:55 +00:00
|
|
|
|
if (parsedEpisodeInfo.AirDate.HasValue)
|
2013-04-15 01:41:39 +00:00
|
|
|
|
{
|
|
|
|
|
if (series.SeriesType == SeriesTypes.Standard)
|
|
|
|
|
{
|
2013-07-05 05:17:25 +00:00
|
|
|
|
_logger.Warn("Found daily-style episode for non-daily series: {0}.", series);
|
2013-04-15 01:41:39 +00:00
|
|
|
|
return new List<Episode>();
|
|
|
|
|
}
|
2013-05-21 04:30:54 +00:00
|
|
|
|
|
2013-04-23 06:14:55 +00:00
|
|
|
|
var episodeInfo = _episodeService.GetEpisode(series.Id, parsedEpisodeInfo.AirDate.Value);
|
2013-04-15 01:41:39 +00:00
|
|
|
|
|
|
|
|
|
if (episodeInfo != null)
|
|
|
|
|
{
|
|
|
|
|
result.Add(episodeInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-23 06:14:55 +00:00
|
|
|
|
if (parsedEpisodeInfo.EpisodeNumbers == null)
|
2013-04-15 01:41:39 +00:00
|
|
|
|
return result;
|
|
|
|
|
|
2013-04-23 06:14:55 +00:00
|
|
|
|
foreach (var episodeNumber in parsedEpisodeInfo.EpisodeNumbers)
|
2013-04-15 01:41:39 +00:00
|
|
|
|
{
|
|
|
|
|
Episode episodeInfo = null;
|
|
|
|
|
|
2013-07-23 05:50:32 +00:00
|
|
|
|
if (series.UseSceneNumbering && sceneSource)
|
2013-04-15 01:41:39 +00:00
|
|
|
|
{
|
2013-05-29 01:28:47 +00:00
|
|
|
|
episodeInfo = _episodeService.FindEpisode(series.Id, parsedEpisodeInfo.SeasonNumber, episodeNumber, true);
|
2013-07-23 05:50:32 +00:00
|
|
|
|
|
|
|
|
|
if (episodeInfo != null)
|
|
|
|
|
{
|
|
|
|
|
_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);
|
|
|
|
|
}
|
2013-04-15 01:41:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (episodeInfo == null)
|
|
|
|
|
{
|
2013-04-23 06:14:55 +00:00
|
|
|
|
episodeInfo = _episodeService.GetEpisode(series.Id, parsedEpisodeInfo.SeasonNumber, episodeNumber);
|
2013-04-15 01:41:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (episodeInfo != null)
|
|
|
|
|
{
|
|
|
|
|
result.Add(episodeInfo);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-04-23 06:14:55 +00:00
|
|
|
|
_logger.Debug("Unable to find {0}", parsedEpisodeInfo);
|
2013-04-15 01:41:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|