1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-24 08:42:19 +02:00

Untested code to get a cached SeedCriteria by infohash to rtorrent handler

This commit is contained in:
Taloth Saldono 2021-02-04 00:16:36 +01:00
parent f3f2648ce5
commit 6143d20462
3 changed files with 129 additions and 22 deletions

View File

@ -22,6 +22,7 @@ public class RTorrent : TorrentClientBase<RTorrentSettings>
{ {
private readonly IRTorrentProxy _proxy; private readonly IRTorrentProxy _proxy;
private readonly IRTorrentDirectoryValidator _rTorrentDirectoryValidator; private readonly IRTorrentDirectoryValidator _rTorrentDirectoryValidator;
private readonly IDownloadSeedConfigProvider _downloadSeedConfigProvider;
public RTorrent(IRTorrentProxy proxy, public RTorrent(IRTorrentProxy proxy,
ITorrentFileInfoReader torrentFileInfoReader, ITorrentFileInfoReader torrentFileInfoReader,
@ -29,12 +30,14 @@ public RTorrent(IRTorrentProxy proxy,
IConfigService configService, IConfigService configService,
IDiskProvider diskProvider, IDiskProvider diskProvider,
IRemotePathMappingService remotePathMappingService, IRemotePathMappingService remotePathMappingService,
IDownloadSeedConfigProvider downloadSeedConfigProvider,
IRTorrentDirectoryValidator rTorrentDirectoryValidator, IRTorrentDirectoryValidator rTorrentDirectoryValidator,
Logger logger) Logger logger)
: base(torrentFileInfoReader, httpClient, configService, diskProvider, remotePathMappingService, logger) : base(torrentFileInfoReader, httpClient, configService, diskProvider, remotePathMappingService, logger)
{ {
_proxy = proxy; _proxy = proxy;
_rTorrentDirectoryValidator = rTorrentDirectoryValidator; _rTorrentDirectoryValidator = rTorrentDirectoryValidator;
_downloadSeedConfigProvider = downloadSeedConfigProvider;
} }
public override void MarkItemAsImported(DownloadClientItem downloadClientItem) public override void MarkItemAsImported(DownloadClientItem downloadClientItem)
@ -53,6 +56,8 @@ public override void MarkItemAsImported(DownloadClientItem downloadClientItem)
Settings.TvImportedCategory, downloadClientItem.Title); Settings.TvImportedCategory, downloadClientItem.Title);
} }
} }
// TODO: Set post-import view
} }
protected override string AddFromMagnetLink(RemoteEpisode remoteEpisode, string hash, string magnetLink) protected override string AddFromMagnetLink(RemoteEpisode remoteEpisode, string hash, string magnetLink)
@ -147,6 +152,9 @@ public override IEnumerable<DownloadClientItem> GetItems()
item.Status = DownloadItemStatus.Paused; item.Status = DownloadItemStatus.Paused;
} }
var seedConfig = _downloadSeedConfigProvider.GetSeedConfiguration(torrent.Hash);
// TODO: Handle
// No stop ratio data is present, so do not delete // No stop ratio data is present, so do not delete
item.CanMoveFiles = item.CanBeRemoved = false; item.CanMoveFiles = item.CanBeRemoved = false;

View File

@ -0,0 +1,79 @@
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;
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;
class CachedSeedConfiguration
{
public int IndexerId { get; set; }
public bool FullSeason { 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.FullSeason);
return seedConfig;
}
private CachedSeedConfiguration FetchIndexer(string infoHash)
{
var historyItem = _downloadHistoryService.GetLatestDownloadHistoryItem(infoHash);
if (historyItem == null)
{
_logger.Debug("No download history item for infohash {0}, unable to provide seed configuration", infoHash);
return null;
}
var parsedEpisodeInfo = Parser.Parser.ParseTitle(historyItem.Release.Title);
if (parsedEpisodeInfo == null)
{
_logger.Debug("No parsed title in download history item item for infohash {0}, unable to provide seed configuration", infoHash);
return null;
}
return new CachedSeedConfiguration
{
IndexerId = historyItem.IndexerId,
FullSeason = parsedEpisodeInfo.FullSeason
};
}
}
}

View File

