2013-04-23 09:06:05 +03:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
2013-07-12 09:10:34 +03:00
|
|
|
using NzbDrone.Common.TPL;
|
2013-04-23 09:06:05 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Indexers
|
|
|
|
{
|
|
|
|
public interface IFetchAndParseRss
|
|
|
|
{
|
2013-09-14 02:17:58 +03:00
|
|
|
List<ReleaseInfo> Fetch();
|
2013-04-23 09:06:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public class FetchAndParseRssService : IFetchAndParseRss
|
|
|
|
{
|
2013-09-25 02:42:55 +03:00
|
|
|
private readonly IIndexerFactory _indexerFactory;
|
2013-04-23 09:06:05 +03:00
|
|
|
private readonly IFetchFeedFromIndexers _feedFetcher;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-09-25 02:42:55 +03:00
|
|
|
public FetchAndParseRssService(IIndexerFactory indexerFactory, IFetchFeedFromIndexers feedFetcher, Logger logger)
|
2013-04-23 09:06:05 +03:00
|
|
|
{
|
2013-09-25 02:42:55 +03:00
|
|
|
_indexerFactory = indexerFactory;
|
2013-04-23 09:06:05 +03:00
|
|
|
_feedFetcher = feedFetcher;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2013-09-14 02:17:58 +03:00
|
|
|
public List<ReleaseInfo> Fetch()
|
2013-04-23 09:06:05 +03:00
|
|
|
{
|
2013-09-14 02:17:58 +03:00
|
|
|
var result = new List<ReleaseInfo>();
|
2013-04-23 09:06:05 +03:00
|
|
|
|
2013-09-25 02:42:55 +03:00
|
|
|
var indexers = _indexerFactory.GetAvailableProviders().ToList();
|
2013-04-23 09:06:05 +03:00
|
|
|
|
|
|
|
if (!indexers.Any())
|
|
|
|
{
|
|
|
|
_logger.Warn("No available indexers. check your configuration.");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.Debug("Available indexers {0}", indexers.Count);
|
|
|
|
|
2013-05-30 04:35:26 +03:00
|
|
|
|
|
|
|
var taskList = new List<Task>();
|
|
|
|
var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None);
|
|
|
|
|
|
|
|
foreach (var indexer in indexers)
|
|
|
|
{
|
|
|
|
var indexerLocal = indexer;
|
|
|
|
|
|
|
|
var task = taskFactory.StartNew(() =>
|
|
|
|
{
|
|
|
|
var indexerFeed = _feedFetcher.FetchRss(indexerLocal);
|
|
|
|
|
|
|
|
lock (result)
|
|
|
|
{
|
|
|
|
result.AddRange(indexerFeed);
|
|
|
|
}
|
2013-07-12 09:10:34 +03:00
|
|
|
}).LogExceptions();
|
2013-05-30 04:35:26 +03:00
|
|
|
|
|
|
|
taskList.Add(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
Task.WaitAll(taskList.ToArray());
|
2013-04-23 09:06:05 +03:00
|
|
|
|
|
|
|
_logger.Debug("Found {0} reports", result.Count);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|