2013-09-26 02:51:07 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using FluentValidation;
|
|
|
|
|
using FluentValidation.Results;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
2013-09-27 04:41:08 +00:00
|
|
|
|
using NzbDrone.Core.Indexers.Exceptions;
|
|
|
|
|
using NzbDrone.Core.Indexers.Newznab;
|
2013-09-26 02:51:07 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Indexers
|
|
|
|
|
{
|
|
|
|
|
public interface INewznabTestService
|
|
|
|
|
{
|
|
|
|
|
void Test(IIndexer indexer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class NewznabTestService : INewznabTestService
|
|
|
|
|
{
|
|
|
|
|
private readonly IFetchFeedFromIndexers _feedFetcher;
|
|
|
|
|
private readonly IHttpProvider _httpProvider;
|
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
public NewznabTestService(IFetchFeedFromIndexers feedFetcher, IHttpProvider httpProvider, Logger logger)
|
|
|
|
|
{
|
|
|
|
|
_feedFetcher = feedFetcher;
|
|
|
|
|
_httpProvider = httpProvider;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Test(IIndexer indexer)
|
|
|
|
|
{
|
|
|
|
|
var releases = _feedFetcher.FetchRss(indexer);
|
|
|
|
|
|
|
|
|
|
if (releases.Any()) return;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-09-27 04:41:08 +00:00
|
|
|
|
var url = indexer.RecentFeed.First();
|
|
|
|
|
var xml = _httpProvider.DownloadString(url);
|
|
|
|
|
|
|
|
|
|
NewznabPreProcessor.Process(xml, url);
|
2013-09-26 02:51:07 +00:00
|
|
|
|
}
|
2013-09-27 04:41:08 +00:00
|
|
|
|
catch (ApiKeyException apiKeyException)
|
|
|
|
|
{
|
|
|
|
|
_logger.Warn("Indexer returned result for Newznab RSS URL, API Key appears to be invalid");
|
2013-09-26 02:51:07 +00:00
|
|
|
|
|
2013-09-27 04:41:08 +00:00
|
|
|
|
var apiKeyFailure = new ValidationFailure("ApiKey", "Invalid API Key");
|
|
|
|
|
throw new ValidationException(new List<ValidationFailure> { apiKeyFailure }.ToArray());
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2013-09-26 02:51:07 +00:00
|
|
|
|
{
|
2013-09-27 04:41:08 +00:00
|
|
|
|
_logger.Warn("Indexer doesn't appear to be Newznab based");
|
2013-09-26 02:51:07 +00:00
|
|
|
|
|
|
|
|
|
var failure = new ValidationFailure("Url", "Invalid Newznab URL entered");
|
|
|
|
|
throw new ValidationException(new List<ValidationFailure> { failure }.ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|