Files
Readarr/src/NzbDrone.Core/Indexers/SeedConfigProvider.cs
T
leaty da262f3d95 New: Removing rtorrent downloads when seeding criteria have been met
(cherry picked from commit 411be4d0116f0739bb9c71235312d0c5a26dd3a2)
2021-12-15 11:55:55 +00:00

91 lines
2.7 KiB
C#

using System;
using NzbDrone.Common.Cache;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Download.Clients;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.Indexers
{
public interface ISeedConfigProvider
{
TorrentSeedConfiguration GetSeedConfiguration(RemoteBook release);
TorrentSeedConfiguration GetSeedConfiguration(int indexerId, bool fullSeason);
}
public class SeedConfigProvider : ISeedConfigProvider, IHandle<IndexerSettingUpdatedEvent>
{
private readonly IIndexerFactory _indexerFactory;
private readonly ICached<SeedCriteriaSettings> _cache;
public SeedConfigProvider(IIndexerFactory indexerFactory, ICacheManager cacheManager)
{
_indexerFactory = indexerFactory;
_cache = cacheManager.GetRollingCache<SeedCriteriaSettings>(GetType(), "criteriaByIndexer", TimeSpan.FromHours(1));
}
public TorrentSeedConfiguration GetSeedConfiguration(RemoteBook remoteBook)
{
if (remoteBook.Release.DownloadProtocol != DownloadProtocol.Torrent)
{
return null;
}
if (remoteBook.Release.IndexerId == 0)
{
return null;
}
return GetSeedConfiguration(remoteBook.Release.IndexerId, remoteBook.ParsedBookInfo.Discography);
}
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.DiscographySeedTime : seedCriteria.SeedTime;
if (seedTime.HasValue)
{
seedConfig.SeedTime = TimeSpan.FromMinutes(seedTime.Value);
}
return seedConfig;
}
private SeedCriteriaSettings FetchSeedCriteria(int indexerId)
{
try
{
var indexer = _indexerFactory.Get(indexerId);
var torrentIndexerSettings = indexer.Settings as ITorrentIndexerSettings;
return torrentIndexerSettings?.SeedCriteria;
}
catch (ModelNotFoundException)
{
return null;
}
}
public void Handle(IndexerSettingUpdatedEvent message)
{
_cache.Clear();
}
}
}