@ -1,9 +1,8 @@
using System; using System;
using System.Linq; using NzbDrone.Common.Cache;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using NzbDrone.Core.Download.Clients; using NzbDrone.Core.Download.Clients;
using NzbDrone.Core.Indexers; using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.Indexers namespace NzbDrone.Core.Indexers
@ -11,15 +10,20 @@ namespace NzbDrone.Core.Indexers
public interface ISeedConfigProvider public interface ISeedConfigProvider
{ {
TorrentSeedConfiguration GetSeedConfiguration(RemoteEpisode release); TorrentSeedConfiguration GetSeedConfiguration(RemoteEpisode release);
TorrentSeedConfiguration GetSeedConfiguration(int indexerId, bool fullSeason);
} }
public class SeedConfigProvider : ISeedConfigProvider public class SeedConfigProvider : ISeedConfigProvider, IHandle<IndexerSettingUpdatedEvent>
{ {
private readonly IIndexerFactory _indexerFactory; private readonly IIndexerFactory _indexerFactory;
public SeedConfigProvider(IIndexerFactory indexerFactory) private readonly ICached<SeedCriteriaSettings> _cache;
public SeedConfigProvider(IIndexerFactory indexerFactory, ICacheManager cacheManager)
{ {
_indexerFactory = indexerFactory; _indexerFactory = indexerFactory;
_cache = cacheManager.GetRollingCache<SeedCriteriaSettings>(GetType(), "criteriaByIndexer", TimeSpan.FromHours(1));
} }
public TorrentSeedConfiguration GetSeedConfiguration(RemoteEpisode remoteEpisode) public TorrentSeedConfiguration GetSeedConfiguration(RemoteEpisode remoteEpisode)
@ -27,33 +31,49 @@ public TorrentSeedConfiguration GetSeedConfiguration(RemoteEpisode remoteEpisode
if (remoteEpisode.Release.DownloadProtocol != DownloadProtocol.Torrent) return null; if (remoteEpisode.Release.DownloadProtocol != DownloadProtocol.Torrent) return null;
if (remoteEpisode.Release.IndexerId == 0) return null; if (remoteEpisode.Release.IndexerId == 0) return null;
return GetSeedConfiguration(remoteEpisode.Release.IndexerId, remoteEpisode.ParsedEpisodeInfo.FullSeason);
}
public TorrentSeedConfiguration GetSeedConfiguration(int indexerId, bool fullSeason)
{
if (indexerId == 0) return null;
var seedCriteria = _cache.Get(indexerId.ToString(), () => FetchSeedCriteria(indexerId));
if (seedCriteria == null) return null;
var seedConfig = new TorrentSeedConfiguration
{
Ratio = seedCriteria.SeedRatio
};
var seedTime = fullSeason ? seedCriteria.SeasonPackSeedTime : seedCriteria.SeedTime;
if (seedTime.HasValue)
{
seedConfig.SeedTime = TimeSpan.FromMinutes(seedTime.Value);
}
return seedConfig;
}
private SeedCriteriaSettings FetchSeedCriteria(int indexerId)
{
try try
{ {
var indexer = _indexerFactory.Get(remoteEpisode.Release.IndexerId); var indexer = _indexerFactory.Get(indexerId);
var torrentIndexerSettings = indexer.Settings as ITorrentIndexerSettings; var torrentIndexerSettings = indexer.Settings as ITorrentIndexerSettings;
if (torrentIndexerSettings != null && torrentIndexerSettings.SeedCriteria != null) return torrentIndexerSettings?.SeedCriteria;
{
var seedConfig = new TorrentSeedConfiguration
{
Ratio = torrentIndexerSettings.SeedCriteria.SeedRatio
};
var seedTime = remoteEpisode.ParsedEpisodeInfo.FullSeason ? torrentIndexerSettings.SeedCriteria.SeasonPackSeedTime : torrentIndexerSettings.SeedCriteria.SeedTime;
if (seedTime.HasValue)
{
seedConfig.SeedTime = TimeSpan.FromMinutes(seedTime.Value);
}
return seedConfig;
}
} }
catch (ModelNotFoundException) catch (ModelNotFoundException)
{ {
return null; return null;
} }
}
return null; public void Handle(IndexerSettingUpdatedEvent message)
{
_cache.Clear();
} }
} }
} }