From fa090aa76ce5f364012679f8881f6eed623dd0a0 Mon Sep 17 00:00:00 2001 From: MIRIMIRIM <59959583+MIRIMIRIM@users.noreply.github.com> Date: Sun, 28 Jul 2024 16:06:22 +0800 Subject: [PATCH] try support nyaa torrentflags --- OKP.Core/Interface/Nyaa/NyaaAdapter.cs | 51 ++++++++++++++++++++++++++ OKP.Core/Interface/TorrentContent.cs | 11 ++++++ 2 files changed, 62 insertions(+) diff --git a/OKP.Core/Interface/Nyaa/NyaaAdapter.cs b/OKP.Core/Interface/Nyaa/NyaaAdapter.cs index 64c42e0..354ca39 100644 --- a/OKP.Core/Interface/Nyaa/NyaaAdapter.cs +++ b/OKP.Core/Interface/Nyaa/NyaaAdapter.cs @@ -18,6 +18,7 @@ internal class NyaaAdapter : AdapterBase private readonly string pingUrl = "upload"; private readonly string postUrl = "upload"; private string category; + private NyaaTorrentFlags torrentFlags; private const string site = "nyaa"; public NyaaAdapter(TorrentContent torrent, Template template) { @@ -41,6 +42,7 @@ public NyaaAdapter(TorrentContent torrent, Template template) httpClientHandler.UseProxy = true; } category = CategoryHelper.SelectCategory(torrent.Tags, site); + torrentFlags = SetFlags(torrent.TorrentFlags, torrent.Tags); if (!Valid()) { IOHelper.ReadLine(); @@ -84,6 +86,28 @@ public override async Task PostAsync() { new StringContent(torrent.About??""), "information" }, { new StringContent(template.Content??""), "description" }, }; + + if (torrentFlags != NyaaTorrentFlags.None) + { + var yes = new StringContent("y"); + if (torrentFlags.HasFlag(NyaaTorrentFlags.Anonymous)) + { + form.Add(yes, "is_anonymous" ); + } + if (torrentFlags.HasFlag(NyaaTorrentFlags.Complete)) + { + form.Add(yes, "is_complete"); + } + if (torrentFlags.HasFlag(NyaaTorrentFlags.Hidden)) + { + form.Add(yes, "is_hidden"); + } + if (torrentFlags.HasFlag(NyaaTorrentFlags.Remake)) + { + form.Add(yes, "is_remake"); + } + } + Log.Verbose("{Site} formdata content: {@MultipartFormDataContent}", site, form); var result = await httpClient.PostAsyncWithRetry(postUrl, form); var raw = await result.Content.ReadAsStringAsync(); @@ -141,5 +165,32 @@ private bool Valid() } return true; } + + private NyaaTorrentFlags SetFlags(List? flags, List? tags) + { + var flag = NyaaTorrentFlags.None; + if (flags is not null) + { + foreach (var v in flags) + { + if (v == NyaaTorrentFlags.None) + { + flag = NyaaTorrentFlags.None; + break; + } + flag |= v; + } + } + + if (tags is not null) + { + if (tags.Contains(ContentTypes.Batch) || tags.Contains(ContentTypes.Collection)) + { + flag |= NyaaTorrentFlags.Complete; + } + } + + return flag; + } } } diff --git a/OKP.Core/Interface/TorrentContent.cs b/OKP.Core/Interface/TorrentContent.cs index d34755b..de3e742 100644 --- a/OKP.Core/Interface/TorrentContent.cs +++ b/OKP.Core/Interface/TorrentContent.cs @@ -50,6 +50,7 @@ public TorrentData(string filename) public bool IsFinished { get; set; } public string? CookiePath { get; set; } public List? Tags { get; set; } + public List? TorrentFlags { get; set; } public class Template { public string? Site { get; set; } @@ -254,6 +255,16 @@ public void DisplayFileTree() } Log.Information("文件列表:{NewLine}{FileList}", Environment.NewLine, fileList); } + + [Flags] + public enum NyaaTorrentFlags + { + None = 0b_0, + Anonymous = 0b_1, + Hidden = 0b_10, + Remake = 0b_100, + Complete = 0b_1000, + } } public class UserProperties