Sonarr/NzbDrone.Core/Indexers/RssSyncService.cs

64 lines
2.1 KiB
C#
Raw Normal View History

2013-04-07 07:30:37 +00:00
using System;
using System.Linq;
using NLog;
using NzbDrone.Core.DecisionEngine;
using NzbDrone.Core.Download;
namespace NzbDrone.Core.Indexers
{
2013-04-23 06:06:05 +00:00
public interface IRssSyncService
2013-04-07 07:30:37 +00:00
{
void Sync();
}
2013-04-23 06:06:05 +00:00
public class RssSyncService : IRssSyncService
2013-04-07 07:30:37 +00: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");
var parseResults = _rssFetcherAndParser.Fetch();
var decisions = _downloadDecisionMaker.GetRssDecision(parseResults);
2013-04-23 06:06:05 +00:00
//TODO: this will download multiple of same episode if they show up in RSS. need to
2013-04-07 07:30:37 +00:00
var qualifiedReports = decisions
.Where(c => c.Approved)
.Select(c => c.Episode)
2013-04-07 07:30:37 +00:00
.OrderByDescending(c => c.Quality)
.ThenBy(c => c.Episodes.Select(e => e.EpisodeNumber).MinOrDefault())
.ThenBy(c => c.Report.Age);
2013-04-07 07:30:37 +00:00
2013-04-23 06:06:05 +00:00
2013-04-07 07:30:37 +00:00
foreach (var episodeParseResult in qualifiedReports)
{
try
{
_downloadService.DownloadReport(episodeParseResult);
}
catch (Exception e)
{
_logger.WarnException("Couldn't add report to download queue. " + episodeParseResult, e);
}
}
2013-04-23 06:06:05 +00:00
_logger.Info("RSS Sync Completed. Reports found: {0}, Fetches attempted: {1}", parseResults.Count, qualifiedReports.Count());
2013-04-07 07:30:37 +00:00
}
}
}