Added MassTorrentAdd project

This commit is contained in:
2024-10-06 17:07:50 -04:00
parent 09f65f9a31
commit 5a0ffdad1e
6 changed files with 182 additions and 1 deletions

28
MassTorrentAdd/Dockerfile Normal file
View File

@@ -0,0 +1,28 @@
# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
USER app
WORKDIR /app
# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["MassTorrentAdd/MassTorrentAdd.csproj", "MassTorrentAdd/"]
RUN dotnet restore "./MassTorrentAdd/MassTorrentAdd.csproj"
COPY . .
WORKDIR "/src/MassTorrentAdd"
RUN dotnet build "./MassTorrentAdd.csproj" -c $BUILD_CONFIGURATION -o /app/build
# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./MassTorrentAdd.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MassTorrentAdd.dll"]

View File

@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="Transmission.API.RPC" Version="2.1.2" />
</ItemGroup>
<ItemGroup>
<None Update="appSettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

100
MassTorrentAdd/Program.cs Normal file
View File

@@ -0,0 +1,100 @@
using Microsoft.Extensions.Configuration;
using System.Xml.Linq;
using Transmission.API.RPC;
namespace MassTorrentAdd
{
internal class Program
{
static String watchFolder, transmissionHost, transmissionUsername, transmissionPassword, transmissionDownloadFolder, destinationFolder;
static Transmission.API.RPC.Client transmissionClient;
static void Main(string[] args)
{
if(args.Length == 0)
{
Console.WriteLine("Please provide the nyaa URL to target for download.");
return;
}
String url = args[0];
if (!url.Contains("rss"))
url = url + "&page=rss";
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
IConfigurationRoot configuration = builder.Build();
transmissionHost = configuration.GetValue<String>("transmissionURI");
transmissionUsername = configuration.GetValue<String>("transmissionUsername");
transmissionPassword = configuration.GetValue<String>("transmissionPassword");
destinationFolder = configuration.GetValue<String>("destinationFolder");
Console.WriteLine($"Using transmission server {transmissionHost}");
transmissionClient = new Transmission.API.RPC.Client(transmissionHost, null, transmissionUsername, transmissionPassword);
var sessionInformation = transmissionClient.GetSessionInformation();
if (sessionInformation == null || String.IsNullOrEmpty(sessionInformation.Version))
{
Console.WriteLine("An error occurred while receiving session information from the rpc server");
return;
}
HttpClient httpClient = new HttpClient();
var response = httpClient.GetStringAsync(url).Result;
if(response == null)
{
Console.WriteLine("An error occurred while contacting Nyaa for a response");
return;
}
XDocument doc = XDocument.Parse(response);
XNamespace nyaaNS = doc.Root.GetNamespaceOfPrefix("nyaa");
var items = from item in doc.Descendants("channel").Elements("item")
select new
{
Title = item.Element("title").Value,
Published = item.Element("pubDate").Value,
Category = item.Element(nyaaNS + "category").Value,
CategoryID = item.Element(nyaaNS + "categoryId").Value,
Size = item.Element(nyaaNS + "size").Value,
Link = item.Element("link").Value,
Description = item.Element("description").Value,
PostID = item.Element("guid").Value,
};
Console.WriteLine("Downloading ..");
foreach ( var item in items )
{
Console.WriteLine($"{item.Title}");
}
String transmissionDirectoryName = items.First().Title.Trim();
transmissionDirectoryName = System.Text.RegularExpressions.Regex.Replace(transmissionDirectoryName, @"E\d{2,3}.*?(\d{3,4}p)", " $1");
transmissionDirectoryName = destinationFolder + (destinationFolder.EndsWith("/") ? String.Empty : "/") + transmissionDirectoryName;
Console.WriteLine("to " + transmissionDirectoryName);
Console.WriteLine("Continue?");
if(Console.ReadKey().KeyChar != 'y')
{
Console.WriteLine("Exiting");
return;
}
foreach (var item in items)
{
var torrentData = httpClient.GetByteArrayAsync(item.Link).Result;
AddNewTorrentFileToServer(torrentData, transmissionDirectoryName);
}
Console.WriteLine("Completed");
}
private static void AddNewTorrentFileToServer(byte[] torrentFileData, String saveLocation)
{
Transmission.API.RPC.Entity.NewTorrent newTorrent = new()
{
DownloadDirectory = saveLocation,
Paused = true,
Metainfo = Convert.ToBase64String(torrentFileData)
};
var torrentInfo = transmissionClient.TorrentAdd(newTorrent);
transmissionClient.TorrentVerify(new object[] { torrentInfo.ID });
}
}
}

View File

@@ -0,0 +1,11 @@
{
"profiles": {
"MassTorrentAdd": {
"commandName": "Project",
"commandLineArgs": "https://nyaa.si/?f=0&c=1_0&q=The+Fable+VARYG"
},
"Container (Dockerfile)": {
"commandName": "Docker"
}
}
}

View File

@@ -0,0 +1,8 @@
{
"transmissionUserName": "transmission",
"transmissionPassword": "",
"transmissionURI": "",
//"watchFolder": "X:\\MP3\\Upload\\current",
"destinationFolder": "/pool/torrents/Animation",
"transmissionDownloadFolder": "/mnt/download/Upload"
}