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