2013-04-07 09:30:37 +02:00
|
|
|
|
using System;
|
2013-05-14 08:22:25 +02:00
|
|
|
|
using System.Collections.Generic;
|
2013-04-07 09:30:37 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
2013-04-27 04:03:34 +02:00
|
|
|
|
using NzbDrone.Common.Messaging;
|
2013-04-07 09:30:37 +02:00
|
|
|
|
using NzbDrone.Core.DecisionEngine;
|
|
|
|
|
using NzbDrone.Core.Download;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Indexers
|
|
|
|
|
{
|
2013-04-23 08:06:05 +02:00
|
|
|
|
public interface IRssSyncService
|
2013-04-07 09:30:37 +02:00
|
|
|
|
{
|
|
|
|
|
void Sync();
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-27 04:03:34 +02:00
|
|
|
|
public class RssSyncService : IRssSyncService, IExecute<RssSyncCommand>
|
2013-04-07 09:30:37 +02:00
|
|
|
|
{
|
|
|
|
|
private readonly IFetchAndParseRss _rssFetcherAndParser;
|
|
|
|
|
private readonly IMakeDownloadDecision _downloadDecisionMaker;
|
|
|
|
|
private readonly IDownloadService _downloadService;
|
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
public RssSyncService(IFetchAndParseRss rssFetcherAndParser, IMakeDownloadDecision downloadDecisionMaker, IDownloadService downloadService, Logger logger)
|
|
|
|
|
{
|
|
|
|
|
_rssFetcherAndParser = rssFetcherAndParser;
|
|
|
|
|
_downloadDecisionMaker = downloadDecisionMaker;
|
|
|
|
|
_downloadService = downloadService;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Sync()
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Starting RSS Sync");
|
|
|
|
|
|
2013-04-23 08:14:55 +02:00
|
|
|
|
var reports = _rssFetcherAndParser.Fetch();
|
|
|
|
|
var decisions = _downloadDecisionMaker.GetRssDecision(reports);
|
2013-04-07 09:30:37 +02:00
|
|
|
|
|
|
|
|
|
var qualifiedReports = decisions
|
|
|
|
|
.Where(c => c.Approved)
|
2013-04-28 21:46:13 +02:00
|
|
|
|
.Select(c => c.RemoteEpisode)
|
|
|
|
|
.OrderByDescending(c => c.ParsedEpisodeInfo.Quality)
|
2013-04-15 03:41:39 +02:00
|
|
|
|
.ThenBy(c => c.Episodes.Select(e => e.EpisodeNumber).MinOrDefault())
|
|
|
|
|
.ThenBy(c => c.Report.Age);
|
2013-04-07 09:30:37 +02:00
|
|
|
|
|
2013-05-14 08:22:25 +02:00
|
|
|
|
var downloadedReports = new List<int>();
|
|
|
|
|
|
2013-04-07 09:30:37 +02:00
|
|
|
|
foreach (var episodeParseResult in qualifiedReports)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-05-14 08:22:25 +02:00
|
|
|
|
if (downloadedReports.Intersect(episodeParseResult.Episodes.Select(e => e.Id)).Any()) continue;
|
|
|
|
|
|
2013-04-07 09:30:37 +02:00
|
|
|
|
_downloadService.DownloadReport(episodeParseResult);
|
2013-05-14 08:22:25 +02:00
|
|
|
|
downloadedReports.AddRange(episodeParseResult.Episodes.Select(e => e.Id));
|
2013-04-07 09:30:37 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.WarnException("Couldn't add report to download queue. " + episodeParseResult, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-23 08:14:55 +02:00
|
|
|
|
_logger.Info("RSS Sync Completed. Reports found: {0}, Fetches attempted: {1}", reports.Count, qualifiedReports.Count());
|
2013-04-07 09:30:37 +02:00
|
|
|
|
}
|
2013-04-27 04:03:34 +02:00
|
|
|
|
|
|
|
|
|
public void Execute(RssSyncCommand message)
|
|
|
|
|
{
|
|
|
|
|
Sync();
|
|
|
|
|
}
|
2013-04-07 09:30:37 +02:00
|
|
|
|
}
|
|
|
|
|
}
|