2011-04-22 04:23:31 +02:00
|
|
|
using System;
|
2011-04-25 00:32:08 +02:00
|
|
|
using System.Collections.Generic;
|
2011-09-04 05:05:44 +02:00
|
|
|
using System.Linq;
|
2011-04-25 22:21:52 +02:00
|
|
|
using System.Net;
|
2011-04-21 03:29:41 +02:00
|
|
|
using System.ServiceModel.Syndication;
|
2011-07-04 00:32:36 +02:00
|
|
|
using System.Text.RegularExpressions;
|
2011-06-14 03:23:04 +02:00
|
|
|
using Ninject;
|
2011-04-04 05:50:12 +02:00
|
|
|
using NLog;
|
2012-02-11 01:48:20 +01:00
|
|
|
using NzbDrone.Common;
|
2011-04-04 05:50:12 +02:00
|
|
|
using NzbDrone.Core.Model;
|
2011-04-04 09:21:07 +02:00
|
|
|
using NzbDrone.Core.Providers.Core;
|
2011-04-04 05:50:12 +02:00
|
|
|
|
2011-04-19 02:12:06 +02:00
|
|
|
namespace NzbDrone.Core.Providers.Indexer
|
2011-04-04 05:50:12 +02:00
|
|
|
{
|
2011-05-20 06:21:18 +02:00
|
|
|
public abstract class IndexerBase
|
2011-04-04 05:50:12 +02:00
|
|
|
{
|
2011-04-22 08:23:29 +02:00
|
|
|
protected readonly Logger _logger;
|
2011-04-07 04:25:52 +02:00
|
|
|
private readonly HttpProvider _httpProvider;
|
2011-05-20 05:47:07 +02:00
|
|
|
protected readonly ConfigProvider _configProvider;
|
2011-04-19 02:12:06 +02:00
|
|
|
|
2011-07-04 00:32:36 +02:00
|
|
|
private static readonly Regex TitleSearchRegex = new Regex(@"[\W]", RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
2012-02-27 06:33:24 +01:00
|
|
|
protected static readonly Regex RemoveThe = new Regex(@"^the\s", RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
2011-07-04 00:32:36 +02:00
|
|
|
|
2011-06-14 03:23:04 +02:00
|
|
|
[Inject]
|
2011-05-26 06:25:59 +02:00
|
|
|
protected IndexerBase(HttpProvider httpProvider, ConfigProvider configProvider)
|
2011-04-04 08:53:22 +02:00
|
|
|
{
|
2011-04-05 07:30:13 +02:00
|
|
|
_httpProvider = httpProvider;
|
2011-05-20 05:47:07 +02:00
|
|
|
_configProvider = configProvider;
|
2011-05-01 10:04:44 +02:00
|
|
|
|
2011-04-29 08:32:51 +02:00
|
|
|
_logger = LogManager.GetLogger(GetType().ToString());
|
2011-04-04 08:53:22 +02:00
|
|
|
}
|
|
|
|
|
2011-05-27 05:54:28 +02:00
|
|
|
public IndexerBase()
|
|
|
|
{
|
2011-05-27 08:03:57 +02:00
|
|
|
|
2011-05-27 05:54:28 +02:00
|
|
|
}
|
|
|
|
|
2011-04-04 05:50:12 +02:00
|
|
|
/// <summary>
|
2011-04-19 02:12:06 +02:00
|
|
|
/// Gets the name for the feed
|
2011-04-04 05:50:12 +02:00
|
|
|
/// </summary>
|
2011-04-19 02:12:06 +02:00
|
|
|
public abstract string Name { get; }
|
2011-04-04 05:50:12 +02:00
|
|
|
|
2011-04-04 08:53:22 +02:00
|
|
|
/// <summary>
|
2011-04-21 03:26:13 +02:00
|
|
|
/// Gets the source URL for the feed
|
2011-04-04 08:53:22 +02:00
|
|
|
/// </summary>
|
2011-04-21 03:26:13 +02:00
|
|
|
protected abstract string[] Urls { get; }
|
2011-04-04 08:53:22 +02:00
|
|
|
|
2012-02-01 02:37:36 +01:00
|
|
|
public abstract bool IsConfigured { get; }
|
|
|
|
|
2012-04-15 00:33:58 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Should the indexer be enabled by default?
|
|
|
|
/// </summary>
|
|
|
|
public virtual bool EnabledByDefault
|
|
|
|
{
|
|
|
|
get { return false; }
|
|
|
|
}
|
2012-02-01 02:37:36 +01:00
|
|
|
|
2011-05-27 04:12:28 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the credential.
|
|
|
|
/// </summary>
|
|
|
|
protected virtual NetworkCredential Credentials
|
|
|
|
{
|
|
|
|
get { return null; }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-29 07:49:38 +01:00
|
|
|
protected abstract IList<String> GetEpisodeSearchUrls(string seriesTitle, int seasonNumber, int episodeNumber);
|
|
|
|
protected abstract IList<String> GetDailyEpisodeSearchUrls(string seriesTitle, DateTime date);
|
|
|
|
protected abstract IList<String> GetSeasonSearchUrls(string seriesTitle, int seasonNumber);
|
|
|
|
protected abstract IList<String> GetPartialSeasonSearchUrls(string seriesTitle, int seasonNumber, int episodeWildcard);
|
2011-05-26 06:25:59 +02:00
|
|
|
|
2011-04-25 22:21:52 +02:00
|
|
|
/// <summary>
|
2011-05-27 04:12:28 +02:00
|
|
|
/// This method can be overwritten to provide indexer specific info parsing
|
2011-04-25 22:21:52 +02:00
|
|
|
/// </summary>
|
2011-05-27 04:12:28 +02:00
|
|
|
/// <param name="item">RSS item that needs to be parsed</param>
|
|
|
|
/// <param name="currentResult">Result of the built in parse function.</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
protected virtual EpisodeParseResult CustomParser(SyndicationItem item, EpisodeParseResult currentResult)
|
2011-04-25 22:21:52 +02:00
|
|
|
{
|
2011-05-27 04:12:28 +02:00
|
|
|
return currentResult;
|
2011-04-25 22:21:52 +02:00
|
|
|
}
|
|
|
|
|
2012-04-15 01:37:36 +02:00
|
|
|
/// <summary>
|
|
|
|
/// This method can be overwritten to provide pre-parse the title
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">RSS item that needs to be parsed</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
protected virtual string TitlePreParser(SyndicationItem item)
|
|
|
|
{
|
|
|
|
return item.Title.Text;
|
|
|
|
}
|
|
|
|
|
2011-05-27 04:12:28 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Generates direct link to download an NZB
|
|
|
|
/// </summary>
|
|
|
|
/// <param name = "item">RSS Feed item to generate the link for</param>
|
|
|
|
/// <returns>Download link URL</returns>
|
|
|
|
protected abstract string NzbDownloadUrl(SyndicationItem item);
|
2011-04-04 08:53:22 +02:00
|
|
|
|
2012-05-02 21:02:39 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Generates link to the NZB info at the indexer
|
|
|
|
/// </summary>
|
|
|
|
/// <param name = "item">RSS Feed item to generate the link for</param>
|
|
|
|
/// <returns>Nzb Info URL</returns>
|
|
|
|
protected abstract string NzbInfoUrl(SyndicationItem item);
|
|
|
|
|
2011-04-04 08:53:22 +02:00
|
|
|
/// <summary>
|
2011-04-10 04:44:01 +02:00
|
|
|
/// Fetches RSS feed and process each news item.
|
2011-04-04 08:53:22 +02:00
|
|
|
/// </summary>
|
2011-05-27 05:54:28 +02:00
|
|
|
public virtual IList<EpisodeParseResult> FetchRss()
|
2011-04-04 05:50:12 +02:00
|
|
|
{
|
2011-05-26 06:25:59 +02:00
|
|
|
_logger.Debug("Fetching feeds from " + Name);
|
2011-05-20 05:47:07 +02:00
|
|
|
|
|
|
|
var result = new List<EpisodeParseResult>();
|
2011-04-04 05:50:12 +02:00
|
|
|
|
2011-12-08 04:54:31 +01:00
|
|
|
|
|
|
|
result = Fetch(Urls);
|
|
|
|
|
2011-05-26 06:25:59 +02:00
|
|
|
|
2012-02-22 05:43:19 +01:00
|
|
|
_logger.Debug("Finished processing feeds from " + Name);
|
2011-05-26 06:25:59 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-08-28 07:45:36 +02:00
|
|
|
public virtual IList<EpisodeParseResult> FetchSeason(string seriesTitle, int seasonNumber)
|
|
|
|
{
|
2012-02-25 20:57:56 +01:00
|
|
|
_logger.Debug("Searching {0} for {1} Season {2}", Name, seriesTitle, seasonNumber);
|
2011-08-28 07:45:36 +02:00
|
|
|
|
2011-11-29 07:49:38 +01:00
|
|
|
var searchUrls = GetSeasonSearchUrls(GetQueryTitle(seriesTitle), seasonNumber);
|
2011-12-08 04:54:31 +01:00
|
|
|
var result = Fetch(searchUrls);
|
2011-08-28 07:45:36 +02:00
|
|
|
|
2012-02-25 20:57:56 +01:00
|
|
|
_logger.Info("Finished searching {0} for {1} Season {2}, Found {3}", Name, seriesTitle, seasonNumber, result.Count);
|
2011-08-28 07:45:36 +02:00
|
|
|
return result;
|
|
|
|
}
|
2011-05-26 06:25:59 +02:00
|
|
|
|
2011-09-01 08:58:54 +02:00
|
|
|
public virtual IList<EpisodeParseResult> FetchPartialSeason(string seriesTitle, int seasonNumber, int episodePrefix)
|
|
|
|
{
|
2012-02-25 20:57:56 +01:00
|
|
|
_logger.Debug("Searching {0} for {1} Season {2}, Prefix: {3}", Name, seriesTitle, seasonNumber, episodePrefix);
|
2011-09-01 08:58:54 +02:00
|
|
|
|
|
|
|
|
2011-11-29 07:49:38 +01:00
|
|
|
var searchUrls = GetPartialSeasonSearchUrls(GetQueryTitle(seriesTitle), seasonNumber, episodePrefix);
|
2011-09-01 08:58:54 +02:00
|
|
|
|
2011-12-08 04:54:31 +01:00
|
|
|
var result = Fetch(searchUrls);
|
2011-09-01 08:58:54 +02:00
|
|
|
|
2012-02-25 20:57:56 +01:00
|
|
|
_logger.Info("Finished searching {0} for {1} Season {2}, Found {3}", Name, seriesTitle, seasonNumber, result.Count);
|
2011-09-01 08:58:54 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-05-27 05:54:28 +02:00
|
|
|
public virtual IList<EpisodeParseResult> FetchEpisode(string seriesTitle, int seasonNumber, int episodeNumber)
|
2011-05-26 06:25:59 +02:00
|
|
|
{
|
2011-05-27 08:03:57 +02:00
|
|
|
_logger.Debug("Searching {0} for {1}-S{2:00}E{3:00}", Name, seriesTitle, seasonNumber, episodeNumber);
|
2011-05-26 06:25:59 +02:00
|
|
|
|
2011-11-29 07:49:38 +01:00
|
|
|
var searchUrls = GetEpisodeSearchUrls(GetQueryTitle(seriesTitle), seasonNumber, episodeNumber);
|
2011-05-26 06:25:59 +02:00
|
|
|
|
2011-12-08 04:54:31 +01:00
|
|
|
var result = Fetch(searchUrls);
|
2011-05-26 06:25:59 +02:00
|
|
|
|
2012-02-25 20:57:56 +01:00
|
|
|
_logger.Info("Finished searching {0} for {1} S{2:00}E{3:00}, Found {4}", Name, seriesTitle, seasonNumber, episodeNumber, result.Count);
|
2011-05-26 06:25:59 +02:00
|
|
|
return result;
|
2011-04-25 20:16:38 +02:00
|
|
|
|
2011-05-26 06:25:59 +02:00
|
|
|
}
|
2011-04-04 05:50:12 +02:00
|
|
|
|
2011-11-26 07:13:47 +01:00
|
|
|
public virtual IList<EpisodeParseResult> FetchDailyEpisode(string seriesTitle, DateTime airDate)
|
|
|
|
{
|
|
|
|
_logger.Debug("Searching {0} for {1}-{2}", Name, seriesTitle, airDate.ToShortDateString());
|
|
|
|
|
2011-11-29 07:49:38 +01:00
|
|
|
var searchUrls = GetDailyEpisodeSearchUrls(GetQueryTitle(seriesTitle), airDate);
|
2011-11-26 07:13:47 +01:00
|
|
|
|
2011-12-08 04:54:31 +01:00
|
|
|
var result = Fetch(searchUrls);
|
2011-11-26 07:13:47 +01:00
|
|
|
|
|
|
|
_logger.Info("Finished searching {0} for {1}-{2}, Found {3}", Name, seriesTitle, airDate.ToShortDateString(), result.Count);
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-08 04:54:31 +01:00
|
|
|
private List<EpisodeParseResult> Fetch(IEnumerable<string> urls)
|
2011-05-26 06:25:59 +02:00
|
|
|
{
|
|
|
|
var result = new List<EpisodeParseResult>();
|
|
|
|
|
2012-02-01 02:37:36 +01:00
|
|
|
if (!IsConfigured)
|
|
|
|
{
|
|
|
|
_logger.Warn("Indexer '{0}' isn't configured correctly. please reconfigure the indexer in settings page.", Name);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-12-08 04:54:31 +01:00
|
|
|
foreach (var url in urls)
|
2011-05-26 06:25:59 +02:00
|
|
|
{
|
2011-12-08 04:54:31 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_logger.Trace("Downloading RSS " + url);
|
2011-05-26 06:25:59 +02:00
|
|
|
|
2011-12-08 04:54:31 +01:00
|
|
|
var reader = new SyndicationFeedXmlReader(_httpProvider.DownloadStream(url, Credentials));
|
|
|
|
var feed = SyndicationFeed.Load(reader).Items;
|
2011-05-26 06:25:59 +02:00
|
|
|
|
2011-12-08 04:54:31 +01:00
|
|
|
foreach (var item in feed)
|
2011-04-22 08:23:29 +02:00
|
|
|
{
|
2011-12-08 04:54:31 +01:00
|
|
|
try
|
2011-04-22 22:14:02 +02:00
|
|
|
{
|
2011-12-08 04:54:31 +01:00
|
|
|
var parsedEpisode = ParseFeed(item);
|
|
|
|
if (parsedEpisode != null)
|
|
|
|
{
|
|
|
|
parsedEpisode.NzbUrl = NzbDownloadUrl(item);
|
2012-05-02 21:02:39 +02:00
|
|
|
parsedEpisode.NzbInfoUrl = NzbInfoUrl(item);
|
2011-12-08 04:54:31 +01:00
|
|
|
parsedEpisode.Indexer = Name;
|
|
|
|
result.Add(parsedEpisode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception itemEx)
|
|
|
|
{
|
2012-01-19 03:08:17 +01:00
|
|
|
itemEx.Data.Add("FeedUrl", url);
|
|
|
|
itemEx.Data.Add("Item", item.Title);
|
2011-12-08 04:54:31 +01:00
|
|
|
_logger.ErrorException("An error occurred while processing feed item", itemEx);
|
2011-04-22 22:14:02 +02:00
|
|
|
}
|
2011-05-26 06:25:59 +02:00
|
|
|
|
2011-12-08 04:54:31 +01:00
|
|
|
}
|
|
|
|
}
|
2012-02-01 02:37:36 +01:00
|
|
|
catch (WebException webException)
|
2012-01-21 20:36:22 +01:00
|
|
|
{
|
|
|
|
if (webException.Message.Contains("503"))
|
|
|
|
{
|
2012-02-28 03:35:25 +01:00
|
|
|
_logger.Warn("{0} server is currently unbelievable.{1} {2}", Name,url, webException.Message);
|
2012-01-21 20:36:22 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
webException.Data.Add("FeedUrl", url);
|
2012-02-28 03:35:25 +01:00
|
|
|
_logger.ErrorException("An error occurred while processing feed. " + url, webException);
|
2012-01-21 20:36:22 +01:00
|
|
|
}
|
|
|
|
}
|
2011-12-08 04:54:31 +01:00
|
|
|
catch (Exception feedEx)
|
|
|
|
{
|
2012-01-19 03:08:17 +01:00
|
|
|
feedEx.Data.Add("FeedUrl", url);
|
2012-02-28 03:35:25 +01:00
|
|
|
_logger.ErrorException("An error occurred while processing feed. " + url, feedEx);
|
2011-04-22 08:23:29 +02:00
|
|
|
}
|
2011-04-04 05:50:12 +02:00
|
|
|
}
|
|
|
|
|
2011-05-20 05:47:07 +02:00
|
|
|
return result;
|
2011-04-04 05:50:12 +02:00
|
|
|
}
|
2011-04-19 02:12:06 +02:00
|
|
|
|
2011-04-21 03:26:13 +02:00
|
|
|
/// <summary>
|
2011-05-20 05:47:07 +02:00
|
|
|
/// Parses the RSS feed item
|
2011-04-21 03:26:13 +02:00
|
|
|
/// </summary>
|
|
|
|
/// <param name = "item">RSS feed item to parse</param>
|
|
|
|
/// <returns>Detailed episode info</returns>
|
2011-04-25 22:21:52 +02:00
|
|
|
public EpisodeParseResult ParseFeed(SyndicationItem item)
|
2011-04-19 02:12:06 +02:00
|
|
|
{
|
2012-04-15 01:37:36 +02:00
|
|
|
var title = TitlePreParser(item);
|
|
|
|
|
|
|
|
var episodeParseResult = Parser.ParseTitle(title);
|
2012-04-23 21:33:16 +02:00
|
|
|
if (episodeParseResult != null)
|
|
|
|
{
|
|
|
|
episodeParseResult.Age = DateTime.Now.Date.Subtract(item.PublishDate.Date).Days;
|
|
|
|
episodeParseResult.OriginalString = title;
|
|
|
|
}
|
2011-04-21 03:26:13 +02:00
|
|
|
|
2012-04-17 05:42:18 +02:00
|
|
|
_logger.Trace("Parsed: {0} from: {1}", episodeParseResult, item.Title.Text);
|
|
|
|
|
2011-05-20 05:47:07 +02:00
|
|
|
return CustomParser(item, episodeParseResult);
|
2011-04-21 03:26:13 +02:00
|
|
|
}
|
2011-05-27 08:03:57 +02:00
|
|
|
|
2012-02-11 09:09:28 +01:00
|
|
|
/// <summary>
|
|
|
|
/// This method can be overwritten to provide indexer specific title cleaning
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="title">Title that needs to be cleaned</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public virtual string GetQueryTitle(string title)
|
2011-05-27 08:03:57 +02:00
|
|
|
{
|
2012-02-27 06:33:24 +01:00
|
|
|
title = RemoveThe.Replace(title, string.Empty);
|
|
|
|
|
2011-07-04 00:32:36 +02:00
|
|
|
var cleanTitle = TitleSearchRegex.Replace(title, "+").Trim('+', ' ');
|
|
|
|
|
|
|
|
//remove any repeating +s
|
|
|
|
cleanTitle = Regex.Replace(cleanTitle, @"\+{1,100}", "+");
|
|
|
|
return cleanTitle;
|
2011-05-27 08:03:57 +02:00
|
|
|
}
|
2011-04-04 05:50:12 +02:00
|
|
|
}
|
2011-04-10 04:44:01 +02:00
|
|
|
}
|