1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-29 02:57:15 +02:00
Sonarr/NzbDrone.Core/Indexers/IndexerFetchService.cs

142 lines
5.0 KiB
C#
Raw Normal View History

2013-04-07 10:30:37 +03:00
using System;
using System.Collections.Generic;
using System.Net;
using NLog;
using NzbDrone.Common;
using NzbDrone.Core.IndexerSearch.Definitions;
using NzbDrone.Core.Parser.Model;
using System.Linq;
2013-04-07 10:30:37 +03:00
namespace NzbDrone.Core.Indexers
{
public interface IFetchFeedFromIndexers
{
2013-09-14 02:17:58 +03:00
IList<ReleaseInfo> FetchRss(IIndexer indexer);
2013-04-07 10:30:37 +03:00
2013-09-14 02:17:58 +03:00
IList<ReleaseInfo> Fetch(IIndexer indexer, SeasonSearchCriteria searchCriteria);
IList<ReleaseInfo> Fetch(IIndexer indexer, SingleEpisodeSearchCriteria searchCriteria);
IList<ReleaseInfo> Fetch(IIndexer indexer, DailyEpisodeSearchCriteria searchCriteria);
2013-04-07 10:30:37 +03:00
}
public class FetchFeedService : IFetchFeedFromIndexers
{
private readonly Logger _logger;
2013-04-11 02:41:45 +03:00
private readonly IHttpProvider _httpProvider;
2013-04-07 10:30:37 +03:00
2013-04-11 02:41:45 +03:00
public FetchFeedService(IHttpProvider httpProvider, Logger logger)
2013-04-07 10:30:37 +03:00
{
_httpProvider = httpProvider;
_logger = logger;
}
2013-09-14 02:17:58 +03:00
public virtual IList<ReleaseInfo> FetchRss(IIndexer indexer)
2013-04-07 10:30:37 +03: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-09-14 02:17:58 +03:00
public IList<ReleaseInfo> Fetch(IIndexer indexer, SeasonSearchCriteria searchCriteria)
2013-04-07 10:30:37 +03:00
{
_logger.Debug("Searching for {0}", searchCriteria);
2013-04-07 10:30:37 +03:00
2013-09-14 02:17:58 +03:00
var result = Fetch(indexer, searchCriteria, 0).DistinctBy(c => c.DownloadUrl).ToList();
2013-04-07 10:30:37 +03:00
_logger.Info("Finished searching {0} for {1}. Found {2}", indexer.Name, searchCriteria, result.Count);
2013-04-07 10:30:37 +03:00
return result;
}
2013-09-14 02:17:58 +03:00
private IList<ReleaseInfo> Fetch(IIndexer indexer, SeasonSearchCriteria searchCriteria, int offset)
2013-04-07 10:30:37 +03:00
{
_logger.Debug("Searching for {0} offset: {1}", searchCriteria, offset);
2013-04-07 10:30:37 +03:00
var searchUrls = indexer.GetSeasonSearchUrls(searchCriteria.QueryTitle, searchCriteria.SeriesTvRageId, searchCriteria.SeasonNumber, offset);
2013-04-07 10:30:37 +03:00
var result = Fetch(indexer, searchUrls);
_logger.Info("{0} offset {1}. Found {2}", indexer.Name, searchCriteria, result.Count);
if (result.Count > 90)
{
result.AddRange(Fetch(indexer, searchCriteria, offset + 90));
}
2013-04-07 10:30:37 +03:00
return result;
2013-04-07 10:30:37 +03:00
}
2013-09-14 02:17:58 +03:00
public IList<ReleaseInfo> Fetch(IIndexer indexer, SingleEpisodeSearchCriteria searchCriteria)
2013-04-07 10:30:37 +03:00
{
_logger.Debug("Searching for {0}", searchCriteria);
2013-04-07 10:30:37 +03:00
var searchUrls = indexer.GetEpisodeSearchUrls(searchCriteria.QueryTitle, searchCriteria.SeriesTvRageId, searchCriteria.SeasonNumber, searchCriteria.EpisodeNumber);
2013-04-07 10:30:37 +03:00
var result = Fetch(indexer, searchUrls);
_logger.Info("Finished searching {0} for {1}. Found {2}", indexer.Name, searchCriteria, result.Count);
2013-04-07 10:30:37 +03:00
return result;
}
2013-09-14 02:17:58 +03:00
public IList<ReleaseInfo> Fetch(IIndexer indexer, DailyEpisodeSearchCriteria searchCriteria)
2013-04-07 10:30:37 +03:00
{
_logger.Debug("Searching for {0}", searchCriteria);
2013-04-07 10:30:37 +03:00
2013-08-06 11:15:24 +03:00
var searchUrls = indexer.GetDailyEpisodeSearchUrls(searchCriteria.QueryTitle, searchCriteria.SeriesTvRageId, searchCriteria.Airtime);
2013-04-07 10:30:37 +03:00
var result = Fetch(indexer, searchUrls);
_logger.Info("Finished searching {0} for {1}. Found {2}", indexer.Name, searchCriteria, result.Count);
2013-04-07 10:30:37 +03:00
return result;
}
2013-09-14 02:17:58 +03:00
private List<ReleaseInfo> Fetch(IIndexer indexer, IEnumerable<string> urls)
2013-04-07 10:30:37 +03:00
{
2013-09-14 02:17:58 +03:00
var result = new List<ReleaseInfo>();
2013-04-07 10:30:37 +03:00
foreach (var url in urls)
{
try
{
2013-08-06 08:06:58 +03:00
_logger.Trace("Downloading Feed " + url);
var xml = _httpProvider.DownloadString(url);
if (!string.IsNullOrWhiteSpace(xml))
{
result.AddRange(indexer.Parser.Process(xml, url));
}
else
{
_logger.Warn("{0} returned empty response.", url);
}
2013-04-07 10:30:37 +03:00
}
catch (WebException webException)
{
if (webException.Message.Contains("502") || webException.Message.Contains("503") || webException.Message.Contains("timed out"))
2013-04-07 10:30:37 +03:00
{
2013-08-06 05:45:57 +03:00
_logger.Warn("{0} server is currently unavailable. {1} {2}", indexer.Name, url, webException.Message);
2013-04-07 10:30:37 +03:00
}
else
{
_logger.Warn("{0} {1} {2}", indexer.Name, url, webException.Message);
2013-04-07 10:30:37 +03:00
}
}
catch (Exception feedEx)
{
feedEx.Data.Add("FeedUrl", url);
2013-08-06 08:06:58 +03:00
_logger.ErrorException("An error occurred while processing feed. " + url, feedEx);
2013-04-07 10:30:37 +03:00
}
}
result.ForEach(c => c.Indexer = indexer.Name);
return result;
}
}
2013-09-14 02:17:58 +03:00
}