mirror of
https://github.com/lidarr/Lidarr
synced 2024-12-28 02:27:13 +00:00
238 lines
No EOL
8.6 KiB
C#
238 lines
No EOL
8.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.ServiceModel.Syndication;
|
|
using System.Text.RegularExpressions;
|
|
using NLog;
|
|
using NzbDrone.Common;
|
|
using NzbDrone.Core.Configuration;
|
|
using NzbDrone.Core.Model;
|
|
|
|
namespace NzbDrone.Core.Indexers
|
|
{
|
|
public abstract class IndexerBase
|
|
{
|
|
protected readonly Logger _logger;
|
|
protected readonly HttpProvider _httpProvider;
|
|
protected readonly IConfigService _configService;
|
|
|
|
protected static readonly Regex TitleSearchRegex = new Regex(@"[\W]", RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
|
protected static readonly Regex RemoveThe = new Regex(@"^the\s", RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
|
|
|
protected IndexerBase(HttpProvider httpProvider, IConfigService configService)
|
|
{
|
|
_httpProvider = httpProvider;
|
|
_configService = configService;
|
|
|
|
_logger = LogManager.GetLogger(GetType().ToString());
|
|
}
|
|
|
|
public IndexerBase()
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the name for the feed
|
|
/// </summary>
|
|
public abstract string Name { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the source URL for the feed
|
|
/// </summary>
|
|
protected abstract string[] Urls { get; }
|
|
|
|
public abstract bool IsConfigured { get; }
|
|
|
|
/// <summary>
|
|
/// Should the indexer be enabled by default?
|
|
/// </summary>
|
|
public virtual bool EnabledByDefault
|
|
{
|
|
get { return false; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the credential.
|
|
/// </summary>
|
|
protected virtual NetworkCredential Credentials
|
|
{
|
|
get { return null; }
|
|
}
|
|
|
|
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);
|
|
|
|
protected virtual EpisodeParseResult CustomParser(SyndicationItem item, EpisodeParseResult currentResult)
|
|
{
|
|
return currentResult;
|
|
}
|
|
|
|
protected virtual string TitlePreParser(SyndicationItem item)
|
|
{
|
|
return item.Title.Text;
|
|
}
|
|
|
|
protected abstract string NzbDownloadUrl(SyndicationItem item);
|
|
|
|
protected abstract string NzbInfoUrl(SyndicationItem item);
|
|
|
|
public virtual IList<EpisodeParseResult> FetchRss()
|
|
{
|
|
_logger.Debug("Fetching feeds from " + Name);
|
|
|
|
var result = new List<EpisodeParseResult>();
|
|
|
|
|
|
result = Fetch(Urls);
|
|
|
|
|
|
_logger.Debug("Finished processing feeds from " + Name);
|
|
return result;
|
|
}
|
|
|
|
public virtual IList<EpisodeParseResult> FetchSeason(string seriesTitle, int seasonNumber)
|
|
{
|
|
_logger.Debug("Searching {0} for {1} Season {2}", Name, seriesTitle, seasonNumber);
|
|
|
|
var searchUrls = GetSeasonSearchUrls(GetQueryTitle(seriesTitle), seasonNumber);
|
|
var result = Fetch(searchUrls);
|
|
|
|
_logger.Info("Finished searching {0} for {1} Season {2}, Found {3}", Name, seriesTitle, seasonNumber, result.Count);
|
|
return result;
|
|
}
|
|
|
|
public virtual IList<EpisodeParseResult> FetchPartialSeason(string seriesTitle, int seasonNumber, int episodePrefix)
|
|
{
|
|
_logger.Debug("Searching {0} for {1} Season {2}, Prefix: {3}", Name, seriesTitle, seasonNumber, episodePrefix);
|
|
|
|
|
|
var searchUrls = GetPartialSeasonSearchUrls(GetQueryTitle(seriesTitle), seasonNumber, episodePrefix);
|
|
|
|
var result = Fetch(searchUrls);
|
|
|
|
_logger.Info("Finished searching {0} for {1} Season {2}, Found {3}", Name, seriesTitle, seasonNumber, result.Count);
|
|
return result;
|
|
}
|
|
|
|
public virtual IList<EpisodeParseResult> FetchEpisode(string seriesTitle, int seasonNumber, int episodeNumber)
|
|
{
|
|
_logger.Debug("Searching {0} for {1}-S{2:00}E{3:00}", Name, seriesTitle, seasonNumber, episodeNumber);
|
|
|
|
var searchUrls = GetEpisodeSearchUrls(GetQueryTitle(seriesTitle), seasonNumber, episodeNumber);
|
|
|
|
var result = Fetch(searchUrls);
|
|
|
|
_logger.Info("Finished searching {0} for {1} S{2:00}E{3:00}, Found {4}", Name, seriesTitle, seasonNumber, episodeNumber, result.Count);
|
|
return result;
|
|
|
|
}
|
|
|
|
public virtual IList<EpisodeParseResult> FetchDailyEpisode(string seriesTitle, DateTime airDate)
|
|
{
|
|
_logger.Debug("Searching {0} for {1}-{2}", Name, seriesTitle, airDate.ToShortDateString());
|
|
|
|
var searchUrls = GetDailyEpisodeSearchUrls(GetQueryTitle(seriesTitle), airDate);
|
|
|
|
var result = Fetch(searchUrls);
|
|
|
|
_logger.Info("Finished searching {0} for {1}-{2}, Found {3}", Name, seriesTitle, airDate.ToShortDateString(), result.Count);
|
|
return result;
|
|
|
|
}
|
|
|
|
private List<EpisodeParseResult> Fetch(IEnumerable<string> urls)
|
|
{
|
|
var result = new List<EpisodeParseResult>();
|
|
|
|
if (!IsConfigured)
|
|
{
|
|
_logger.Warn("Indexer '{0}' isn't configured correctly. please reconfigure the indexer in settings page.", Name);
|
|
return result;
|
|
}
|
|
|
|
foreach (var url in urls)
|
|
{
|
|
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
|
|
{
|
|
var parsedEpisode = ParseFeed(item);
|
|
if (parsedEpisode != null)
|
|
{
|
|
parsedEpisode.NzbUrl = NzbDownloadUrl(item);
|
|
parsedEpisode.NzbInfoUrl = NzbInfoUrl(item);
|
|
parsedEpisode.Indexer = String.IsNullOrWhiteSpace(parsedEpisode.Indexer) ? Name : parsedEpisode.Indexer;
|
|
result.Add(parsedEpisode);
|
|
}
|
|
}
|
|
catch (Exception itemEx)
|
|
{
|
|
itemEx.Data.Add("FeedUrl", url);
|
|
itemEx.Data.Add("Item", item.Title);
|
|
_logger.ErrorException("An error occurred while processing feed item", itemEx);
|
|
}
|
|
|
|
}
|
|
}
|
|
catch (WebException webException)
|
|
{
|
|
if (webException.Message.Contains("503"))
|
|
{
|
|
_logger.Warn("{0} server is currently unavailable.{1} {2}", Name, url, webException.Message);
|
|
}
|
|
else
|
|
{
|
|
webException.Data.Add("FeedUrl", url);
|
|
_logger.ErrorException("An error occurred while processing feed. " + url, webException);
|
|
}
|
|
}
|
|
catch (Exception feedEx)
|
|
{
|
|
feedEx.Data.Add("FeedUrl", url);
|
|
_logger.ErrorException("An error occurred while processing feed. " + url, feedEx);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public EpisodeParseResult ParseFeed(SyndicationItem item)
|
|
{
|
|
var title = TitlePreParser(item);
|
|
|
|
var episodeParseResult = Parser.ParseTitle(title);
|
|
if (episodeParseResult != null)
|
|
{
|
|
episodeParseResult.Age = DateTime.Now.Date.Subtract(item.PublishDate.Date).Days;
|
|
episodeParseResult.OriginalString = title;
|
|
episodeParseResult.SceneSource = true;
|
|
}
|
|
|
|
_logger.Trace("Parsed: {0} from: {1}", episodeParseResult, item.Title.Text);
|
|
|
|
return CustomParser(item, episodeParseResult);
|
|
}
|
|
|
|
public virtual string GetQueryTitle(string title)
|
|
{
|
|
title = RemoveThe.Replace(title, string.Empty);
|
|
|
|
var cleanTitle = TitleSearchRegex.Replace(title, "+").Trim('+', ' ');
|
|
|
|
//remove any repeating +s
|
|
cleanTitle = Regex.Replace(cleanTitle, @"\+{1,100}", "+");
|
|
return cleanTitle;
|
|
}
|
|
}
|
|
} |