Implement TorrentMetadata

This commit is contained in:
Michael Feinbier 2023-09-18 20:43:23 +02:00
parent 9bff767feb
commit 2e70764dd9
7 changed files with 137 additions and 11 deletions

View File

@ -162,10 +162,10 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.PutioTests
items.Should().HaveCount(6);
}
[TestCase("1", 5)]
[TestCase("2", 1)]
[TestCase("3", 0)]
public void getItems_contains_only_items_with_matching_parent_id(string configuredParentId, int expectedCount)
[TestCase(1, 5)]
[TestCase(2, 1)]
[TestCase(3, 0)]
public void getItems_contains_only_items_with_matching_parent_id(long configuredParentId, int expectedCount)
{
GivenTorrents(new List<PutioTorrent>
{
@ -177,7 +177,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.PutioTests
_completed_different_parent
});
_settings.SaveParentId = configuredParentId;
_settings.SaveParentId = configuredParentId.ToString();
Subject.GetItems().Should().HaveCount(expectedCount);
}

View File

@ -0,0 +1,70 @@
using Moq;
using NUnit.Framework;
using NzbDrone.Common.Http;
using NzbDrone.Core.Download.Clients.Putio;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.Download.DownloadClientTests.PutioTests
{
public class PutioProxyFixtures : CoreTest<PutioProxy>
{
[Test]
public void test_GetTorrentMetadata_createsNewObject()
{
ClientGetWillReturn<PutioConfigResponse>("{\"status\":\"OK\",\"value\":null}");
var mt = Subject.GetTorrentMetadata(new PutioTorrent { Id = 1 }, new PutioSettings());
Assert.IsNotNull(mt);
Assert.AreEqual(1, mt.Id);
Assert.IsFalse(mt.Downloaded);
}
[Test]
public void test_GetTorrentMetadata_returnsExistingObject()
{
ClientGetWillReturn<PutioConfigResponse>("{\"status\":\"OK\",\"value\":{\"id\":4711,\"downloaded\":true}}");
var mt = Subject.GetTorrentMetadata(new PutioTorrent { Id = 1 }, new PutioSettings());
Assert.IsNotNull(mt);
Assert.AreEqual(4711, mt.Id);
Assert.IsTrue(mt.Downloaded);
}
[Test]
public void test_GetAllTorrentMetadata_filters_properly()
{
var json = @"{
""config"": {
""sonarr_123"": {
""downloaded"": true,
""id"": 123
},
""another_key"": {
""foo"": ""bar""
},
""sonarr_456"": {
""downloaded"": true,
""id"": 456
}
},
""status"": ""OK""
}";
ClientGetWillReturn<PutioAllConfigResponse>(json);
var list = Subject.GetAllTorrentMetadata(new PutioSettings());
Assert.IsTrue(list.ContainsKey("123"));
Assert.IsTrue(list.ContainsKey("456"));
Assert.AreEqual(list.Count, 2);
Assert.IsTrue(list["123"].Downloaded);
Assert.IsTrue(list["456"].Downloaded);
}
private void ClientGetWillReturn<TResult>(string obj)
where TResult : new()
{
Mocker.GetMock<IHttpClient>()
.Setup(s => s.Get<TResult>(It.IsAny<HttpRequest>()))
.Returns<HttpRequest>(r => new HttpResponse<TResult>(new HttpResponse(r, new HttpHeader(), obj)));
}
}
}

View File

