diff --git a/DelugeRPCClient.Net.Tests/Constants.cs b/DelugeRPCClient.Net.Tests/Constants.cs index 4752aa4..a411622 100644 --- a/DelugeRPCClient.Net.Tests/Constants.cs +++ b/DelugeRPCClient.Net.Tests/Constants.cs @@ -11,5 +11,6 @@ internal class Constants internal const string TestLabelName = "testlabel"; internal const string TorrentMagnet = "magnet:?xt=urn:btih:30987c19cf0eae3cf47766f387c621fa78a58ab9&dn=debian-9.2.1-amd64-netinst.iso"; internal const string TestTorrentFilename = "test.torrent"; + internal const string TestTorrentUrl = "https://archive.org/download/die_siedler_2_151/die_siedler_2_151_archive.torrent"; } } diff --git a/DelugeRPCClient.Net.Tests/TorrentsTests.cs b/DelugeRPCClient.Net.Tests/TorrentsTests.cs index 7bedb2a..8b0da9e 100644 --- a/DelugeRPCClient.Net.Tests/TorrentsTests.cs +++ b/DelugeRPCClient.Net.Tests/TorrentsTests.cs @@ -73,6 +73,26 @@ public async Task AddRemoveTorrentByFile() Assert.IsTrue(logoutResult); } + [TestMethod] + public async Task AddRemoveTorrentByUrl() + { + DelugeClient client = new DelugeClient(url: Constants.DelugeUrl, password: Constants.DelugePassword); + + bool loginResult = await client.Login(); + Assert.IsTrue(loginResult); + + Torrent torrent = await client.AddTorrentByUrl(Constants.TestTorrentUrl); + Assert.IsNotNull(torrent); + + Thread.Sleep(1000); + + bool removeTorrentResult = await client.RemoveTorrent(torrent.Hash); + Assert.IsTrue(removeTorrentResult); + + bool logoutResult = await client.Logout(); + Assert.IsTrue(logoutResult); + } + [TestMethod] public async Task PauseResumeTorrent() { diff --git a/DelugeRPCClient.Net/Core/DelugeResponse.cs b/DelugeRPCClient.Net/Core/DelugeResponse.cs index a1f6975..5282b96 100644 --- a/DelugeRPCClient.Net/Core/DelugeResponse.cs +++ b/DelugeRPCClient.Net/Core/DelugeResponse.cs @@ -16,4 +16,11 @@ internal class DelugeResponsee [JsonProperty(PropertyName = "error")] public DelugeError Error { get; set; } } + + internal class ResultEntry + { + public bool Success { get; set; } + public string Hash { get; set; } + + } } diff --git a/DelugeRPCClient.Net/DelugeClient.cs b/DelugeRPCClient.Net/DelugeClient.cs index 5285a2f..9daa1c6 100644 --- a/DelugeRPCClient.Net/DelugeClient.cs +++ b/DelugeRPCClient.Net/DelugeClient.cs @@ -1,4 +1,5 @@ -using DelugeRPCClient.Net.Models; +using DelugeRPCClient.Net.Core; +using DelugeRPCClient.Net.Models; using Newtonsoft.Json; using System; using System.Collections.Generic; @@ -114,6 +115,31 @@ public async Task AddTorrentByFile(string file, TorrentOptions options return await GetTorrent(hash); } + /// + /// Add a new torrent by .torrent url + /// + /// url of the .torrent file + /// Optional torrent options + /// the torrent object + /// + public async Task AddTorrentByUrl(string url, TorrentOptions options = null) + { + if (String.IsNullOrWhiteSpace(url)) throw new ArgumentException(nameof(url)); + var request = CreateRequest("web.download_torrent_from_url", url, options); + request.NullValueHandling = NullValueHandling.Ignore; + string pathTemp = await SendRequest(request); + + request = CreateRequest("web.add_torrents", new object[] { new object[] { new { options = new object { }, path = pathTemp } } }); + var resultAdd = await SendRequest>>(request); + return await GetTorrent(resultAdd.Select(item => + new ResultEntry + { + Success = item[0] is bool success && success, + Hash = item[1] as string + } + ).FirstOrDefault().Hash); + } + /// /// Remove a torrent from deluge /// diff --git a/README.md b/README.md index f83952e..fc962cf 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,12 @@ Add a torrent by .torrent file ```C# Torrent torrent = await client.AddTorrentByFile(torrentFilename); ``` +``` + +Add a torrent by .torrent url +```C# +Torrent torrent = await client.AddTorrentByUrl(torrentUrl); +``` #### Remove Torrent ```C#