58 lines
2.3 KiB
C#
58 lines
2.3 KiB
C#
using Transmission.API.RPC;
|
|
using Transmission.API.RPC.Arguments;
|
|
using Transmission.API.RPC.Entity;
|
|
|
|
namespace AnimeAnnouncer.Torrents
|
|
{
|
|
/// <summary>
|
|
/// Thin wrapper around the Transmission RPC client for adding torrents to the server.
|
|
/// </summary>
|
|
public class TransmissionService
|
|
{
|
|
public Client Client { get; }
|
|
|
|
private TransmissionService(Client client)
|
|
{
|
|
Client = client;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a client and verifies the server is reachable by requesting session information.
|
|
/// Throws <see cref="InvalidOperationException"/> if the server doesn't respond with a valid session.
|
|
/// </summary>
|
|
public static async Task<TransmissionService> ConnectAsync(String host, String? username, String? password)
|
|
{
|
|
var client = new Client(host, null, username, password);
|
|
var sessionInformation = await client.GetSessionInformationAsync();
|
|
if (sessionInformation == null || String.IsNullOrEmpty(sessionInformation.Version))
|
|
throw new InvalidOperationException($"An error occurred while receiving session information from the rpc server at {host}");
|
|
|
|
return new TransmissionService(client);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a .torrent file to the server, saving to <paramref name="saveLocation"/>, and applies a seed ratio limit.
|
|
/// </summary>
|
|
public async Task<NewTorrentInfo> AddNewTorrentFileToServerAsync(byte[] torrentFileData, String saveLocation, bool paused = true, double seedRatioLimit = 1.0)
|
|
{
|
|
NewTorrent newTorrent = new()
|
|
{
|
|
DownloadDirectory = saveLocation,
|
|
Paused = paused,
|
|
Metainfo = Convert.ToBase64String(torrentFileData)
|
|
};
|
|
var torrentInfo = await Client.TorrentAddAsync(newTorrent);
|
|
var seedSettings = new TorrentSettings()
|
|
{
|
|
SeedRatioLimit = seedRatioLimit,
|
|
SeedRatioMode = 1,
|
|
IDs = [torrentInfo.ID]
|
|
};
|
|
|
|
// TorrentSetAsync returns void in Transmission.API.RPC 2.1.2, so it can't be awaited
|
|
Client.TorrentSet(seedSettings);
|
|
return torrentInfo;
|
|
}
|
|
}
|
|
}
|