2013-04-07 07:30:37 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Net;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common;
|
|
|
|
using NzbDrone.Core.IndexerSearch.Definitions;
|
2013-04-15 01:41:39 +00:00
|
|
|
using NzbDrone.Core.Parser.Model;
|
2013-04-07 07:30:37 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Indexers
|
|
|
|
{
|
|
|
|
public interface IFetchFeedFromIndexers
|
|
|
|
{
|
2013-05-02 01:59:09 +00:00
|
|
|
IList<ReportInfo> FetchRss(IIndexer indexer);
|
2013-04-07 07:30:37 +00:00
|
|
|
|
2013-06-06 14:42:23 +00:00
|
|
|
IList<ReportInfo> Fetch(IIndexer indexer, SeasonSearchCriteria searchCriteria);
|
|
|
|
IList<ReportInfo> Fetch(IIndexer indexer, SingleEpisodeSearchCriteria searchCriteria);
|
|
|
|
IList<ReportInfo> Fetch(IIndexer indexer, PartialSeasonSearchCriteria searchCriteria);
|
|
|
|
IList<ReportInfo> Fetch(IIndexer indexer, DailyEpisodeSearchCriteria searchCriteria);
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class FetchFeedService : IFetchFeedFromIndexers
|
|
|
|
{
|
|
|
|
private readonly Logger _logger;
|
2013-04-10 23:41:45 +00:00
|
|
|
private readonly IHttpProvider _httpProvider;
|
2013-04-07 07:30:37 +00:00
|
|
|
|
|
|
|
|
2013-04-10 23:41:45 +00:00
|
|
|
public FetchFeedService(IHttpProvider httpProvider, Logger logger)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
|
|
|
_httpProvider = httpProvider;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-02 01:59:09 +00:00
|
|
|
public virtual IList<ReportInfo> FetchRss(IIndexer indexer)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
|
|
|
_logger.Debug("Fetching feeds from " + indexer.Name);
|
|
|
|
|
|
|
|
var result = Fetch(indexer, indexer.RecentFeed);
|
|
|
|
|
|
|
|
_logger.Debug("Finished processing feeds from " + indexer.Name);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-06-06 14:42:23 +00:00
|
|
|
public IList<ReportInfo> Fetch(IIndexer indexer, SeasonSearchCriteria searchCriteria)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-06-06 14:42:23 +00:00
|
|
|
_logger.Debug("Searching for {0}", searchCriteria);
|
2013-04-07 07:30:37 +00:00
|
|
|
|
2013-08-06 05:26:09 +00:00
|
|
|
var searchUrls = indexer.GetSeasonSearchUrls(searchCriteria.QueryTitle, searchCriteria.SeriesRageTvId, searchCriteria.SeasonNumber);
|
2013-04-07 07:30:37 +00:00
|
|
|
var result = Fetch(indexer, searchUrls);
|
|
|
|
|
|
|
|
|
2013-06-06 14:42:23 +00:00
|
|
|
_logger.Info("Finished searching {0} on {1}. Found {2}", indexer.Name, searchCriteria, result.Count);
|
2013-04-07 07:30:37 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-06-06 14:42:23 +00:00
|
|
|
public IList<ReportInfo> Fetch(IIndexer indexer, SingleEpisodeSearchCriteria searchCriteria)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-06-06 14:42:23 +00:00
|
|
|
_logger.Debug("Searching for {0}", searchCriteria);
|
2013-04-07 07:30:37 +00:00
|
|
|
|
2013-08-06 05:26:09 +00:00
|
|
|
var searchUrls = indexer.GetEpisodeSearchUrls(searchCriteria.QueryTitle, searchCriteria.SeriesRageTvId, searchCriteria.SeasonNumber, searchCriteria.EpisodeNumber);
|
2013-04-07 07:30:37 +00:00
|
|
|
var result = Fetch(indexer, searchUrls);
|
|
|
|
|
|
|
|
|
2013-06-06 14:42:23 +00:00
|
|
|
_logger.Info("Finished searching {0} on {1}. Found {2}", indexer.Name, searchCriteria, result.Count);
|
2013-04-07 07:30:37 +00:00
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-06 14:42:23 +00:00
|
|
|
public IList<ReportInfo> Fetch(IIndexer indexer, PartialSeasonSearchCriteria searchCriteria)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-06-06 14:42:23 +00:00
|
|
|
_logger.Debug("Searching for {0}", searchCriteria);
|
2013-04-07 07:30:37 +00:00
|
|
|
|
2013-08-06 05:26:09 +00:00
|
|
|
var searchUrls = indexer.GetSeasonSearchUrls(searchCriteria.QueryTitle, searchCriteria.SeriesRageTvId, searchCriteria.SeasonNumber);
|
2013-04-07 07:30:37 +00:00
|
|
|
var result = Fetch(indexer, searchUrls);
|
|
|
|
|
|
|
|
|
2013-06-06 14:42:23 +00:00
|
|
|
_logger.Info("Finished searching {0} on {1}. Found {2}", indexer.Name, searchCriteria, result.Count);
|
2013-04-07 07:30:37 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-06-06 14:42:23 +00:00
|
|
|
public IList<ReportInfo> Fetch(IIndexer indexer, DailyEpisodeSearchCriteria searchCriteria)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-06-06 14:42:23 +00:00
|
|
|
_logger.Debug("Searching for {0}", searchCriteria);
|
2013-04-07 07:30:37 +00:00
|
|
|
|
2013-08-06 05:26:09 +00:00
|
|
|
var searchUrls = indexer.GetDailyEpisodeSearchUrls(searchCriteria.QueryTitle, searchCriteria.SeriesRageTvId, searchCriteria.Airtime);
|
2013-04-07 07:30:37 +00:00
|
|
|
var result = Fetch(indexer, searchUrls);
|
|
|
|
|
2013-06-06 14:42:23 +00:00
|
|
|
_logger.Info("Finished searching {0} on {1}. Found {2}", indexer.Name, searchCriteria, result.Count);
|
2013-04-07 07:30:37 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-05-02 01:59:09 +00:00
|
|
|
private List<ReportInfo> Fetch(IIndexer indexer, IEnumerable<string> urls)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-04-15 01:41:39 +00:00
|
|
|
var result = new List<ReportInfo>();
|
2013-04-07 07:30:37 +00:00
|
|
|
|
|
|
|
foreach (var url in urls)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_logger.Trace("Downloading Feed " + url);
|
|
|
|
var stream = _httpProvider.DownloadStream(url);
|
|
|
|
result.AddRange(indexer.Parser.Process(stream));
|
|
|
|
}
|
|
|
|
catch (WebException webException)
|
|
|
|
{
|
2013-07-05 05:16:49 +00:00
|
|
|
if (webException.Message.Contains("503") || webException.Message.Contains("timed out"))
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
|
|
|
_logger.Warn("{0} server is currently unavailable.{1} {2}", indexer.Name, url, webException.Message);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
webException.Data.Add("FeedUrl", url);
|
2013-07-05 05:16:49 +00:00
|
|
|
_logger.WarnException("An error occurred while processing feed. " + url, webException);
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception feedEx)
|
|
|
|
{
|
|
|
|
feedEx.Data.Add("FeedUrl", url);
|
|
|
|
_logger.ErrorException("An error occurred while processing feed. " + url, feedEx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result.ForEach(c => c.Indexer = indexer.Name);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|