pull/10609/merge
Mark Mendoza 3 weeks ago committed by GitHub
commit 317b43a5af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,173 @@
using System;
using System.Collections.Generic;
using FluentValidation.Results;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Core.Blocklisting;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Download.Clients.RQbit;
using NzbDrone.Core.Download.Clients.rTorrent;
using NzbDrone.Core.Localization;
using NzbDrone.Core.MediaFiles.TorrentInfo;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.RemotePathMappings;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Download.Clients.rQbit
{
public class RQBit : TorrentClientBase<RQbitSettings>
{
private readonly IRQbitProxy _proxy;
private readonly IDownloadSeedConfigProvider _downloadSeedConfigProvider;
public RQBit(IRQbitProxy proxy,
ITorrentFileInfoReader torrentFileInfoReader,
IHttpClient httpClient,
IConfigService configService,
IDiskProvider diskProvider,
IRemotePathMappingService remotePathMappingService,
IDownloadSeedConfigProvider downloadSeedConfigProvider,
IRTorrentDirectoryValidator rTorrentDirectoryValidator,
ILocalizationService localizationService,
IBlocklistService blocklistService,
Logger logger)
: base(torrentFileInfoReader, httpClient, configService, diskProvider, remotePathMappingService, localizationService, blocklistService, logger)
{
_proxy = proxy;
_downloadSeedConfigProvider = downloadSeedConfigProvider;
}
public override IEnumerable<DownloadClientItem> GetItems()
{
var torrents = _proxy.GetTorrents(Settings);
_logger.Debug("Retrieved metadata of {0} torrents in client", torrents.Count);
var items = new List<DownloadClientItem>();
foreach (var torrent in torrents)
{
// // Ignore torrents with an empty path
// if (torrent.Path.IsNullOrWhiteSpace())
// {
// _logger.Warn("Torrent '{0}' has an empty download path and will not be processed. Adjust this to an absolute path in rTorrent", torrent.Name);
// continue;
// }
//
// if (torrent.Path.StartsWith("."))
// {
// _logger.Warn("Torrent '{0}' has a download path starting with '.' and will not be processed. Adjust this to an absolute path in rTorrent", torrent.Name);
// continue;
// }
var item = new DownloadClientItem();
item.DownloadClientInfo = DownloadClientItemClientInfo.FromDownloadClient(this, false);
item.Title = torrent.Name;
item.DownloadId = torrent.Hash;
item.OutputPath = _remotePathMappingService.RemapRemoteToLocal(Settings.Host, new OsPath(torrent.Path));
item.TotalSize = torrent.TotalSize;
item.RemainingSize = torrent.RemainingSize;
item.Category = torrent.Category;
item.SeedRatio = torrent.Ratio;
if (torrent.DownRate > 0)
{
var secondsLeft = torrent.RemainingSize / torrent.DownRate;
item.RemainingTime = TimeSpan.FromSeconds(secondsLeft);
}
else
{
item.RemainingTime = TimeSpan.Zero;
}
if (torrent.IsFinished)
{
item.Status = DownloadItemStatus.Completed;
}
else if (torrent.IsActive)
{
item.Status = DownloadItemStatus.Downloading;
}
else if (!torrent.IsActive)
{
item.Status = DownloadItemStatus.Paused;
}
// Grab cached seedConfig
var seedConfig = _downloadSeedConfigProvider.GetSeedConfiguration(torrent.Hash);
if (item.DownloadClientInfo.RemoveCompletedDownloads && torrent.IsFinished && seedConfig != null)
{
var canRemove = false;
if (torrent.Ratio / 1000.0 >= seedConfig.Ratio)
{
_logger.Trace($"{item} has met seed ratio goal of {seedConfig.Ratio}");
canRemove = true;
}
else if (DateTimeOffset.Now - DateTimeOffset.FromUnixTimeSeconds(torrent.FinishedTime) >= seedConfig.SeedTime)
{
_logger.Trace($"{item} has met seed time goal of {seedConfig.SeedTime} minutes");
canRemove = true;
}
else
{
_logger.Trace($"{item} seeding goals have not yet been reached");
}
// Check if torrent is finished and if it exceeds cached seedConfig
item.CanMoveFiles = item.CanBeRemoved = canRemove;
}
items.Add(item);
}
return items;
}
public override void RemoveItem(DownloadClientItem item, bool deleteData)
{
_proxy.RemoveTorrent(item.DownloadId, deleteData, Settings);
}
public override DownloadClientInfo GetStatus()
{
return new DownloadClientInfo
{
IsLocalhost = Settings.Host == "127.0.0.1" || Settings.Host == "localhost",
};
}
protected override void Test(List<ValidationFailure> failures)
{
failures.AddIfNotNull(TestConnection());
if (failures.HasErrors())
{
return;
}
// failures.AddIfNotNull(TestGetTorrents());
// failures.AddIfNotNull(TestDirectory());
}
private ValidationFailure TestConnection()
{
var version = _proxy.GetVersion(Settings);
return null;
}
protected override string AddFromMagnetLink(RemoteMovie remoteMovie, string hash, string magnetLink)
{
return _proxy.AddTorrentFromUrl(magnetLink, Settings);
}
protected override string AddFromTorrentFile(RemoteMovie remoteMovie, string hash, string filename, byte[] fileContent)
{
return _proxy.AddTorrentFromFile(filename, fileContent, Settings);
}
public override string Name => "RQBit";
}
}

