Skip to content

Commit

Permalink
try support nyaa torrentflags
Browse files Browse the repository at this point in the history
  • Loading branch information
MIRIMIRIM committed Jul 28, 2024
1 parent 8f1449c commit fa090aa
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
51 changes: 51 additions & 0 deletions OKP.Core/Interface/Nyaa/NyaaAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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();
Expand Down Expand Up @@ -84,6 +86,28 @@ public override async Task<HttpResult> 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();
Expand Down Expand Up @@ -141,5 +165,32 @@ private bool Valid()
}
return true;
}

private NyaaTorrentFlags SetFlags(List<NyaaTorrentFlags>? flags, List<ContentTypes>? 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;
}
}
}
11 changes: 11 additions & 0 deletions OKP.Core/Interface/TorrentContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public TorrentData(string filename)
public bool IsFinished { get; set; }
public string? CookiePath { get; set; }
public List<ContentTypes>? Tags { get; set; }
public List<NyaaTorrentFlags>? TorrentFlags { get; set; }
public class Template
{
public string? Site { get; set; }
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fa090aa

Please sign in to comment.