Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support auto-generate publish contents #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/dotnet-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ jobs:
with:
dotnet-version: 8.x

- name: Prepare
run: |
dotnet nuget add source --username sun128764 --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/MIRIMIRIM/index.json"
dotnet restore

- name: Publish the application
run: dotnet publish OKP.Core --configuration ${{ matrix.configuration }} --runtime ${{ matrix.runtime-identifier }} /p:PublishAot=false

Expand Down
18 changes: 1 addition & 17 deletions OKP.Core/Interface/Acgnx/AcgnxAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,7 @@ private bool Valid()
throw new ArgumentNullException(nameof(torrent.Data.TorrentObject));
}

if (template.Content != null && template.Content.ToLower().EndsWith(".html"))
{
Log.Debug("开始寻找{Site} .html文件 {File}", site, template.Content);
var templateFile = FileHelper.ParseFileFullPath(template.Content, torrent.SettingPath);
if (File.Exists(templateFile))
{
Log.Debug("找到了{Site} .html文件 {File}", site, template.Content);
template.Content = File.ReadAllText(templateFile);
}
else
{
Log.Error("发布模板看起来是个.html文件,但是这个.html文件不存在{NewLine}" +
"{Source}-->{Dest}", Environment.NewLine, template.Content, templateFile);
return false;
}
}
return true;
return ValidTemplate(template, site, torrent.SettingPath);
}
}

Expand Down
18 changes: 1 addition & 17 deletions OKP.Core/Interface/Acgrip/AcgripAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,7 @@ private bool Valid()
return false;
}
}
if (template.Content != null && template.Content.ToLower().EndsWith(".bbcode"))
{
Log.Debug("开始寻找{Site} bbcode文件 {File}", site, template.Content);
var templateFile = FileHelper.ParseFileFullPath(template.Content, torrent.SettingPath);
if (File.Exists(templateFile))
{
Log.Debug("找到了{Site} bbcode文件 {File}", site, templateFile);
template.Content = File.ReadAllText(templateFile);
}
else
{
Log.Error("发布模板看起来是个.bbcode文件,但是这个.bbcode文件不存在{NewLine}" +
"{Source}-->{Dest}", Environment.NewLine, template.Content, templateFile);
return false;
}
}
return true;
return ValidTemplate(template, site, torrent.SettingPath);
}
}
}
124 changes: 123 additions & 1 deletion OKP.Core/Interface/AdapterBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,131 @@
namespace OKP.Core.Interface
using Serilog;
using Markdig;
using Converter.MarkdownToBBCode.Shared;
using Markdig.Extensions.EmphasisExtras;
using Markdig.Parsers;
using OKP.Core.Utils;

namespace OKP.Core.Interface
{
internal abstract class AdapterBase
{
public abstract Task<HttpResult> PingAsync();
public abstract Task<HttpResult> PostAsync();

internal static string ConvertMarkdownToHtml(string mdContent)
{
return Markdown.ToHtml(mdContent);
}

internal static string ConvertMarkdownToBBCode(string mdContent)
{
var pipeline = new MarkdownPipelineBuilder().EnableTrackTrivia().UseEmphasisExtras(EmphasisExtraOptions.Strikethrough).Build();
var document = MarkdownParser.Parse(mdContent, pipeline);

using var sw = new StringWriter();
var renderer = new BBCodeRenderer(BBCodeType.NexusMods, pipeline, false, false, sw);
renderer.Render(document);
renderer.Writer.Flush();

return renderer.Writer.ToString() ?? string.Empty;
}

private enum ContentType
{
Text,
MarkdownFile,
HtmlFile,
BBCodeFile,
}

private static string GetContentExt(ContentType contentType)
{
return contentType switch
{
ContentType.MarkdownFile => ".md",
ContentType.HtmlFile => ".html",
ContentType.BBCodeFile => ".bbcode",
_ => throw new ArgumentOutOfRangeException(nameof(contentType), contentType, null)
};
}

private static ContentType GetContentType(string site)
{
return site switch
{
"dmhy" => ContentType.HtmlFile,
"acgnx_asia" => ContentType.HtmlFile,
"acgnx_global" => ContentType.HtmlFile,
"acgrip" => ContentType.BBCodeFile,
"bangumi" => ContentType.HtmlFile,
"nyaa" => ContentType.MarkdownFile,
_ => throw new ArgumentOutOfRangeException(nameof(site), site, null)
};
}

internal static bool ValidTemplate(TorrentContent.Template template, string site, string? settingPath)
{
if (template.Content == null) return false;
var contentType = ContentType.Text;

var span = template.Content.AsSpan();
if (span.IndexOf('\n') != -1) return true;

if (span.EndsWith(".md", StringComparison.OrdinalIgnoreCase))
{
contentType = ContentType.MarkdownFile;
}
else if (span.EndsWith(".html", StringComparison.OrdinalIgnoreCase))
{
contentType = ContentType.HtmlFile;
}
else if (span.EndsWith(".bbcode", StringComparison.OrdinalIgnoreCase))
{
contentType = ContentType.BBCodeFile;
}
else
{
return true;
}

var siteContentType = GetContentType(site);
var siteContentExt = GetContentExt(siteContentType);
var contentExt = GetContentExt(contentType);
if (contentType != siteContentType)
{
Log.Debug("不存在{Site} {FileExt}文件,将用 {File} 生成", site, siteContentExt, template.Content);
}
else
{
Log.Debug("开始寻找{Site} {FileExt}文件 {File}", site, siteContentExt, template.Content);
}
var templateFile = FileHelper.ParseFileFullPath(template.Content, settingPath);
if (File.Exists(templateFile))
{
Log.Debug("找到了{FileExt}文件 {File}", contentExt, templateFile);
var text = File.ReadAllText(templateFile);
if (contentType == siteContentType)
{
template.Content = text;
}
else
{
template.Content = siteContentType switch
{
ContentType.HtmlFile => ConvertMarkdownToHtml(text),
ContentType.BBCodeFile => ConvertMarkdownToBBCode(text),
_ => throw new ArgumentOutOfRangeException()
};
}
}
else
{
Log.Error("发布模板不存在{NewLine}{Source}-->{Dest}", Environment.NewLine, template.Content, templateFile);
return false;
}

return true;
}
}
public class HttpResult
{
Expand Down
18 changes: 1 addition & 17 deletions OKP.Core/Interface/Bangumi/BangumiAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,7 @@ private bool Valid()
Log.Fatal("{Site} torrent.Data?.TorrentObject is null", site);
throw new ArgumentNullException(nameof(torrent.Data.TorrentObject));
}
if (template.Content != null && template.Content.ToLower().EndsWith(".html"))
{
Log.Debug("开始寻找{Site} html文件 {File}", site, template.Content);
var templateFile = FileHelper.ParseFileFullPath(template.Content, torrent.SettingPath);
if (File.Exists(templateFile))
{
Log.Debug("找到了{Site} html文件 {File}", site, templateFile);
template.Content = File.ReadAllText(templateFile);
}
else
{
Log.Error("发布模板看起来是个.html文件,但是这个.html文件不存在{NewLine}" +
"{Source}-->{Dest}", template.Content, Environment.NewLine, templateFile);
return false;
}
}
return true;
return ValidTemplate(template, site, torrent.SettingPath);
}
private static List<string?> CastTags(List<ContentTypes>? tags)
{
Expand Down
19 changes: 2 additions & 17 deletions OKP.Core/Interface/Dmhy/DmhyAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,8 @@ private bool Valid()
Log.Fatal("{Site} torrent.Data?.TorrentObject is null", site);
throw new ArgumentNullException(nameof(torrent.Data.TorrentObject));
}
if (template.Content != null && template.Content.ToLower().EndsWith(".html"))
{
Log.Debug("开始寻找{Site} html文件 {File}", site, template.Content);
var templateFile = FileHelper.ParseFileFullPath(template.Content, torrent.SettingPath);
if (File.Exists(templateFile))
{
Log.Debug("找到了{Site} html文件 {File}", site, templateFile);
template.Content = File.ReadAllText(templateFile);
}
else
{
Log.Error("发布模板看起来是个.html文件,但是这个.html文件不存在{NewLine}" +
"{Source}-->{Dest}", template.Content, Environment.NewLine, templateFile);
return false;
}
}
return true;