@ -0,0 +1,8 @@
namespace NzbDrone.Core.Download.Clients.RQBit;
public class RQBitFile
{
public string FileName { get; set; }
public int FileSize { get; set; }
public int FileDownloaded { get; set; }
}

@ -0,0 +1,17 @@
namespace NzbDrone.Core.Download.Clients.RQBit;
public class RQBitTorrent
{
public long id { get; set; }
public string Name { get; set; }
public string Hash { get; set; }
public long TotalSize { get; set; }
public long RemainingSize { get; set; }
public string Category { get; set; }
public double? Ratio { get; set; }
public long DownRate { get; set; }
public bool IsFinished { get; set; }
public bool IsActive { get; set; }
public long FinishedTime { get; set; }
public string Path { get; set; }
}

@ -0,0 +1,218 @@
using System.Collections.Generic;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using NLog;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Http;
using NzbDrone.Core.Download.Clients.RQbit;
using NzbDrone.Core.Download.Clients.RQBit;
using NzbDrone.Core.Download.Clients.RQBit.ResponseModels;
namespace NzbDrone.Core.Download.Clients.rQbit;
public interface IRQbitProxy
{
string GetVersion(RQbitSettings settings);
List<RQBitTorrent> GetTorrents(RQbitSettings settings);
void RemoveTorrent(string hash, bool removeData, RQbitSettings settings);
string AddTorrentFromUrl(string torrentUrl, RQbitSettings settings);
string AddTorrentFromFile(string fileName, byte[] fileContent, RQbitSettings settings);
void SetTorrentLabel(string hash, string label, RQbitSettings settings);
bool HasHashTorrent(string hash, RQbitSettings settings);
}
public class RQbitProxy : IRQbitProxy
{
private readonly IHttpClient _httpClient;
private readonly Logger _logger;
public RQbitProxy(IHttpClient httpClient, ICacheManager cacheManager, Logger logger)
{
_httpClient = httpClient;
_logger = logger;
}
public string GetVersion(RQbitSettings settings)
{
var version = "";
var request = BuildRequest(settings).Resource("");
var response = _httpClient.Get(request.Build());
if (response.StatusCode == HttpStatusCode.OK)
{
var jsonStr = Encoding.UTF8.GetString(response.ResponseData);
var rootResponse = JsonConvert.DeserializeObject<RootResponse>(jsonStr);
version = rootResponse.Version;
}
else
{
_logger.Error("Failed to get torrent version");
}
return version;
}
public List<RQBitTorrent> GetTorrents(RQbitSettings settings)
{
List<RQBitTorrent> result = null;
var request = BuildRequest(settings).Resource("/torrents");
var response = _httpClient.Get(request.Build());
TorrentListResponse torrentList = null;
if (response.StatusCode == HttpStatusCode.OK)
{
var jsonStr = Encoding.UTF8.GetString(response.ResponseData);
torrentList = JsonConvert.DeserializeObject<TorrentListResponse>(jsonStr);
}
else
{
_logger.Error("Failed to get torrent version");
}
if (torrentList != null)
{
result = new List<RQBitTorrent>();
foreach (var torrentListItem in torrentList.torrents)
{
var torrentResponse = getTorrent(torrentListItem.InfoHash, settings);
var torrentStatsResponse = getTorrentStats(torrentListItem.InfoHash, settings);
var torrent = new RQBitTorrent();
torrent.id = torrentListItem.Id;
torrent.Name = torrentResponse.Name;
torrent.Hash = torrentResponse.InfoHash;
torrent.TotalSize = torrentStatsResponse.TotalBytes;
torrent.Path = torrentResponse.OutputFolder + torrentResponse.Name;
var statsLive = torrentStatsResponse.Live;
if (statsLive != null && statsLive.Snapshot != null)
{
torrent.DownRate = statsLive.DownloadSpeed.Mbps * 1048576; // mib/sec -> bytes per second
}
torrent.RemainingSize = torrentStatsResponse.TotalBytes - torrentStatsResponse.ProgressBytes;
torrent.Ratio = torrentStatsResponse.UploadedBytes / torrentStatsResponse.ProgressBytes;
torrent.IsFinished = torrentStatsResponse.Finished;
torrent.IsActive = torrentStatsResponse.State != "paused";
result.Add(torrent);
}
}
return result;
}
public void RemoveTorrent(string info_hash, bool removeData, RQbitSettings settings)
{
var endpoint = removeData ? "/delete" : "/forget";
var itemRequest = BuildRequest(settings).Resource("/torrents/" + info_hash + endpoint);
_httpClient.Post(itemRequest.Build());
}
public string AddTorrentFromUrl(string torrentUrl, RQbitSettings settings)
{
string info_hash = null;
var itemRequest = BuildRequest(settings).Resource("/torrents?overwrite=true").Post().Build();
itemRequest.SetContent(torrentUrl);
var httpResponse = _httpClient.Post(itemRequest);
if (httpResponse.StatusCode != HttpStatusCode.OK)
{
return info_hash;
}
var jsonStr = Encoding.UTF8.GetString(httpResponse.ResponseData);
var response = JsonConvert.DeserializeObject<PostTorrentResponse>(jsonStr);
if (response.Details != null)
{
info_hash = response.Details.InfoHash;
}
return info_hash;
}
public string AddTorrentFromFile(string fileName, byte[] fileContent, RQbitSettings settings)
{
string info_hash = null;
var itemRequest = BuildRequest(settings)
.Post()
.Resource("/torrents?overwrite=true")
.Build();
itemRequest.SetContent(fileContent);
var httpResponse = _httpClient.Post(itemRequest);
if (httpResponse.StatusCode != HttpStatusCode.OK)
{
return info_hash;
}
var jsonStr = Encoding.UTF8.GetString(httpResponse.ResponseData);
var response = JsonConvert.DeserializeObject<PostTorrentResponse>(jsonStr);
if (response.Details != null)
{
info_hash = response.Details.InfoHash;
}
return info_hash;
}
public void SetTorrentLabel(string hash, string label, RQbitSettings settings)
{
_logger.Warn("Torrent labels currently unsupported by RQBit");
}
public bool HasHashTorrent(string hash, RQbitSettings settings)
{
var result = true;
var rqBitTorrentResponse = getTorrent(hash, settings);
if (rqBitTorrentResponse == null || string.IsNullOrWhiteSpace(rqBitTorrentResponse.InfoHash))
{
result = false;
}
return result;
}
private TorrentResponse getTorrent(string info_hash, RQbitSettings settings)
{
TorrentResponse result = null;
var itemRequest = BuildRequest(settings).Resource("/torrents/" + info_hash);
var itemResponse = _httpClient.Get(itemRequest.Build());
if (itemResponse.StatusCode != HttpStatusCode.OK)
{
return result;
}
var jsonStr = Encoding.UTF8.GetString(itemResponse.ResponseData);
result = JsonConvert.DeserializeObject<TorrentResponse>(jsonStr);
return result;
}
private TorrentV1StatResponse getTorrentStats(string info_hash, RQbitSettings settings)
{
TorrentV1StatResponse result = null;
var itemRequest = BuildRequest(settings).Resource("/torrents/" + info_hash + "/stats/v1");
var itemResponse = _httpClient.Get(itemRequest.Build());
if (itemResponse.StatusCode != HttpStatusCode.OK)
{
return result;
}
var jsonStr = Encoding.UTF8.GetString(itemResponse.ResponseData);
result = JsonConvert.DeserializeObject<TorrentV1StatResponse>(jsonStr);
return result;
}
private HttpRequestBuilder BuildRequest(RQbitSettings settings)
{
var requestBuilder = new HttpRequestBuilder(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase)
{
LogResponseContent = true,
};
return requestBuilder;
}
}

