You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/src/NzbDrone.Core/Download/DownloadSeedConfigProvider.cs

91 lines
3.0 KiB

using System;
using NLog;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Download.Clients;
using NzbDrone.Core.Download.History;
using NzbDrone.Core.Indexers;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.Download
{
public interface IDownloadSeedConfigProvider
{
TorrentSeedConfiguration GetSeedConfiguration(string infoHash);
}
public class DownloadSeedConfigProvider : IDownloadSeedConfigProvider
{
private readonly Logger _logger;
private readonly ISeedConfigProvider _indexerSeedConfigProvider;
private readonly IDownloadHistoryService _downloadHistoryService;
private class CachedSeedConfiguration
{
public int IndexerId { get; set; }
public bool Discography { get; set; }
}
private readonly ICached<CachedSeedConfiguration> _cacheDownloads;
public DownloadSeedConfigProvider(IDownloadHistoryService downloadHistoryService, ISeedConfigProvider indexerSeedConfigProvider, ICacheManager cacheManager, Logger logger)
{
_logger = logger;
_indexerSeedConfigProvider = indexerSeedConfigProvider;
_downloadHistoryService = downloadHistoryService;
_cacheDownloads = cacheManager.GetRollingCache<CachedSeedConfiguration>(GetType(), "indexerByHash", TimeSpan.FromHours(1));
}
public TorrentSeedConfiguration GetSeedConfiguration(string infoHash)
{
if (infoHash.IsNullOrWhiteSpace())
{
return null;
}
infoHash = infoHash.ToUpper();
var cachedConfig = _cacheDownloads.Get(infoHash, () => FetchIndexer(infoHash));
if (cachedConfig == null)
{
return null;
}
var seedConfig = _indexerSeedConfigProvider.GetSeedConfiguration(cachedConfig.IndexerId, cachedConfig.Discography);
return seedConfig;
}
private CachedSeedConfiguration FetchIndexer(string infoHash)
{
var historyItem = _downloadHistoryService.GetLatestGrab(infoHash);
if (historyItem == null)
{
_logger.Debug("No download history item for infohash {0}, unable to provide seed configuration", infoHash);
return null;
}
ParsedAlbumInfo parsedAlbumInfo = null;
if (historyItem.Release != null)
{
parsedAlbumInfo = Parser.Parser.ParseAlbumTitle(historyItem.Release.Title);
}
if (parsedAlbumInfo == null)
{
_logger.Debug("No parsed title in download history item for infohash {0}, unable to provide seed configuration", infoHash);
return null;
}
return new CachedSeedConfiguration
{
IndexerId = historyItem.IndexerId,
Discography = parsedAlbumInfo.Discography
};
}
}
}