CloudBuilder.Topshelf/Task/Novel/ImportBookTask.cs
owenchen 597b88d075 ow
2026-05-28 15:49:25 +08:00

137 lines
4.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using CloudBuilder.AI.Data;
using CloudBuilder.AI.Entity;
using CloudBuilder.AI.Policy;
using CloudBuilder.AI.Service;
using CloudBuilder.Core.DatabaseAccessor.Entity;
using CloudBuilder.Core.DependencyInjection.Task;
using CloudBuilder.Topshelf.Task.Novel.Process;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace CloudBuilder.Topshelf.Task.Novel
{
//task:ImportBookTask encoding:GB2312 pattern: @"第([^】]+)章" input:D:\NET8\AI\仙侠\凡人修仙传_忘语.txt
//input可以是文件名:D:\NET8\AI\仙侠\书名_作者.txt
//input也可以是目錄名:D:\NET8\AI\仙侠,必须是小说类型为结尾
//output輸出目錄
//pattern可能自定義分割表達式
public class ImportBookTask : IScheduleTask
{
private readonly IApplicationService service;
public ImportBookTask(IApplicationService service)
{
this.service = service;
}
public void Run(Dictionary<string, string> bodyDict)
{
if (!bodyDict.ContainsKey("input") || string.IsNullOrEmpty(bodyDict["input"])) return;
string input = bodyDict["input"];
string pattern = @"第([^】]+)章";
if (bodyDict.ContainsKey("pattern") && !string.IsNullOrEmpty(bodyDict["pattern"]))
pattern = bodyDict["pattern"];
string encoding = "UTF-8";
if (bodyDict.ContainsKey("encoding") && !string.IsNullOrEmpty(bodyDict["encoding"])) encoding = bodyDict["encoding"];
List<string> files = PackFiles(input);
foreach (string file in files)
{
ProcessBook(file, encoding, pattern);
}
}
public void ProcessBook(string file, string encoding, string pattern)
{
AiBookData data = new AiBookData();
AiBookEntity book = new AiBookEntity();
book.Guid = Guid.NewGuid().ToString();
string[] temps = Path.GetFileNameWithoutExtension(file).Split('_');
if (temps == null || temps.Length != 2)
{
Console.WriteLine("文件格式要求书名_作者.txt");
return;
}
book.Title = temps[0];
book.Author = temps[1];
temps = file.Split('\\');
book.BookType = temps[temps.Length - 2];
//book.FilePath = file;
book.Status = BookStatusPolicy.SUBMIT;//S-待提交
ChapterSplitter chapterSplitter = new ChapterSplitter();
data.AiBook = book;
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
string context = File.ReadAllText(file, Encoding.GetEncoding(encoding));
List<AiChapterEntity> aiChapterEntities = chapterSplitter.SplitNovelIntoChapters(context, pattern);
if (aiChapterEntities == null || aiChapterEntities.Count() == 0) return;
aiChapterEntities.ToLookup(x => x.BookGuid = book.Guid);
aiChapterEntities.ToLookup(x => x.Status = ChapterStatusPolicy.PROCESS);
data.AiChapters = aiChapterEntities.ToArray();
var transactionOptions = new TransactionOptions
{
IsolationLevel = IsolationLevel.ReadCommitted,
Timeout = TimeSpan.FromSeconds(60 * 10)
};
using (TransactionScope _transactionScope = new TransactionScope(
TransactionScopeOption.Required,
transactionOptions,
TransactionScopeAsyncFlowOption.Enabled))
{
if (data.AiBook != null)
service.ServiceProvider.GetService<IAiBookService>().Insert(data);
_transactionScope.Complete();
}
}
private List<string> PackFiles(string input)
{
List<string> files = new List<string>();
if (Path.HasExtension(input) && !string.IsNullOrEmpty(input))
{
if (File.Exists(input))
{
files.Add(input);
}
}
else if (!string.IsNullOrEmpty(input))
{
if (Directory.Exists(input))
{
string[] tempFiles = Directory.GetFiles(input);
if (tempFiles != null && tempFiles.Length > 0)
{
files.AddRange(tempFiles);
}
}
}
return files;
}
}
}