@ -0,0 +1,36 @@
using NzbDrone.Core.Annotations;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Download.Clients.RQbit
{
public class RQbitSettings : DownloadClientSettingsBase<RQbitSettings>
{
private static readonly RQbitSettingsValidator Validator = new RQbitSettingsValidator();
public RQbitSettings()
{
Host = "localhost";
Port = 3030;
UrlBase = "/";
}
[FieldDefinition(0, Label = "Host", Type = FieldType.Textbox)]
public string Host { get; set; }
[FieldDefinition(1, Label = "Port", Type = FieldType.Textbox)]
public int Port { get; set; }
[FieldDefinition(2, Label = "UseSsl", Type = FieldType.Checkbox, HelpText = "DownloadClientQbittorrentSettingsUseSslHelpText")]
public bool UseSsl { get; set; }
[FieldDefinition(3, Label = "UrlBase", Type = FieldType.Textbox, Advanced = true, HelpText = "DownloadClientSettingsUrlBaseHelpText")]
[FieldToken(TokenField.HelpText, "UrlBase", "clientName", "RQBit")]
[FieldToken(TokenField.HelpText, "UrlBase", "url", "http://[host]:[port]/")]
public string UrlBase { get; set; }
public override NzbDroneValidationResult Validate()
{
return new NzbDroneValidationResult(Validator.Validate(this));
}
}
}

