134 lines
4.7 KiB
C#
134 lines
4.7 KiB
C#
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
||
using CloudBuilder.Core.DependencyInjection.Task;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace CloudBuilder.Topshelf.Task.Novel
|
||
{
|
||
//task:SplitFileChapterTask encoding:GB2312 pattern: @"第([^】]+)章" input:D:\NET8\AI\frxxz.txt output:D:\NET8\AI\NovelSplitter
|
||
//input可以是文件名,也可以是目錄名
|
||
//output輸出目錄
|
||
//pattern可能自定義分割表達式
|
||
public class SplitFileChapterTask : IScheduleTask
|
||
{
|
||
private readonly IApplicationService service;
|
||
|
||
public SplitFileChapterTask(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 output = AppDomain.CurrentDomain.BaseDirectory;
|
||
|
||
//如果是文件名,輸出到同一目錄下
|
||
if (File.Exists(input))
|
||
{
|
||
output = Path.GetDirectoryName(input) + "\\";
|
||
}
|
||
else if (Directory.Exists(input))
|
||
{
|
||
output = input;
|
||
}
|
||
|
||
if (bodyDict.ContainsKey("output") && !string.IsNullOrEmpty(bodyDict["output"]))
|
||
{
|
||
output = bodyDict["output"];
|
||
//如果目錄不存在,創建目錄
|
||
if (!Directory.Exists(output)) Directory.CreateDirectory(output);
|
||
}
|
||
|
||
|
||
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)
|
||
{
|
||
SaveFile(encoding, file, output, pattern);
|
||
}
|
||
}
|
||
|
||
private void SaveFile(string encoding, string fileName, string output, string pattern)
|
||
{
|
||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||
|
||
string[] lines = File.ReadAllLines(fileName, Encoding.GetEncoding(encoding));
|
||
|
||
string title = string.Empty;
|
||
|
||
int chapterIndex = 1;
|
||
StringBuilder chapter = new StringBuilder();
|
||
foreach (string line in lines)
|
||
{
|
||
Match match = Regex.Match(line, pattern);
|
||
if (match.Success)
|
||
{
|
||
// 提取第一个分组(即括号内的内容)
|
||
//string value = match.Groups[1].Value;
|
||
|
||
//第一次找到是空的,下次找到新的保存之前的
|
||
if (!string.IsNullOrEmpty(title) && chapter.Length > 0)
|
||
{
|
||
File.WriteAllText(Path.Combine(output, Path.GetFileNameWithoutExtension(fileName) + string.Format("_{0}_{1}", chapterIndex++,title) + Path.GetExtension(fileName)), chapter.ToString(), Encoding.UTF8);
|
||
|
||
chapter = new StringBuilder();
|
||
}
|
||
|
||
title = line;
|
||
}
|
||
|
||
if (!string.IsNullOrEmpty(title))
|
||
{
|
||
chapter.AppendLine(line);
|
||
}
|
||
}
|
||
//最后一章
|
||
if (!string.IsNullOrEmpty(title) && chapter.Length > 0)
|
||
{
|
||
File.WriteAllText(Path.Combine(output, Path.GetFileNameWithoutExtension(fileName) + string.Format("_{0}_{1}", chapterIndex++, title) + Path.GetExtension(fileName)), chapter.ToString(), Encoding.UTF8);
|
||
}
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
}
|
||
}
|