return ValidTemplate(template, site, torrent.SettingPath);
}
}
}
18 changes: 1 addition & 17 deletions OKP.Core/Interface/Nyaa/NyaaAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,7 @@ private bool Valid()
return false;
}
}
if (template.Content != null && template.Content.ToLower().EndsWith(".md"))
{
Log.Debug("开始寻找{Site} .md文件 {File}", site, template.Content);
var templateFile = FileHelper.ParseFileFullPath(template.Content, torrent.SettingPath);
if (File.Exists(templateFile))
{
Log.Debug("找到了{Site} .md文件 {File}", site, template.Content);
template.Content = File.ReadAllText(templateFile);
}
else
{
Log.Error("发布模板看起来是个.md文件,但是这个.md文件不存在{NewLine}" +
"{Source}-->{Dest}", Environment.NewLine, template.Content, templateFile);
return false;
}
}
return true;
return ValidTemplate(template, site, torrent.SettingPath);
}

private NyaaTorrentFlags SetFlags(List<NyaaTorrentFlags>? flags, List<ContentTypes>? tags)
Expand Down
21 changes: 20 additions & 1 deletion OKP.Core/Interface/TorrentContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public enum ContentTypes
App,
Game
}
public static TorrentContent Build(string filename, string settingFile, string appLocation)
public static TorrentContent Build(string filename, string settingFile, string? baseTemplate, string appLocation)
{
var settingFilePath = settingFile;

Expand All @@ -116,6 +116,7 @@ public static TorrentContent Build(string filename, string settingFile, string a
}

var torrentC = TomlParseHelper.DeserializeTorrentContent(settingFilePath);
ProcessTemplate(torrentC, baseTemplate);
torrentC.SettingPath = Path.GetDirectoryName(settingFilePath);
//if (!File.Exists(torrentC.CookiePath))
//{
Expand Down Expand Up @@ -194,6 +195,24 @@ public static TorrentContent Build(string filename, string settingFile, string a

return torrentC;
}

private static void ProcessTemplate(TorrentContent torrentC, string? baseTemplate)
{
if (baseTemplate is null) return;
if (torrentC.IntroTemplate is null) return;

if (Path.GetDirectoryName(baseTemplate) == "")
{
baseTemplate = Path.Combine(Environment.CurrentDirectory, baseTemplate);
}

// Automatically generate for sites with missing publishing content
foreach (var site in torrentC.IntroTemplate.Where(site => string.IsNullOrEmpty(site.Content)))
{
site.Content = baseTemplate;
}
}

public bool IsV2()
{
if (Data?.TorrentObject is null)
Expand Down
1 change: 1 addition & 0 deletions OKP.Core/OKP.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Released under the GNU GPLv3+.
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AC.Converter.MarkdownToBBCode.Shared" Version="1.0.0" />
<PackageReference Include="BencodeNET" Version="5.0.0" />
<PackageReference Include="Polly.Extensions.Http" Version="3.0.0" />
<PackageReference Include="Serilog" Version="4.0.1" />
Expand Down
Loading
Loading