@ -0,0 +1,15 @@
using FluentValidation;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Download.Clients.RQbit;
public class RQbitSettingsValidator : AbstractValidator<RQbitSettings>
{
public RQbitSettingsValidator()
{
RuleFor(c => c.Host).ValidHost();
RuleFor(c => c.Port).InclusiveBetween(1, 65535);
RuleFor(c => c.UrlBase).ValidUrlBase();
}
}

@ -0,0 +1,26 @@
using System.Collections.Generic;
using Newtonsoft.Json;
namespace NzbDrone.Core.Download.Clients.RQBit.ResponseModels;
public class PostTorrentResponse
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("details")]
public PostTorrentDetailsResponse Details { get; set; }
[JsonProperty("output_folder")]
public string OutputFolder { get; set; }
[JsonProperty("seen_peers")]
public List<string> SeenPeers { get; set; }
}
public class PostTorrentDetailsResponse
{
[JsonProperty("info_hash")]
public string InfoHash { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("files")]
public List<TorrentFileResponse> Files { get; set; }
}

@ -0,0 +1,14 @@
using System.Collections.Generic;
using Newtonsoft.Json;
namespace NzbDrone.Core.Download.Clients.rQbit;
public class RootResponse
{
[JsonProperty("apis")]
public Dictionary<string, string> Apis { get; set; }
[JsonProperty("server")]
public string Server { get; set; }
[JsonProperty("version")]
public string Version { get; set; }
}

@ -0,0 +1,16 @@
using System.Collections.Generic;
using Newtonsoft.Json;
namespace NzbDrone.Core.Download.Clients.RQBit.ResponseModels;
public class TorrentFileResponse
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("components")]
public List<string> Components { get; set; }
[JsonProperty("length")]
public long Length { get; set; }
[JsonProperty("included")]
public bool Included { get; set; }
}

