2010-10-04 03:00:50 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2011-06-18 03:46:22 +02:00
|
|
|
|
using System.Linq;
|
2010-10-04 03:00:50 +02:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using NLog;
|
2011-10-21 07:04:26 +02:00
|
|
|
|
using Ninject;
|
2011-10-29 06:54:33 +02:00
|
|
|
|
using NzbDrone.Common;
|
2012-10-13 05:49:59 +02:00
|
|
|
|
using XemLib;
|
|
|
|
|
using XemLib.Data;
|
2010-09-23 05:19:47 +02:00
|
|
|
|
|
2010-09-28 06:25:41 +02:00
|
|
|
|
namespace NzbDrone.Core.Providers
|
2010-09-23 05:19:47 +02:00
|
|
|
|
{
|
2011-04-07 05:34:48 +02:00
|
|
|
|
public class TvDbProvider
|
2010-09-23 05:19:47 +02:00
|
|
|
|
{
|
2012-03-07 03:59:43 +01:00
|
|
|
|
private readonly EnvironmentProvider _environmentProvider;
|
2012-07-10 06:37:24 +02:00
|
|
|
|
public const string TVDB_APIKEY = "5D2D188E86E07F4F";
|
2011-01-29 07:10:22 +01:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2010-10-04 03:00:50 +02:00
|
|
|
|
|
2012-10-13 05:49:59 +02:00
|
|
|
|
private readonly XemClient _xemClient;
|
2010-09-23 05:19:47 +02:00
|
|
|
|
|
2011-10-21 07:04:26 +02:00
|
|
|
|
[Inject]
|
2012-03-07 03:59:43 +01:00
|
|
|
|
public TvDbProvider(EnvironmentProvider environmentProvider)
|
2011-10-21 07:04:26 +02:00
|
|
|
|
{
|
2012-03-07 03:59:43 +01:00
|
|
|
|
_environmentProvider = environmentProvider;
|
2012-10-13 05:49:59 +02:00
|
|
|
|
_xemClient = new XemClient(TVDB_APIKEY);
|
2011-10-21 07:04:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-28 06:25:41 +02:00
|
|
|
|
public TvDbProvider()
|
2010-09-23 05:19:47 +02:00
|
|
|
|
{
|
2011-11-03 06:04:14 +01:00
|
|
|
|
|
2010-09-23 05:19:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-07 05:34:48 +02:00
|
|
|
|
public virtual IList<TvdbSearchResult> SearchSeries(string title)
|
2010-10-04 03:00:50 +02:00
|
|
|
|
{
|
2012-10-13 05:49:59 +02:00
|
|
|
|
Logger.Debug("Searching TVDB for '{0}'", title);
|
2011-04-20 09:44:13 +02:00
|
|
|
|
|
2012-10-13 05:49:59 +02:00
|
|
|
|
var result = _xemClient.SearchSeries(title);
|
2010-10-04 03:00:50 +02:00
|
|
|
|
|
2012-10-13 05:49:59 +02:00
|
|
|
|
Logger.Debug("Search for '{0}' returned {1} possible results", title, result.Count);
|
|
|
|
|
return result;
|
2010-10-04 03:00:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-13 05:49:59 +02:00
|
|
|
|
public virtual TvdbSeries GetSeries(int id, bool loadEpisodes, bool loadActors = false, bool loadBanners = false)
|
2010-09-23 05:19:47 +02:00
|
|
|
|
{
|
2012-10-13 05:49:59 +02:00
|
|
|
|
Logger.Debug("Fetching SeriesId'{0}' from tvdb", id);
|
|
|
|
|
var result = _xemClient.GetSeries(id, loadEpisodes, loadActors, true, TvdbLanguage.Default);
|
2011-05-29 03:58:35 +02:00
|
|
|
|
|
2012-10-13 05:49:59 +02: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());
|
2011-07-03 01:12:20 +02:00
|
|
|
|
|
2012-10-13 05:49:59 +02:00
|
|
|
|
result.Episodes = episodes.ToList();
|
2011-05-29 03:58:35 +02:00
|
|
|
|
|
2012-10-13 05:49:59 +02:00
|
|
|
|
return result;
|
2010-09-23 05:19:47 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|