2010-10-04 04:00:50 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using NLog;
|
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
|
|
|
{
|
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
|
|
|
|
2011-04-10 05:44:01 +03:00
|
|
|
private static readonly Regex CleanUpRegex = new Regex(@"((\s|^)the(\s|$))|((\s|^)and(\s|$))|[^a-z]",
|
|
|
|
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
|
|
|
|
2010-09-23 06:19:47 +03:00
|
|
|
private readonly TvdbHandler _handler;
|
|
|
|
|
2010-09-28 07:25:41 +03:00
|
|
|
public TvDbProvider()
|
2010-09-23 06:19:47 +03:00
|
|
|
{
|
2010-10-05 09:21:18 +03:00
|
|
|
_handler = new TvdbHandler(new XmlCacheProvider(CentralDispatch.AppPath + @"\cache\tvdb"), TVDB_APIKEY);
|
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 TvdbSearchResult GetSeries(string title)
|
2010-09-23 06:19:47 +03:00
|
|
|
{
|
2011-04-20 10:44:13 +03:00
|
|
|
lock (_handler)
|
2011-03-10 22:30:22 +02:00
|
|
|
{
|
2011-04-20 10:44:13 +03:00
|
|
|
var searchResults = SearchSeries(title);
|
|
|
|
if (searchResults.Count == 0)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
foreach (var tvdbSearchResult in searchResults)
|
2011-03-10 22:30:22 +02:00
|
|
|
{
|
2011-04-20 10:44:13 +03:00
|
|
|
if (IsTitleMatch(tvdbSearchResult.SeriesName, title))
|
|
|
|
{
|
|
|
|
Logger.Debug("Search for '{0}' was successful", title);
|
|
|
|
return tvdbSearchResult;
|
|
|
|
}
|
2011-03-10 22:30:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2010-09-23 06:19:47 +03:00
|
|
|
}
|
|
|
|
|
2011-04-07 06:34:48 +03:00
|
|
|
public virtual int GetBestMatch(List<TvdbSearchResult> searchResults, string title)
|
2011-04-05 05:48:46 +03:00
|
|
|
{
|
|
|
|
if (searchResults.Count == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
foreach (var tvdbSearchResult in searchResults)
|
|
|
|
{
|
|
|
|
if (IsTitleMatch(tvdbSearchResult.SeriesName, title))
|
|
|
|
{
|
|
|
|
Logger.Debug("Search for '{0}' was successful", title);
|
|
|
|
return tvdbSearchResult.Id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return searchResults[0].Id;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
return _handler.GetSeries(id, TvdbLanguage.DefaultLanguage, loadEpisodes, false, false);
|
|
|
|
}
|
2010-09-23 06:19:47 +03:00
|
|
|
}
|
|
|
|
|
2010-10-04 04:00:50 +03:00
|
|
|
/// <summary>
|
2011-04-10 05:44:01 +03:00
|
|
|
/// Determines whether a title in a search result is equal to the title searched for.
|
2010-10-04 04:00:50 +03:00
|
|
|
/// </summary>
|
2011-04-10 05:44:01 +03:00
|
|
|
/// <param name = "directoryName">Name of the directory.</param>
|
|
|
|
/// <param name = "tvdbTitle">The TVDB title.</param>
|
2010-10-04 04:00:50 +03:00
|
|
|
/// <returns>
|
2011-04-10 05:44:01 +03:00
|
|
|
/// <c>true</c> if the titles are found to be same; otherwise, <c>false</c>.
|
2010-10-04 04:00:50 +03:00
|
|
|
/// </returns>
|
|
|
|
public static bool IsTitleMatch(string directoryName, string tvdbTitle)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2011-04-10 05:44:01 +03:00
|
|
|
else if (String.Equals(CleanUpRegex.Replace(directoryName, ""), CleanUpRegex.Replace(tvdbTitle, ""),
|
|
|
|
StringComparison.InvariantCultureIgnoreCase))
|
2010-10-04 04:00:50 +03:00
|
|
|
result = true;
|
|
|
|
|
|
|
|
Logger.Debug("Match between '{0}' and '{1}' was {2}", tvdbTitle, directoryName, result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2010-09-23 06:19:47 +03:00
|
|
|
}
|
|
|
|
}
|