2010-10-04 01:00:50 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2010-09-23 03:19:47 +00:00
|
|
|
|
using System.IO;
|
2010-10-04 01:00:50 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using NLog;
|
2010-09-23 03:19:47 +00:00
|
|
|
|
using TvdbLib;
|
|
|
|
|
using TvdbLib.Cache;
|
|
|
|
|
using TvdbLib.Data;
|
|
|
|
|
|
2010-09-28 04:25:41 +00:00
|
|
|
|
namespace NzbDrone.Core.Providers
|
2010-09-23 03:19:47 +00:00
|
|
|
|
{
|
2010-09-28 04:25:41 +00:00
|
|
|
|
public class TvDbProvider : ITvDbProvider
|
2010-09-23 03:19:47 +00:00
|
|
|
|
{
|
2011-01-29 06:10:22 +00:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2010-10-04 01:00:50 +00:00
|
|
|
|
private static readonly Regex CleanUpRegex = new Regex(@"((\s|^)the(\s|$))|((\s|^)and(\s|$))|[^a-z]", RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
|
|
|
|
|
2010-09-28 06:09:24 +00:00
|
|
|
|
private const string TVDB_APIKEY = "5D2D188E86E07F4F";
|
2010-09-23 03:19:47 +00:00
|
|
|
|
private readonly TvdbHandler _handler;
|
|
|
|
|
|
2010-09-28 04:25:41 +00:00
|
|
|
|
public TvDbProvider()
|
2010-09-23 03:19:47 +00:00
|
|
|
|
{
|
2010-10-05 06:21:18 +00:00
|
|
|
|
_handler = new TvdbHandler(new XmlCacheProvider(CentralDispatch.AppPath + @"\cache\tvdb"), TVDB_APIKEY);
|
2010-09-23 03:19:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-28 04:25:41 +00:00
|
|
|
|
#region ITvDbProvider Members
|
2010-09-23 03:19:47 +00:00
|
|
|
|
|
2010-10-04 01:00:50 +00:00
|
|
|
|
public IList<TvdbSearchResult> SearchSeries(string title)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Searching TVDB for '{0}'", title);
|
|
|
|
|
var result = new List<TvdbSearchResult>();
|
|
|
|
|
|
|
|
|
|
foreach (var tvdbSearchResult in _handler.SearchSeries(title))
|
|
|
|
|
{
|
|
|
|
|
if (IsTitleMatch(tvdbSearchResult.SeriesName, title))
|
|
|
|
|
{
|
|
|
|
|
result.Add(tvdbSearchResult);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-12 02:49:27 +00:00
|
|
|
|
Logger.Debug("Search for '{0}' returned {1} results", title, result.Count);
|
2010-10-04 01:00:50 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public TvdbSearchResult GetSeries(string title)
|
2010-09-23 03:19:47 +00:00
|
|
|
|
{
|
2010-10-05 06:21:18 +00:00
|
|
|
|
var searchResults = SearchSeries(title);
|
|
|
|
|
if (searchResults.Count == 0)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return searchResults[0];
|
2010-09-23 03:19:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-05 06:21:18 +00:00
|
|
|
|
public TvdbSeries GetSeries(int id, bool loadEpisodes)
|
2010-09-23 03:19:47 +00:00
|
|
|
|
{
|
2010-10-05 06:21:18 +00:00
|
|
|
|
Logger.Debug("Fetching SeriesId'{0}' from tvdb", id);
|
|
|
|
|
return _handler.GetSeries(id, TvdbLanguage.DefaultLanguage, loadEpisodes, false, false);
|
2010-09-23 03:19:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-04 01:00:50 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether a title in a search result is equal to the title searched for.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="directoryName">Name of the directory.</param>
|
|
|
|
|
/// <param name="tvdbTitle">The TVDB title.</param>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// <c>true</c> if the titles are found to be same; otherwise, <c>false</c>.
|
|
|
|
|
/// </returns>
|
|
|
|
|
public static bool IsTitleMatch(string directoryName, string tvdbTitle)
|
|
|
|
|
{
|
2010-10-17 17:22:48 +00:00
|
|
|
|
|
2010-10-04 01:00:50 +00:00
|
|
|
|
|
|
|
|
|
var result = false;
|
|
|
|
|
|
|
|
|
|
if (String.IsNullOrEmpty(directoryName))
|
|
|
|
|
throw new ArgumentException("directoryName");
|
|
|
|
|
if (String.IsNullOrEmpty(tvdbTitle))
|
|
|
|
|
throw new ArgumentException("tvdbTitle");
|
|
|
|
|
|
|
|
|
|
if (String.Equals(directoryName, tvdbTitle, StringComparison.CurrentCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
result = true;
|
|
|
|
|
}
|
|
|
|
|
else if (String.Equals(CleanUpRegex.Replace(directoryName, ""), CleanUpRegex.Replace(tvdbTitle, ""), StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
result = true;
|
|
|
|
|
|
|
|
|
|
Logger.Debug("Match between '{0}' and '{1}' was {2}", tvdbTitle, directoryName, result);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-23 03:19:47 +00:00
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|