2011-04-22 05:23:31 +03:00
|
|
|
using System;
|
2011-04-25 01:32:08 +03:00
|
|
|
using System.Collections.Generic;
|
2011-04-25 23:21:52 +03:00
|
|
|
using System.Net;
|
2011-04-21 04:29:41 +03:00
|
|
|
using System.ServiceModel.Syndication;
|
2011-07-04 01:32:36 +03:00
|
|
|
using System.Text.RegularExpressions;
|
2011-05-26 07:25:59 +03:00
|
|
|
using System.Web;
|
2011-06-14 04:23:04 +03:00
|
|
|
using Ninject;
|
2011-04-04 06:50:12 +03:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Core.Model;
|
2011-04-04 10:21:07 +03:00
|
|
|
using NzbDrone.Core.Providers.Core;
|
2011-04-04 06:50:12 +03:00
|
|
|
|
2011-04-19 03:12:06 +03:00
|
|
|
namespace NzbDrone.Core.Providers.Indexer
|
2011-04-04 06:50:12 +03:00
|
|
|
{
|
2011-05-20 07:21:18 +03:00
|
|
|
public abstract class IndexerBase
|
2011-04-04 06:50:12 +03:00
|
|
|
{
|
2011-04-22 09:23:29 +03:00
|
|
|
protected readonly Logger _logger;
|
2011-04-07 05:25:52 +03:00
|
|
|
private readonly HttpProvider _httpProvider;
|
2011-05-20 06:47:07 +03:00
|
|
|
protected readonly ConfigProvider _configProvider;
|
2011-04-19 03:12:06 +03:00
|
|
|
|
2011-07-04 01:32:36 +03:00
|
|
|
private static readonly Regex TitleSearchRegex = new Regex(@"[\W]", RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
|
|
|
|
2011-06-14 04:23:04 +03:00
|
|
|
[Inject]
|
2011-05-26 07:25:59 +03:00
|
|
|
protected IndexerBase(HttpProvider httpProvider, ConfigProvider configProvider)
|
2011-04-04 09:53:22 +03:00
|
|
|
{
|
2011-04-05 08:30:13 +03:00
|
|
|
_httpProvider = httpProvider;
|
2011-05-20 06:47:07 +03:00
|
|
|
_configProvider = configProvider;
|
2011-05-01 11:04:44 +03:00
|
|
|
|
2011-04-29 09:32:51 +03:00
|
|
|
_logger = LogManager.GetLogger(GetType().ToString());
|
2011-04-04 09:53:22 +03:00
|
|
|
}
|
|
|
|
|
2011-05-27 06:54:28 +03:00
|
|
|
public IndexerBase()
|
|
|
|
{
|
2011-05-27 09:03:57 +03:00
|
|
|
|
2011-05-27 06:54:28 +03:00
|
|
|
}
|
|
|
|
|
2011-04-04 06:50:12 +03:00
|
|
|
/// <summary>
|
2011-04-19 03:12:06 +03:00
|
|
|
/// Gets the name for the feed
|
2011-04-04 06:50:12 +03:00
|
|
|
/// </summary>
|
2011-04-19 03:12:06 +03:00
|
|
|
public abstract string Name { get; }
|
2011-04-04 06:50:12 +03:00
|
|
|
|
2011-04-04 09:53:22 +03:00
|
|
|
/// <summary>
|
2011-04-21 04:26:13 +03:00
|
|
|
/// Gets the source URL for the feed
|
2011-04-04 09:53:22 +03:00
|
|
|
/// </summary>
|
2011-04-21 04:26:13 +03:00
|
|
|
protected abstract string[] Urls { get; }
|
2011-04-04 09:53:22 +03:00
|
|
|
|
2011-05-26 07:25:59 +03:00
|
|
|
|
2011-05-27 05:12:28 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the credential.
|
|
|
|
/// </summary>
|
|
|
|
protected virtual NetworkCredential Credentials
|
|
|
|
{
|
|
|
|
get { return null; }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-26 07:25:59 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the rss url for specific episode search
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="seriesTitle">The series title.</param>
|
|
|
|
/// <param name="seasonNumber">The season number.</param>
|
|
|
|
/// <param name="episodeNumber">The episode number.</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
protected abstract IList<String> GetSearchUrls(string seriesTitle, int seasonNumber, int episodeNumber);
|
|
|
|
|
2011-04-25 23:21:52 +03:00
|
|
|
/// <summary>
|
2011-05-27 05:12:28 +03:00
|
|
|
/// This method can be overwritten to provide indexer specific info parsing
|
2011-04-25 23:21:52 +03:00
|
|
|
/// </summary>
|
2011-05-27 05:12:28 +03: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 23:21:52 +03:00
|
|
|
{
|
2011-05-27 05:12:28 +03:00
|
|
|
return currentResult;
|
2011-04-25 23:21:52 +03:00
|
|
|
}
|
|
|
|
|
2011-05-27 05:12:28 +03: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 09:53:22 +03:00
|
|
|
|
|
|
|
/// <summary>
|
2011-04-10 05:44:01 +03:00
|
|
|
/// Fetches RSS feed and process each news item.
|
2011-04-04 09:53:22 +03:00
|
|
|
/// </summary>
|
2011-05-27 06:54:28 +03:00
|
|
|
public virtual IList<EpisodeParseResult> FetchRss()
|
2011-04-04 06:50:12 +03:00
|
|
|
{
|
2011-05-26 07:25:59 +03:00
|
|
|
_logger.Debug("Fetching feeds from " + Name);
|
2011-05-20 06:47:07 +03:00
|
|
|
|
|
|
|
var result = new List<EpisodeParseResult>();
|
2011-04-04 06:50:12 +03:00
|
|
|
|
2011-04-21 04:26:13 +03:00
|
|
|
foreach (var url in Urls)
|
2011-04-04 06:50:12 +03:00
|
|
|
{
|
2011-05-26 07:25:59 +03:00
|
|
|
result.AddRange(Fetch(url));
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.Info("Finished processing feeds from " + Name);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-27 06:54:28 +03:00
|
|
|
public virtual IList<EpisodeParseResult> FetchEpisode(string seriesTitle, int seasonNumber, int episodeNumber)
|
2011-05-26 07:25:59 +03:00
|
|
|
{
|
2011-05-27 09:03:57 +03:00
|
|
|
_logger.Debug("Searching {0} for {1}-S{2:00}E{3:00}", Name, seriesTitle, seasonNumber, episodeNumber);
|
2011-05-26 07:25:59 +03:00
|
|
|
|
|
|
|
var result = new List<EpisodeParseResult>();
|
|
|
|
|
2011-07-04 01:32:36 +03:00
|
|
|
var searchUrls = GetSearchUrls(GetQueryTitle(seriesTitle), seasonNumber, episodeNumber);
|
2011-05-26 07:25:59 +03:00
|
|
|
|
|
|
|
foreach (var url in searchUrls)
|
|
|
|
{
|
|
|
|
result.AddRange(Fetch(url));
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.Info("Finished searching {0} for {1}-S{2}E{3:00}, Found {4}", Name, seriesTitle, seasonNumber, episodeNumber, result.Count);
|
|
|
|
return result;
|
2011-04-25 21:16:38 +03:00
|
|
|
|
2011-05-26 07:25:59 +03:00
|
|
|
}
|
2011-04-04 06:50:12 +03:00
|
|
|
|
2011-05-27 05:12:28 +03:00
|
|
|
private IEnumerable<EpisodeParseResult> Fetch(string url)
|
2011-05-26 07:25:59 +03:00
|
|
|
{
|
|
|
|
var result = new List<EpisodeParseResult>();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_logger.Trace("Downloading RSS " + url);
|
|
|
|
|
|
|
|
var reader = new SyndicationFeedXmlReader(_httpProvider.DownloadStream(url, Credentials));
|
|
|
|
var feed = SyndicationFeed.Load(reader).Items;
|
|
|
|
|
|
|
|
foreach (var item in feed)
|
|
|
|
{
|
|
|
|
try
|
2011-04-22 09:23:29 +03:00
|
|
|
{
|
2011-05-26 07:25:59 +03:00
|
|
|
var parsedEpisode = ParseFeed(item);
|
|
|
|
if (parsedEpisode != null)
|
2011-04-22 23:14:02 +03:00
|
|
|
{
|
2011-05-26 07:25:59 +03:00
|
|
|
parsedEpisode.NzbUrl = NzbDownloadUrl(item);
|
|
|
|
parsedEpisode.Indexer = Name;
|
|
|
|
parsedEpisode.NzbTitle = item.Title.Text;
|
|
|
|
result.Add(parsedEpisode);
|
2011-04-22 23:14:02 +03:00
|
|
|
}
|
2011-04-22 09:23:29 +03:00
|
|
|
}
|
2011-05-26 07:25:59 +03:00
|
|
|
catch (Exception itemEx)
|
|
|
|
{
|
|
|
|
_logger.ErrorException("An error occurred while processing feed item", itemEx);
|
|
|
|
}
|
|
|
|
|
2011-04-22 09:23:29 +03:00
|
|
|
}
|
2011-05-26 07:25:59 +03:00
|
|
|
}
|
|
|
|
catch (Exception feedEx)
|
|
|
|
{
|
|
|
|
_logger.ErrorException("An error occurred while processing feed", feedEx);
|
2011-04-04 06:50:12 +03:00
|
|
|
}
|
|
|
|
|
2011-05-20 06:47:07 +03:00
|
|
|
return result;
|
2011-04-04 06:50:12 +03:00
|
|
|
}
|
2011-04-19 03:12:06 +03:00
|
|
|
|
2011-04-21 04:26:13 +03:00
|
|
|
/// <summary>
|
2011-05-20 06:47:07 +03:00
|
|
|
/// Parses the RSS feed item
|
2011-04-21 04:26:13 +03:00
|
|
|
/// </summary>
|
|
|
|
/// <param name = "item">RSS feed item to parse</param>
|
|
|
|
/// <returns>Detailed episode info</returns>
|
2011-04-25 23:21:52 +03:00
|
|
|
public EpisodeParseResult ParseFeed(SyndicationItem item)
|
2011-04-19 03:12:06 +03:00
|
|
|
{
|
2011-06-30 02:31:16 +03:00
|
|
|
var episodeParseResult = Parser.ParseTitle(item.Title.Text);
|
2011-04-21 04:26:13 +03:00
|
|
|
|
2011-05-20 06:47:07 +03:00
|
|
|
return CustomParser(item, episodeParseResult);
|
2011-04-21 04:26:13 +03:00
|
|
|
}
|
2011-05-27 09:03:57 +03:00
|
|
|
|
2011-07-04 01:32:36 +03:00
|
|
|
public static string GetQueryTitle(string title)
|
2011-05-27 09:03:57 +03:00
|
|
|
{
|
2011-07-04 01:32:36 +03:00
|
|
|
var cleanTitle = TitleSearchRegex.Replace(title, "+").Trim('+', ' ');
|
|
|
|
|
|
|
|
//remove any repeating +s
|
|
|
|
cleanTitle = Regex.Replace(cleanTitle, @"\+{1,100}", "+");
|
|
|
|
return cleanTitle;
|
2011-05-27 09:03:57 +03:00
|
|
|
}
|
2011-04-04 06:50:12 +03:00
|
|
|
}
|
2011-04-10 05:44:01 +03:00
|
|
|
}
|