Skip to content

Commit

Permalink
NEW! Add AddTorrentByUrl()
Browse files Browse the repository at this point in the history
- DelugeResponse
- Call
- Test
- Constants
- Readme
  • Loading branch information
rafaelkan committed Oct 29, 2024
1 parent 50f63d3 commit f6e920c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions DelugeRPCClient.Net.Tests/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
20 changes: 20 additions & 0 deletions DelugeRPCClient.Net.Tests/TorrentsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
7 changes: 7 additions & 0 deletions DelugeRPCClient.Net/Core/DelugeResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ internal class DelugeResponsee<T>
[JsonProperty(PropertyName = "error")]
public DelugeError Error { get; set; }
}

internal class ResultEntry
{
public bool Success { get; set; }
public string Hash { get; set; }

}
}
28 changes: 27 additions & 1 deletion DelugeRPCClient.Net/DelugeClient.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -114,6 +115,31 @@ public async Task<Torrent> AddTorrentByFile(string file, TorrentOptions options
return await GetTorrent(hash);
}

/// <summary>
/// Add a new torrent by .torrent url
/// </summary>
/// <param name="file">url of the .torrent file</param>
/// <param name="options">Optional torrent options</param>
/// <returns>the torrent object</returns>
/// <exception cref="ArgumentException"></exception>
public async Task<Torrent> 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<string>(request);

request = CreateRequest("web.add_torrents", new object[] { new object[] { new { options = new object { }, path = pathTemp } } });
var resultAdd = await SendRequest<List<List<Object>>>(request);
return await GetTorrent(resultAdd.Select(item =>
new ResultEntry
{
Success = item[0] is bool success && success,
Hash = item[1] as string
}
).FirstOrDefault().Hash);
}

/// <summary>
/// Remove a torrent from deluge
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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#
Expand Down

0 comments on commit f6e920c

Please sign in to comment.