2013-02-26 05:20:42 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2011-06-18 04:46:22 +03:00
|
|
|
using System.Linq;
|
2010-10-04 04:00:50 +03:00
|
|
|
using NLog;
|
2011-10-29 07:54:33 +03:00
|
|
|
using NzbDrone.Common;
|
2013-02-26 05:20:42 +03:00
|
|
|
using NzbDrone.Core.Tv;
|
2012-10-17 03:08:47 +03:00
|
|
|
using TvdbLib;
|
|
|
|
using TvdbLib.Cache;
|
|
|
|
using TvdbLib.Data;
|
2013-01-31 03:42:11 +03:00
|
|
|
using TvdbLanguage = TvdbLib.Data.TvdbLanguage;
|
2010-09-23 06:19:47 +03:00
|
|
|
|
2010-09-28 07:25:41 +03:00
|
|
|
namespace NzbDrone.Core.Providers
|
2010-09-23 06:19:47 +03:00
|
|
|
{
|
2011-04-07 06:34:48 +03:00
|
|
|
public class TvDbProvider
|
2010-09-23 06:19:47 +03:00
|
|
|
{
|
2012-03-07 05:59:43 +03:00
|
|
|
private readonly EnvironmentProvider _environmentProvider;
|
2012-07-10 07:37:24 +03:00
|
|
|
public const string TVDB_APIKEY = "5D2D188E86E07F4F";
|
2013-01-31 03:42:11 +03:00
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
|
2010-10-04 04:00:50 +03:00
|
|
|
|
2012-10-17 03:08:47 +03:00
|
|
|
private readonly TvdbHandler _handler;
|
2013-01-31 03:42:11 +03:00
|
|
|
private readonly Tvdb.Tvdb _handlerV2;
|
|
|
|
|
2010-09-23 06:19:47 +03:00
|
|
|
|
2012-03-07 05:59:43 +03:00
|
|
|
public TvDbProvider(EnvironmentProvider environmentProvider)
|
2011-10-21 08:04:26 +03:00
|
|
|
{
|
2012-03-07 05:59:43 +03:00
|
|
|
_environmentProvider = environmentProvider;
|
2012-10-17 03:08:47 +03:00
|
|
|
_handler = new TvdbHandler(new XmlCacheProvider(_environmentProvider.GetCacheFolder()), TVDB_APIKEY);
|
2013-01-31 03:42:11 +03:00
|
|
|
_handlerV2 = new Tvdb.Tvdb(TVDB_APIKEY);
|
2011-10-21 08:04:26 +03:00
|
|
|
}
|
|
|
|
|
2010-09-28 07:25:41 +03:00
|
|
|
public TvDbProvider()
|
2010-09-23 06:19:47 +03:00
|
|
|
{
|
2011-11-03 08:04:14 +03:00
|
|
|
|
2010-09-23 06:19:47 +03:00
|
|
|
}
|
|
|
|
|
2013-02-26 06:58:57 +03:00
|
|
|
public virtual List<Series> SearchSeries(string title)
|
2010-10-04 04:00:50 +03:00
|
|
|
{
|
2013-01-31 03:42:11 +03:00
|
|
|
logger.Debug("Searching TVDB for '{0}'", title);
|
2011-04-20 10:44:13 +03:00
|
|
|
|
2013-01-31 03:42:11 +03:00
|
|
|
if (title.Contains(" & "))
|
|
|
|
{
|
|
|
|
title = title.Replace(" & ", " ");
|
|
|
|
}
|
2012-12-21 00:27:54 +03:00
|
|
|
|
2013-02-26 06:58:57 +03:00
|
|
|
var searchResult = _handlerV2.SearchSeries(title).Select(tvdbSeriesSearchItem => new Series
|
|
|
|
{
|
|
|
|
TvDbId = tvdbSeriesSearchItem.seriesid,
|
|
|
|
Title = tvdbSeriesSearchItem.SeriesName,
|
|
|
|
FirstAired = tvdbSeriesSearchItem.FirstAired,
|
|
|
|
Overview = tvdbSeriesSearchItem.Overview
|
|
|
|
}).ToList();
|
2010-10-04 04:00:50 +03:00
|
|
|
|
2013-02-26 06:58:57 +03:00
|
|
|
logger.Debug("Search for '{0}' returned {1} possible results", title, searchResult.Count);
|
|
|
|
return searchResult;
|
2010-10-04 04:00:50 +03:00
|
|
|
}
|
|
|
|
|
2013-02-26 06:58:57 +03:00
|
|
|
public virtual TvdbSeries GetSeries(int tvDbSeriesId, bool loadEpisodes, bool loadActors = false)
|
2010-09-23 06:19:47 +03:00
|
|
|
{
|
2012-10-17 03:08:47 +03:00
|
|
|
lock (_handler)
|
|
|
|
{
|
2013-02-26 06:58:57 +03:00
|
|
|
logger.Debug("Fetching SeriesId'{0}' from tvdb", tvDbSeriesId);
|
|
|
|
var result = _handler.GetSeries(tvDbSeriesId, TvdbLanguage.DefaultLanguage, loadEpisodes, loadActors, true, true);
|
2011-05-29 04:58:35 +03:00
|
|
|
|
2012-10-17 03:08:47 +03:00
|
|
|
//Remove duplicated episodes
|
|
|
|
var episodes = result.Episodes.OrderByDescending(e => e.FirstAired).ThenByDescending(e => e.EpisodeName)
|
|
|
|
.GroupBy(e => e.SeriesId.ToString("000000") + e.SeasonNumber.ToString("000") + e.EpisodeNumber.ToString("000"))
|
|
|
|
.Select(e => e.First());
|
|
|
|
|
2013-02-26 05:20:42 +03:00
|
|
|
result.Episodes = episodes.Where(episode => !string.IsNullOrWhiteSpace(episode.EpisodeName) || (episode.FirstAired < DateTime.Now.AddDays(2) && episode.FirstAired.Year > 1900)).ToList();
|
2012-10-17 03:08:47 +03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2010-09-23 06:19:47 +03:00
|
|
|
}
|
2013-02-26 05:20:42 +03:00
|
|
|
|
|
|
|
public virtual IList<Episode> GetEpisodes(int tvDbSeriesId)
|
|
|
|
{
|
|
|
|
var series = GetSeries(tvDbSeriesId, true);
|
|
|
|
|
|
|
|
var episodes = new List<Episode>();
|
|
|
|
|
|
|
|
foreach (var tvDbEpisode in series.Episodes)
|
|
|
|
{
|
|
|
|
var episode = new Episode();
|
|
|
|
|
|
|
|
episode.TvDbEpisodeId = tvDbEpisode.Id;
|
|
|
|
episode.EpisodeNumber = tvDbEpisode.EpisodeNumber;
|
|
|
|
episode.SeasonNumber = tvDbEpisode.SeasonNumber;
|
|
|
|
episode.AbsoluteEpisodeNumber = tvDbEpisode.AbsoluteNumber;
|
|
|
|
episode.Title = tvDbEpisode.EpisodeName;
|
|
|
|
episode.Overview = tvDbEpisode.Overview;
|
|
|
|
|
|
|
|
if (tvDbEpisode.FirstAired.Year > 1900)
|
|
|
|
{
|
|
|
|
episode.AirDate = tvDbEpisode.FirstAired.Date;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
episode.AirDate = null;
|
|
|
|
}
|
2013-02-26 09:47:30 +03:00
|
|
|
|
|
|
|
episodes.Add(episode);
|
2013-02-26 05:20:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return episodes;
|
|
|
|
}
|
2010-09-23 06:19:47 +03:00
|
|
|
}
|
|
|
|
}
|