2010-10-04 04:00:50 +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 System.Text.RegularExpressions;
|
|
|
|
using NLog;
|
2011-10-21 08:04:26 +03:00
|
|
|
using Ninject;
|
2011-10-29 07:54:33 +03:00
|
|
|
using NzbDrone.Common;
|
2010-09-23 06:19:47 +03:00
|
|
|
using TvdbLib;
|
|
|
|
using TvdbLib.Cache;
|
|
|
|
using TvdbLib.Data;
|
|
|
|
|
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;
|
2011-04-10 05:44:01 +03:00
|
|
|
private const string TVDB_APIKEY = "5D2D188E86E07F4F";
|
2011-01-29 08:10:22 +02:00
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2010-10-04 04:00:50 +03:00
|
|
|
|
2010-09-23 06:19:47 +03:00
|
|
|
private readonly TvdbHandler _handler;
|
|
|
|
|
2011-10-21 08:04:26 +03:00
|
|
|
[Inject]
|
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;
|
|
|
|
_handler = new TvdbHandler(new XmlCacheProvider(_environmentProvider.GetCacheFolder()), 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
|
|
|
}
|
|
|
|
|
2011-04-07 06:34:48 +03:00
|
|
|
public virtual IList<TvdbSearchResult> SearchSeries(string title)
|
2010-10-04 04:00:50 +03:00
|
|
|
{
|
2011-04-20 10:44:13 +03:00
|
|
|
lock (_handler)
|
|
|
|
{
|
|
|
|
Logger.Debug("Searching TVDB for '{0}'", title);
|
|
|
|
|
|
|
|
var result = _handler.SearchSeries(title);
|
2010-10-04 04:00:50 +03:00
|
|
|
|
2011-04-20 10:44:13 +03:00
|
|
|
Logger.Debug("Search for '{0}' returned {1} possible results", title, result.Count);
|
|
|
|
return result;
|
|
|
|
}
|
2010-10-04 04:00:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-07 06:34:48 +03:00
|
|
|
public virtual TvdbSeries GetSeries(int id, bool loadEpisodes)
|
2010-09-23 06:19:47 +03:00
|
|
|
{
|
2011-04-20 10:44:13 +03:00
|
|
|
lock (_handler)
|
|
|
|
{
|
|
|
|
Logger.Debug("Fetching SeriesId'{0}' from tvdb", id);
|
2011-06-21 08:44:01 +03:00
|
|
|
var result = _handler.GetSeries(id, TvdbLanguage.DefaultLanguage, loadEpisodes, false, true, true);
|
2011-05-29 04:58:35 +03:00
|
|
|
|
|
|
|
//Fix American Dad's scene gongshow
|
|
|
|
if (result != null && result.Id == 73141)
|
|
|
|
{
|
2011-07-03 02:12:20 +03:00
|
|
|
result.Episodes = result.Episodes.Where(e => e.SeasonNumber == 0 || e.EpisodeNumber > 0).ToList();
|
|
|
|
|
2011-06-13 07:24:57 +03:00
|
|
|
var seasonOneEpisodeCount = result.Episodes.Where(e => e.SeasonNumber == 1).Count();
|
2011-05-29 04:58:35 +03:00
|
|
|
var seasonOneId = result.Episodes.Where(e => e.SeasonNumber == 1).First().SeasonId;
|
|
|
|
|
|
|
|
foreach (var episode in result.Episodes)
|
|
|
|
{
|
|
|
|
if (episode.SeasonNumber > 1)
|
|
|
|
{
|
|
|
|
if (episode.SeasonNumber == 2)
|
|
|
|
{
|
|
|
|
episode.EpisodeNumber = episode.EpisodeNumber + seasonOneEpisodeCount;
|
|
|
|
episode.SeasonId = seasonOneId;
|
|
|
|
}
|
|
|
|
|
|
|
|
episode.SeasonNumber = episode.SeasonNumber - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-19 20:56:54 +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());
|
|
|
|
|
|
|
|
result.Episodes = episodes.ToList();
|
|
|
|
|
2011-05-29 04:58:35 +03:00
|
|
|
return result;
|
2011-04-20 10:44:13 +03:00
|
|
|
}
|
2010-09-23 06:19:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|