Skip to content

Commit

Permalink
#36 読み込みサービスを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
TakenPt committed Apr 28, 2024
1 parent 0a39681 commit 7c74a35
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions Epub/KoeBook.Epub/Services/ScrapingClaudeStroyService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using KoeBook.Epub.Models;
using KoeBook.Core;
using System.Xml.Serialization;
using KoeBook.Core.Utilities;
using KoeBook.Epub.Contracts.Services;
using KoeBook.Core.Models;

namespace KoeBook.Epub.Services
{
public partial class ScrapingClaudeStroyService(ISplitBraceService splitBraceService)
{

private readonly ISplitBraceService _splitBraceService = splitBraceService;
public async ValueTask<EpubDocument> ScrapingAsync(string xmlText, string coverFillePath, string imgDirectory, Guid id, CancellationToken ct)

Check warning on line 17 in Epub/KoeBook.Epub/Services/ScrapingClaudeStroyService.cs

View workflow job for this annotation

GitHub Actions / test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 17 in Epub/KoeBook.Epub/Services/ScrapingClaudeStroyService.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
if (!FitXmlSchema(xmlText))
throw new EbookException(ExceptionType.XmlSchemaUnfulfilled);

var book = LoadBook(xmlText);

var paragraphbuilder = new SplittedLineBuilder();
var scriptLineBuilder = new SplittedLineBuilder();

var document = new EpubDocument(book.Title, "", coverFillePath, id);
document.Chapters.Add(new Chapter());
foreach (var section in book.Content.Sections)
{
var docSection = new Section(section.Title ?? "");
foreach (var paragraph in section.Paragraphs)
{
foreach (var item in paragraph.Texts)
{
switch (item)
{
case Text text:
paragraphbuilder.Append(text.InnerText);
scriptLineBuilder.Append(text.InnerText);
break;
case Ruby ruby:
paragraphbuilder.Append($"<ruby>{ruby.Rb}<rp>(</rp><rt>{ruby.Rt}</rt><rp>)</rp></ruby>");
scriptLineBuilder.Append($"{ruby.Rt}");
break;
default:
throw new EbookException(ExceptionType.XmlSchemaUnfulfilled);
}
}

foreach ((var textSplit, var scriptSplit) in
_splitBraceService.SplitBrace(paragraphbuilder.ToLinesAndClear()).Zip(
_splitBraceService.SplitBrace(scriptLineBuilder.ToLinesAndClear())
)
)
{
docSection.Elements.Add(new Paragraph() { Text = textSplit, ScriptLine = new ScriptLine(scriptSplit, "", "") });
}
}
document.Chapters.Single().Sections.Add(docSection);
}
return document;
}

internal bool FitXmlSchema(string xmlText)
{
// スキーマファイルのパスを入れる。
using var xsdFs = new FileStream(@"", FileMode.Open);
XmlSchemaSet schema = new XmlSchemaSet();
schema.Add("", XmlReader.Create(xsdFs));

XDocument xml = XDocument.Parse(xmlText);

bool errorExist = false;
xml.Validate(schema, (o, e) => errorExist = true);

return !errorExist;
}

internal Book LoadBook(string xmlText)
{
using var xmlStringReader = new StringReader(xmlText);
var serializer = new XmlSerializer(typeof(Book));
return (Book)serializer.Deserialize(xmlStringReader)

Check warning on line 84 in Epub/KoeBook.Epub/Services/ScrapingClaudeStroyService.cs

View workflow job for this annotation

GitHub Actions / test

Converting null literal or possible null value to non-nullable type.

Check warning on line 84 in Epub/KoeBook.Epub/Services/ScrapingClaudeStroyService.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
?? throw new EbookException(ExceptionType.XmlSchemaUnfulfilled);
}
}
}

0 comments on commit 7c74a35

Please sign in to comment.