2011-04-23 22:33:24 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
|
using SubSonic.Repository;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
|
|
|
|
public class EpisodeProvider
|
|
|
|
|
{
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-04-26 02:28:33 +02:00
|
|
|
|
private readonly IRepository _repository;
|
|
|
|
|
private readonly TvDbProvider _tvDbProvider;
|
|
|
|
|
|
2011-06-04 03:56:53 +02:00
|
|
|
|
public EpisodeProvider(IRepository repository, TvDbProvider tvDbProviderProvider)
|
2011-04-23 22:33:24 +02:00
|
|
|
|
{
|
2011-04-26 02:28:33 +02:00
|
|
|
|
_repository = repository;
|
|
|
|
|
_tvDbProvider = tvDbProviderProvider;
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EpisodeProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-04 03:56:53 +02:00
|
|
|
|
public virtual void AddEpisode(Episode episode)
|
2011-05-20 05:47:07 +02:00
|
|
|
|
{
|
2011-06-04 03:56:53 +02:00
|
|
|
|
_repository.Add(episode);
|
2011-05-20 05:47:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-23 22:33:24 +02:00
|
|
|
|
public virtual Episode GetEpisode(long id)
|
|
|
|
|
{
|
2011-04-26 02:28:33 +02:00
|
|
|
|
return _repository.Single<Episode>(id);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber)
|
|
|
|
|
{
|
|
|
|
|
return
|
2011-04-26 02:28:33 +02:00
|
|
|
|
_repository.Single<Episode>(
|
2011-04-23 22:33:24 +02:00
|
|
|
|
c => c.SeriesId == seriesId && c.SeasonNumber == seasonNumber && c.EpisodeNumber == episodeNumber);
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-25 23:23:02 +02:00
|
|
|
|
public virtual Episode GetEpisode(int seriesId, DateTime date)
|
|
|
|
|
{
|
|
|
|
|
return
|
2011-04-26 02:28:33 +02:00
|
|
|
|
_repository.Single<Episode>(
|
2011-04-25 23:23:02 +02:00
|
|
|
|
c => c.SeriesId == seriesId && c.AirDate == date.Date);
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-05 09:25:50 +02:00
|
|
|
|
public virtual IQueryable<Episode> GetEpisodeBySeries(long seriesId)
|
2011-04-23 22:33:24 +02:00
|
|
|
|
{
|
2011-06-05 09:25:50 +02:00
|
|
|
|
return _repository.All<Episode>().Where(e => e.SeriesId == seriesId);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-04 03:56:53 +02:00
|
|
|
|
public virtual IList<Episode> GetEpisodesBySeason(long seriesId, int seasonNumber)
|
2011-04-23 22:33:24 +02:00
|
|
|
|
{
|
2011-06-04 03:56:53 +02:00
|
|
|
|
return _repository.Find<Episode>(e => e.SeriesId == seriesId && e.SeasonNumber == seasonNumber);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-01 10:04:44 +02:00
|
|
|
|
public virtual IList<Episode> EpisodesWithoutFiles(bool includeSpecials)
|
|
|
|
|
{
|
|
|
|
|
if (includeSpecials)
|
2011-06-02 01:18:08 +02:00
|
|
|
|
return _repository.All<Episode>().Where(e => e.EpisodeFileId == 0 && e.AirDate <= DateTime.Today && e.AirDate > new DateTime(1899, 12, 31)).ToList();
|
2011-05-01 10:04:44 +02:00
|
|
|
|
|
2011-06-02 01:18:08 +02:00
|
|
|
|
return _repository.All<Episode>().Where(e => e.EpisodeFileId == 0 && e.AirDate <= DateTime.Today && e.AirDate > new DateTime(1899, 12, 31) && e.SeasonNumber > 0).ToList();
|
2011-05-01 10:04:44 +02:00
|
|
|
|
}
|
2011-04-23 22:33:24 +02:00
|
|
|
|
|
2011-05-29 05:01:35 +02:00
|
|
|
|
|
|
|
|
|
public virtual void RefreshEpisodeInfo(Series series)
|
2011-04-23 22:33:24 +02:00
|
|
|
|
{
|
2011-05-29 05:01:35 +02:00
|
|
|
|
Logger.Info("Starting episode info refresh for series:{0}", series.SeriesId);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
int successCount = 0;
|
|
|
|
|
int failCount = 0;
|
2011-05-29 05:01:35 +02:00
|
|
|
|
var tvDbSeriesInfo = _tvDbProvider.GetSeries(series.SeriesId, true);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
|
|
|
|
|
var updateList = new List<Episode>();
|
|
|
|
|
var newList = new List<Episode>();
|
|
|
|
|
|
2011-05-29 05:01:35 +02:00
|
|
|
|
foreach (var episode in tvDbSeriesInfo.Episodes)
|
2011-04-23 22:33:24 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//DateTime throws an error in SQLServer per message below:
|
|
|
|
|
//SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
|
|
|
|
|
//So lets hack it so it works for SQLServer (as well as SQLite), perhaps we can find a better solution
|
|
|
|
|
//Todo: Fix this hack
|
|
|
|
|
if (episode.FirstAired < new DateTime(1753, 1, 1))
|
|
|
|
|
episode.FirstAired = new DateTime(1753, 1, 1);
|
|
|
|
|
|
2011-05-29 05:01:35 +02:00
|
|
|
|
Logger.Trace("Updating info for [{0}] - S{1}E{2}", tvDbSeriesInfo.SeriesName, episode.SeasonNumber, episode.EpisodeNumber);
|
|
|
|
|
|
2011-05-29 05:37:19 +02:00
|
|
|
|
//first check using tvdbId, this should cover cases when and episode number in a season is changed
|
|
|
|
|
var episodeToUpdate = _repository.Single<Episode>(c => c.TvDbEpisodeId == episode.Id);
|
2011-05-29 05:01:35 +02:00
|
|
|
|
|
2011-05-29 05:37:19 +02:00
|
|
|
|
//not found, try using season/episode number
|
|
|
|
|
if (episodeToUpdate == null)
|
|
|
|
|
{
|
|
|
|
|
episodeToUpdate = GetEpisode(series.SeriesId, episode.SeasonNumber, episode.EpisodeNumber);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Episode doesn't exist locally
|
2011-05-29 05:01:35 +02:00
|
|
|
|
if (episodeToUpdate == null)
|
2011-04-23 22:33:24 +02:00
|
|
|
|
{
|
2011-05-29 05:01:35 +02:00
|
|
|
|
episodeToUpdate = new Episode();
|
|
|
|
|
newList.Add(episodeToUpdate);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-05-29 05:01:35 +02:00
|
|
|
|
updateList.Add(episodeToUpdate);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-29 05:01:35 +02:00
|
|
|
|
episodeToUpdate.SeriesId = series.SeriesId;
|
|
|
|
|
episodeToUpdate.TvDbEpisodeId = episode.Id;
|
|
|
|
|
episodeToUpdate.AirDate = episode.FirstAired.Date;
|
|
|
|
|
episodeToUpdate.EpisodeNumber = episode.EpisodeNumber;
|
|
|
|
|
episodeToUpdate.SeasonNumber = episode.SeasonNumber;
|
|
|
|
|
episodeToUpdate.Title = episode.EpisodeName;
|
|
|
|
|
episodeToUpdate.Overview = episode.Overview;
|
|
|
|
|
|
2011-04-23 22:33:24 +02:00
|
|
|
|
successCount++;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.FatalException(
|
2011-05-29 05:01:35 +02:00
|
|
|
|
String.Format("An error has occurred while updating episode info for series {0}", series.SeriesId), e);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
failCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-26 02:28:33 +02:00
|
|
|
|
_repository.AddMany(newList);
|
|
|
|
|
_repository.UpdateMany(updateList);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
|
|
|
|
|
Logger.Debug("Finished episode refresh for series:{0}. Successful:{1} - Failed:{2} ",
|
2011-05-29 05:01:35 +02:00
|
|
|
|
tvDbSeriesInfo.SeriesName, successCount, failCount);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void DeleteEpisode(int episodeId)
|
|
|
|
|
{
|
2011-04-26 02:28:33 +02:00
|
|
|
|
_repository.Delete<Episode>(episodeId);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void UpdateEpisode(Episode episode)
|
|
|
|
|
{
|
2011-04-26 02:28:33 +02:00
|
|
|
|
_repository.Update(episode);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
2011-06-04 03:56:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public virtual bool IsIgnored(int seriesId, int seasonNumber)
|
|
|
|
|
{
|
2011-06-05 08:24:24 +02:00
|
|
|
|
return !_repository.Exists<Episode>(e => e.SeriesId == seriesId && e.SeasonNumber == seasonNumber && !e.Ignored);
|
2011-06-04 03:56:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual IList<int> GetSeasons(int seriesId)
|
|
|
|
|
{
|
|
|
|
|
return _repository.All<Episode>().Where(e => e.SeriesId == seriesId).Select(s => s.SeasonNumber).Distinct().OrderBy(c => c).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void SetSeasonIgnore(long seriesId, int seasonNumber, bool isIgnored)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Setting ignore flag on Series:{0} Season:{1} to {2}", seriesId, seasonNumber, isIgnored);
|
|
|
|
|
var episodes = GetEpisodesBySeason(seriesId, seasonNumber);
|
|
|
|
|
|
|
|
|
|
foreach (var episode in episodes)
|
|
|
|
|
{
|
|
|
|
|
episode.Ignored = isIgnored;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_repository.UpdateMany(episodes);
|
|
|
|
|
|
|
|
|
|
Logger.Info("Ignore flag for Series:{0} Season:{1} successfully set to {2}", seriesId, seasonNumber, isIgnored);
|
|
|
|
|
}
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
2011-04-23 00:24:05 +02:00
|
|
|
|
} |