@ -0,0 +1,10 @@
using System.Collections.Generic;
using Newtonsoft.Json;
namespace NzbDrone.Core.Download.Clients.RQBit.ResponseModels;
public class TorrentListResponse
{
[JsonProperty("torrents")]
public List<TorrentListingResponse> torrents { get; set; }
}

@ -0,0 +1,11 @@
using Newtonsoft.Json;
namespace NzbDrone.Core.Download.Clients.RQBit.ResponseModels;
public class TorrentListingResponse
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("info_hash")]
public string InfoHash { get; set; }
}

@ -0,0 +1,16 @@
using System.Collections.Generic;
using Newtonsoft.Json;
namespace NzbDrone.Core.Download.Clients.RQBit.ResponseModels;
public class TorrentResponse
{
[JsonProperty("info_hash")]
public string InfoHash { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("files")]
public List<TorrentFileResponse> Files { get; set; }
[JsonProperty("output_folder")]
public string OutputFolder { get; set; }
}

@ -0,0 +1,11 @@
namespace NzbDrone.Core.Download.Clients.RQBit;
// https://github.com/ikatson/rqbit/blob/946ad3625892f4f40dde3d0e6bbc3030f68a973c/crates/librqbit/src/torrent_state/mod.rs#L65
public enum TorrentStatus
{
Initializing = 0,
Paused = 1,
Live = 2,
Error = 3,
Invalid = 4
}

@ -0,0 +1,89 @@
using System.Collections.Generic;
using Newtonsoft.Json;
namespace NzbDrone.Core.Download.Clients.RQBit.ResponseModels;
public class TorrentV1StatResponse
{
[JsonProperty("state")]
public string State { get; set; }
[JsonProperty("file_progress")]
public List<long> FileProgress { get; set; }
[JsonProperty("error")]
public string Error { get; set; }
[JsonProperty("progress_bytes")]
public long ProgressBytes { get; set; }
[JsonProperty("uploaded_bytes")]
public long UploadedBytes { get; set; }
[JsonProperty("total_bytes")]
public long TotalBytes { get; set; }
[JsonProperty("finished")]
public bool Finished { get; set; }
[JsonProperty("live")]
public TorrentV1StatLiveResponse Live { get; set; }
}
public class RQBitTorrentSpeedResponse
{
[JsonProperty("mbps")]
public long Mbps { get; set; }
[JsonProperty("human_readable")]
public string HumanReadable { get; set; }
}
public class TorrentV1StatLiveResponse
{
[JsonProperty("snapshot")]
public TorrentV1StatLiveSnapshotResponse Snapshot { get; set; }
[JsonProperty("download_speed")]
public RQBitTorrentSpeedResponse DownloadSpeed { get; set; }
[JsonProperty("upload_speed")]
public RQBitTorrentSpeedResponse UploadSpeed { get; set; }
}
public class TorrentV1StatLiveSnapshotResponse
{
[JsonProperty("downloaded_and_checked_bytes")]
public long DownloadedAndCheckedBytes { get; set; }
[JsonProperty("fetched_bytes")]
public long FetchedBytes { get; set; }
[JsonProperty("uploaded_bytes")]
public long UploadedBytes { get; set; }
[JsonProperty("downloaded_and_checked_pieces")]
public long DownloadedAndCheckedPieces { get; set; }
[JsonProperty("total_piece_downloaded_ms")]
public long TotalPieceDownloadedMs { get; set; }
[JsonProperty("peer_stats")]
public TorrentV1StatLiveSnapshotPeerStatsResponse PeerStats { get; set; }
}
public class TorrentV1StatLiveSnapshotPeerStatsResponse
{
[JsonProperty("queued")]
public int Queued { get; set; }
[JsonProperty("connecting")]
public int Connecting { get; set; }
[JsonProperty("live")]
public int Live { get; set; }
[JsonProperty("seen")]
public int Seen { get; set; }
[JsonProperty("dead")]
public int Dead { get; set; }
[JsonProperty("not_needed")]
public int NotNeeded { get; set; }
[JsonProperty("steals")]
public int Steals { get; set; }
}
Loading…
Cancel
Save