2010-09-28 19:32:19 +00:00
|
|
|
using System;
|
2010-10-17 17:22:48 +00:00
|
|
|
using System.Linq;
|
2010-09-28 19:32:19 +00:00
|
|
|
using System.Collections.Generic;
|
2010-09-28 05:01:54 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2010-10-05 06:21:18 +00:00
|
|
|
using NLog;
|
2010-10-08 03:35:04 +00:00
|
|
|
using NzbDrone.Core.Entities.Episode;
|
|
|
|
using NzbDrone.Core.Entities.Quality;
|
2010-09-28 19:32:19 +00:00
|
|
|
using SubSonic.Repository;
|
2010-09-28 05:01:54 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
{
|
2010-09-28 20:44:33 +00:00
|
|
|
public class EpisodeProvider : IEpisodeProvider
|
2010-09-28 05:01:54 +00:00
|
|
|
{
|
2010-09-28 19:32:19 +00:00
|
|
|
//TODO: Remove parsing of the series name, it should be done in series provider
|
2010-09-28 06:09:24 +00:00
|
|
|
private static readonly Regex ParseRegex = new Regex(@"(?<showName>.*)
|
2010-09-28 05:01:54 +00:00
|
|
|
(?:
|
|
|
|
s(?<seasonNumber>\d+)e(?<episodeNumber>\d+)-?e(?<episodeNumber2>\d+)
|
|
|
|
| s(?<seasonNumber>\d+)e(?<episodeNumber>\d+)
|
|
|
|
| (?<seasonNumber>\d+)x(?<episodeNumber>\d+)
|
|
|
|
| (?<airDate>\d{4}.\d{2}.\d{2})
|
|
|
|
)
|
|
|
|
(?:
|
|
|
|
(?<episodeName>.*?)
|
|
|
|
(?<release>
|
|
|
|
(?:hdtv|pdtv|xvid|ws|720p|x264|bdrip|dvdrip|dsr|proper)
|
|
|
|
.*)
|
|
|
|
| (?<episodeName>.*)
|
|
|
|
)", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);
|
|
|
|
|
2010-09-28 19:32:19 +00:00
|
|
|
|
|
|
|
private readonly IRepository _sonicRepo;
|
2010-10-05 06:21:18 +00:00
|
|
|
private readonly ISeriesProvider _series;
|
|
|
|
private readonly ISeasonProvider _seasons;
|
|
|
|
private readonly ITvDbProvider _tvDb;
|
2010-10-07 22:17:24 +00:00
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2010-09-28 19:32:19 +00:00
|
|
|
|
2010-10-05 06:21:18 +00:00
|
|
|
|
|
|
|
public EpisodeProvider(IRepository sonicRepo, ISeriesProvider seriesProvider, ISeasonProvider seasonProvider, ITvDbProvider tvDbProvider)
|
2010-09-28 05:01:54 +00:00
|
|
|
{
|
2010-09-28 19:32:19 +00:00
|
|
|
_sonicRepo = sonicRepo;
|
2010-10-05 06:21:18 +00:00
|
|
|
_series = seriesProvider;
|
|
|
|
_tvDb = tvDbProvider;
|
|
|
|
_seasons = seasonProvider;
|
2010-09-28 19:32:19 +00:00
|
|
|
}
|
|
|
|
|
2010-10-10 21:20:43 +00:00
|
|
|
public EpisodeInfo GetEpisode(long id)
|
2010-09-28 19:32:19 +00:00
|
|
|
{
|
2010-10-10 21:20:43 +00:00
|
|
|
return _sonicRepo.Single<EpisodeInfo>(e => e.EpisodeId == id);
|
2010-09-28 19:32:19 +00:00
|
|
|
}
|
|
|
|
|
2010-10-10 21:20:43 +00:00
|
|
|
public void UpdateEpisode(EpisodeInfo episode)
|
2010-09-28 19:32:19 +00:00
|
|
|
{
|
2010-10-10 21:20:43 +00:00
|
|
|
var episodeToUpdate = _sonicRepo.Single<EpisodeInfo>(e => e.EpisodeId == episode.EpisodeId);
|
|
|
|
|
|
|
|
episodeToUpdate.AirDate = episode.AirDate;
|
|
|
|
episodeToUpdate.Overview = episode.Overview;
|
|
|
|
episodeToUpdate.Title = episode.Title;
|
|
|
|
episodeToUpdate.EpisodeNumber = episode.EpisodeNumber;
|
|
|
|
episodeToUpdate.SeasonNumber = episode.SeasonNumber;
|
|
|
|
|
|
|
|
_sonicRepo.Update<EpisodeInfo>(episodeToUpdate);
|
2010-09-28 19:32:19 +00:00
|
|
|
}
|
|
|
|
|
2010-10-10 21:20:43 +00:00
|
|
|
public IList<EpisodeInfo> GetEpisodesBySeason(long seasonId)
|
2010-09-28 20:44:33 +00:00
|
|
|
{
|
2010-10-10 21:20:43 +00:00
|
|
|
return _sonicRepo.Find<EpisodeInfo>(e => e.SeasonId == seasonId);
|
2010-09-28 20:44:33 +00:00
|
|
|
}
|
|
|
|
|
2010-10-10 21:20:43 +00:00
|
|
|
public IList<EpisodeInfo> GetEpisodeBySeries(long seriesId)
|
2010-09-28 20:44:33 +00:00
|
|
|
{
|
2010-10-10 21:20:43 +00:00
|
|
|
return _sonicRepo.Find<EpisodeInfo>(e => e.SeriesId == seriesId);
|
2010-09-28 20:44:33 +00:00
|
|
|
}
|
|
|
|
|
2010-10-08 03:35:04 +00:00
|
|
|
public String GetSabTitle(BasicEpisode episode)
|
2010-09-28 19:32:19 +00:00
|
|
|
{
|
2010-10-05 06:21:18 +00:00
|
|
|
var series = _series.GetSeries(episode.SeriesId);
|
2010-09-28 19:32:19 +00:00
|
|
|
if (series == null) throw new ArgumentException("Unknown series. ID: " + episode.SeriesId);
|
|
|
|
|
|
|
|
//TODO: This method should return a standard title for the sab episode.
|
|
|
|
throw new NotImplementedException();
|
2010-09-28 05:01:54 +00:00
|
|
|
}
|
|
|
|
|
2010-09-28 19:32:19 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Comprehensive check on whether or not this episode is needed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="episode">Episode that needs to be checked</param>
|
|
|
|
/// <returns></returns>
|
2010-10-10 21:20:43 +00:00
|
|
|
public bool IsNeeded(RemoteEpisode episode)
|
2010-09-28 05:01:54 +00:00
|
|
|
{
|
2010-09-28 19:32:19 +00:00
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
2010-10-17 17:22:48 +00:00
|
|
|
public void RefreshEpisodeInfo(int seriesId)
|
2010-10-05 06:21:18 +00:00
|
|
|
{
|
|
|
|
Logger.Info("Starting episode info refresh for series:{0}", seriesId);
|
|
|
|
int successCount = 0;
|
|
|
|
int failCount = 0;
|
|
|
|
var targetSeries = _tvDb.GetSeries(seriesId, true);
|
2010-10-17 17:22:48 +00:00
|
|
|
|
|
|
|
var updateList = new List<EpisodeInfo>();
|
|
|
|
var newList = new List<EpisodeInfo>();
|
|
|
|
|
|
|
|
Logger.Debug("Updating season info for series:{0}", seriesId);
|
|
|
|
targetSeries.Episodes.Select(e => new { e.SeasonId, e.SeasonNumber })
|
|
|
|
.Distinct().ToList()
|
|
|
|
.ForEach(s => _seasons.EnsureSeason(seriesId, s.SeasonId, s.SeasonNumber));
|
|
|
|
|
2010-10-05 06:21:18 +00:00
|
|
|
foreach (var episode in targetSeries.Episodes)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2010-10-17 17:22:48 +00:00
|
|
|
Logger.Debug("Updating info for series:{0} - episode:{1}", seriesId, episode.Id);
|
2010-10-05 06:21:18 +00:00
|
|
|
var newEpisode = new EpisodeInfo()
|
2010-10-17 17:22:48 +00:00
|
|
|
{
|
|
|
|
AirDate = episode.FirstAired,
|
|
|
|
EpisodeId = episode.Id,
|
|
|
|
EpisodeNumber = episode.EpisodeNumber,
|
|
|
|
Language = episode.Language.Abbriviation,
|
|
|
|
Overview = episode.Overview,
|
|
|
|
SeasonId = episode.SeasonId,
|
|
|
|
SeasonNumber = episode.SeasonNumber,
|
|
|
|
SeriesId = seriesId,
|
|
|
|
Title = episode.EpisodeName
|
|
|
|
};
|
|
|
|
|
|
|
|
if (_sonicRepo.Exists<EpisodeInfo>(e => e.EpisodeId == newEpisode.EpisodeId))
|
2010-10-05 06:21:18 +00:00
|
|
|
{
|
2010-10-17 17:22:48 +00:00
|
|
|
updateList.Add(newEpisode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newList.Add(newEpisode);
|
|
|
|
}
|
2010-10-05 06:21:18 +00:00
|
|
|
|
2010-10-17 17:22:48 +00:00
|
|
|
successCount++;
|
2010-10-05 06:21:18 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2010-10-17 17:22:48 +00:00
|
|
|
Logger.FatalException(String.Format("An error has occurred while updating episode info for series {0}", seriesId), e);
|
2010-10-05 06:21:18 +00:00
|
|
|
failCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-17 17:22:48 +00:00
|
|
|
_sonicRepo.AddMany(newList);
|
|
|
|
_sonicRepo.UpdateMany(updateList);
|
|
|
|
|
2010-10-05 06:21:18 +00:00
|
|
|
Logger.Info("Finished episode refresh for series:{0}. Success:{1} - Fail:{2} ", seriesId, successCount, failCount);
|
|
|
|
}
|
|
|
|
|
2010-09-28 19:32:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Parses a post title into list of episode objects
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="title">Title of the report</param>
|
|
|
|
/// <returns>List of episodes relating to the post</returns>
|
2010-10-05 06:21:18 +00:00
|
|
|
public static List<RemoteEpisode> Parse(string title)
|
2010-09-28 19:32:19 +00:00
|
|
|
{
|
|
|
|
var match = ParseRegex.Match(title);
|
|
|
|
|
|
|
|
if (!match.Success)
|
|
|
|
throw new ArgumentException(String.Format("Title doesn't match any know patterns. [{0}]", title));
|
2010-09-28 06:09:24 +00:00
|
|
|
|
2010-10-05 06:21:18 +00:00
|
|
|
var result = new List<RemoteEpisode>();
|
2010-09-28 19:32:19 +00:00
|
|
|
|
2010-10-05 06:21:18 +00:00
|
|
|
result.Add(new RemoteEpisode { EpisodeNumber = Convert.ToInt32(match.Groups["episodeNumber"].Value) });
|
2010-09-28 19:32:19 +00:00
|
|
|
|
|
|
|
if (match.Groups["episodeNumber2"].Success)
|
|
|
|
{
|
2010-10-05 06:21:18 +00:00
|
|
|
result.Add(new RemoteEpisode { EpisodeNumber = Convert.ToInt32(match.Groups["episodeNumber2"].Value) });
|
2010-09-28 19:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var ep in result)
|
|
|
|
{
|
2010-10-01 00:09:22 +00:00
|
|
|
ep.SeasonNumber = Convert.ToInt32(match.Groups["seasonNumber"].Value);
|
2010-09-28 19:32:19 +00:00
|
|
|
ep.Proper = title.Contains("PROPER");
|
2010-10-01 00:09:22 +00:00
|
|
|
ep.Quality = QualityTypes.Unknown;
|
2010-09-28 19:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2010-09-28 05:01:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|