@ -84,7 +84,7 @@ namespace NzbDrone.Core.Download.Clients.Putio
var item = new DownloadClientItem
{
DownloadId = torrent.Id.ToString(),
Category = Settings.SaveParentId,
Category = Settings.SaveParentId?.ToString(),
Title = torrent.Name,
TotalSize = torrent.Size,
RemainingSize = torrent.Size - torrent.Downloaded,
@ -165,12 +165,12 @@ namespace NzbDrone.Core.Download.Clients.Putio
public override DownloadClientInfo GetStatus()
{
var destDir = string.Format("{0}", Settings.SaveParentId);
var destDir = new OsPath(Settings.DownloadPath);
return new DownloadClientInfo
{
IsLocalhost = false,
OutputRootFolders = new List<OsPath> { _remotePathMappingService.RemapRemoteToLocal(Settings.Url, new OsPath(destDir)) }
OutputRootFolders = new List<OsPath> { destDir }
};
}

View File

@ -19,6 +19,7 @@ namespace NzbDrone.Core.Download.Clients.Putio
public class PutioProxy : IPutioProxy
{
private const string _configPrefix = "sonarr_";
private readonly Logger _logger;
private readonly IHttpClient _httpClient;
@ -66,6 +67,39 @@ namespace NzbDrone.Core.Download.Clients.Putio
Execute<PutioGenericResponse>(BuildRequest(HttpMethod.Get, "account/settings", settings));
}
public PutioTorrentMetadata GetTorrentMetadata(PutioTorrent torrent, PutioSettings settings)
{
var metadata = Execute<PutioConfigResponse>(BuildRequest(HttpMethod.Get, "config/" + _configPrefix + torrent.Id, settings));
if (metadata.Resource.Value != null)
{
_logger.Debug("Found metadata for torrent: {0} {1}", torrent.Id, metadata.Resource.Value);
return metadata.Resource.Value;
}
return new PutioTorrentMetadata
{
Id = torrent.Id,
Downloaded = false
};
}
public Dictionary<string, PutioTorrentMetadata> GetAllTorrentMetadata(PutioSettings settings)
{
var metadata = Execute<PutioAllConfigResponse>(BuildRequest(HttpMethod.Get, "config", settings));
var result = new Dictionary<string, PutioTorrentMetadata>();
foreach (var item in metadata.Resource.Config)
{
if (item.Key.StartsWith(_configPrefix))
{
var torrentId = item.Key.Substring(_configPrefix.Length);
result[torrentId] = item.Value;
}
}
return result;
}
private HttpRequestBuilder BuildRequest(HttpMethod method, string endpoint, PutioSettings settings)
{
var requestBuilder = new HttpRequestBuilder("https://api.put.io/v2")

View File

@ -15,4 +15,14 @@ namespace NzbDrone.Core.Download.Clients.Putio
{
public List<PutioTorrent> Transfers { get; set; }
}
public class PutioConfigResponse : PutioGenericResponse
{
public PutioTorrentMetadata Value { get; set; }
}
public class PutioAllConfigResponse : PutioGenericResponse
{
public Dictionary<string, PutioTorrentMetadata> Config { get; set; }
}
}

View File

@ -3,6 +3,7 @@ using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Validation;
using NzbDrone.Core.Validation.Paths;
namespace NzbDrone.Core.Download.Clients.Putio
{
@ -11,6 +12,7 @@ namespace NzbDrone.Core.Download.Clients.Putio
public PutioSettingsValidator()
{
RuleFor(c => c.OAuthToken).NotEmpty().WithMessage("Please provide an OAuth token");
RuleFor(c => c.DownloadPath).IsValidPath().WithMessage("Please provide a valid local path");
RuleFor(c => c.SaveParentId).Matches(@"^\.?[0-9]*$", RegexOptions.IgnoreCase).WithMessage("Allowed characters 0-9");
}
}
@ -29,11 +31,14 @@ namespace NzbDrone.Core.Download.Clients.Putio
[FieldDefinition(0, Label = "OAuth Token", Type = FieldType.Password)]
public string OAuthToken { get; set; }
[FieldDefinition(1, Label = "Save Parent ID", Type = FieldType.Textbox, HelpText = "If you provide a folder id here the torrents will be saved in that directory")]
[FieldDefinition(1, Label = "Save Parent Folder ID", Type = FieldType.Textbox, HelpText = "Adding a parent folder ID specific to Sonarr avoids conflicts with unrelated non-Sonarr downloads. Using a parent folder is optional, but strongly recommended.")]
public string SaveParentId { get; set; }
[FieldDefinition(2, Label = "Disable Download", Type = FieldType.Checkbox, HelpText = "If enabled, Sonarr will not download completed files from Put.io. Useful if you manually sync with rclone or similar")]
public bool DisableDownload { get; set; }
[FieldDefinition(2, Label = "Download completed transfers", Type = FieldType.Checkbox, HelpText = "If enabled, Sonarr will download completed files from Put.io. If you manually sync with rclone or similar, disable this")]
public bool DownloadFiles { get; set; }
[FieldDefinition(3, Label = "Download Path", Type = FieldType.Path, HelpText = "Path were Put.io is downloading to or if downloading is disabled where the mounts are expected")]
public string DownloadPath { get; set; }
public NzbDroneValidationResult Validate()
{

View File

@ -34,4 +34,11 @@ namespace NzbDrone.Core.Download.Clients.Putio
public string Hash { get; set; }
}
public class PutioTorrentMetadata
{
public bool Downloaded { get; set; }
public long Id { get; set; }
}
}