Skip to content

Commit

Permalink
Merge pull request #18 from OUCC/feat/#17-1
Browse files Browse the repository at this point in the history
Epubライブラリのエラー調整
  • Loading branch information
aiueo-1234 authored Mar 16, 2024
2 parents e3a124c + 3adc1d4 commit 059eaf5
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 35 deletions.
51 changes: 50 additions & 1 deletion Epub/KoeBook.Epub.sln
Original file line number Diff line number Diff line change
@@ -1,25 +1,74 @@
Microsoft Visual Studio Solution File, Format Version 12.00

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.34607.79
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KoeBook.Epub", "KoeBook.Epub\KoeBook.Epub.csproj", "{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EpubConsoleApp", "EpubConsoleApp\EpubConsoleApp.csproj", "{DE1F3CE9-4F3B-4428-9293-D18446F333D8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KoeBook.Core", "..\KoeBook.Core\KoeBook.Core.csproj", "{9F11B1EF-8330-400E-A60C-CFC98E827AA7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|arm64 = Debug|arm64
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|arm64 = Release|arm64
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}.Debug|arm64.ActiveCfg = Debug|Any CPU
{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}.Debug|arm64.Build.0 = Debug|Any CPU
{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}.Debug|x64.ActiveCfg = Debug|Any CPU
{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}.Debug|x64.Build.0 = Debug|Any CPU
{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}.Debug|x86.ActiveCfg = Debug|Any CPU
{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}.Debug|x86.Build.0 = Debug|Any CPU
{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}.Release|Any CPU.Build.0 = Release|Any CPU
{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}.Release|arm64.ActiveCfg = Release|Any CPU
{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}.Release|arm64.Build.0 = Release|Any CPU
{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}.Release|x64.ActiveCfg = Release|Any CPU
{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}.Release|x64.Build.0 = Release|Any CPU
{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}.Release|x86.ActiveCfg = Release|Any CPU
{E8E224F2-D1E9-437E-8222-FF2F33DB3FCD}.Release|x86.Build.0 = Release|Any CPU
{DE1F3CE9-4F3B-4428-9293-D18446F333D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE1F3CE9-4F3B-4428-9293-D18446F333D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE1F3CE9-4F3B-4428-9293-D18446F333D8}.Debug|arm64.ActiveCfg = Debug|Any CPU
{DE1F3CE9-4F3B-4428-9293-D18446F333D8}.Debug|arm64.Build.0 = Debug|Any CPU
{DE1F3CE9-4F3B-4428-9293-D18446F333D8}.Debug|x64.ActiveCfg = Debug|Any CPU
{DE1F3CE9-4F3B-4428-9293-D18446F333D8}.Debug|x64.Build.0 = Debug|Any CPU
{DE1F3CE9-4F3B-4428-9293-D18446F333D8}.Debug|x86.ActiveCfg = Debug|Any CPU
{DE1F3CE9-4F3B-4428-9293-D18446F333D8}.Debug|x86.Build.0 = Debug|Any CPU
{DE1F3CE9-4F3B-4428-9293-D18446F333D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE1F3CE9-4F3B-4428-9293-D18446F333D8}.Release|Any CPU.Build.0 = Release|Any CPU
{DE1F3CE9-4F3B-4428-9293-D18446F333D8}.Release|arm64.ActiveCfg = Release|Any CPU
{DE1F3CE9-4F3B-4428-9293-D18446F333D8}.Release|arm64.Build.0 = Release|Any CPU
{DE1F3CE9-4F3B-4428-9293-D18446F333D8}.Release|x64.ActiveCfg = Release|Any CPU
{DE1F3CE9-4F3B-4428-9293-D18446F333D8}.Release|x64.Build.0 = Release|Any CPU
{DE1F3CE9-4F3B-4428-9293-D18446F333D8}.Release|x86.ActiveCfg = Release|Any CPU
{DE1F3CE9-4F3B-4428-9293-D18446F333D8}.Release|x86.Build.0 = Release|Any CPU
{9F11B1EF-8330-400E-A60C-CFC98E827AA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9F11B1EF-8330-400E-A60C-CFC98E827AA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F11B1EF-8330-400E-A60C-CFC98E827AA7}.Debug|arm64.ActiveCfg = Debug|arm64
{9F11B1EF-8330-400E-A60C-CFC98E827AA7}.Debug|arm64.Build.0 = Debug|arm64
{9F11B1EF-8330-400E-A60C-CFC98E827AA7}.Debug|x64.ActiveCfg = Debug|x64
{9F11B1EF-8330-400E-A60C-CFC98E827AA7}.Debug|x64.Build.0 = Debug|x64
{9F11B1EF-8330-400E-A60C-CFC98E827AA7}.Debug|x86.ActiveCfg = Debug|x86
{9F11B1EF-8330-400E-A60C-CFC98E827AA7}.Debug|x86.Build.0 = Debug|x86
{9F11B1EF-8330-400E-A60C-CFC98E827AA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F11B1EF-8330-400E-A60C-CFC98E827AA7}.Release|Any CPU.Build.0 = Release|Any CPU
{9F11B1EF-8330-400E-A60C-CFC98E827AA7}.Release|arm64.ActiveCfg = Release|arm64
{9F11B1EF-8330-400E-A60C-CFC98E827AA7}.Release|arm64.Build.0 = Release|arm64
{9F11B1EF-8330-400E-A60C-CFC98E827AA7}.Release|x64.ActiveCfg = Release|x64
{9F11B1EF-8330-400E-A60C-CFC98E827AA7}.Release|x64.Build.0 = Release|x64
{9F11B1EF-8330-400E-A60C-CFC98E827AA7}.Release|x86.ActiveCfg = Release|x86
{9F11B1EF-8330-400E-A60C-CFC98E827AA7}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
13 changes: 0 additions & 13 deletions Epub/KoeBook.Epub/EpubDocumentException.cs

This file was deleted.

6 changes: 3 additions & 3 deletions Epub/KoeBook.Epub/Services/EpubGenerateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace KoeBook.Epub.Services;

public class EpubGenerateService(ISoundGenerationService soundGenerationService, IEpubDocumentStoreService epubDocumentStoreService, IEpubCreateService epubCreateService) : IEpubGenerateService
{
private ISoundGenerationService _soundGenerationService = soundGenerationService;
private IEpubDocumentStoreService _documentStoreService = epubDocumentStoreService;
private IEpubCreateService _createService = epubCreateService;
private readonly ISoundGenerationService _soundGenerationService = soundGenerationService;
private readonly IEpubDocumentStoreService _documentStoreService = epubDocumentStoreService;
private readonly IEpubCreateService _createService = epubCreateService;

public async ValueTask<string> GenerateEpubAsync(BookScripts bookScripts, string tempDirectory, CancellationToken cancellationToken)
{
Expand Down
9 changes: 5 additions & 4 deletions Epub/KoeBook.Epub/Services/ScrapingAozoraService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using AngleSharp.Dom;
using AngleSharp.Html.Dom;
using AngleSharp.Io;
using KoeBook.Core;
using KoeBook.Epub.Contracts.Services;
using KoeBook.Epub.Models;
using static KoeBook.Epub.Utility.ScrapingHelper;
Expand Down Expand Up @@ -29,11 +30,11 @@ public async ValueTask<EpubDocument> ScrapingAsync(string url, string coverFileP

// title の取得
var bookTitle = doc.QuerySelector(".title")
?? throw new EpubDocumentException($"Failed to get title properly.\nYou may be able to get proper URL at {GetCardUrl(url)}");
?? throw new EbookException(ExceptionType.WebScrapingFailed, $"タイトルの取得に失敗しました。\n以下のリンクから正しい小説のリンクを取得してください。\n{GetCardUrl(url)}");

// auther の取得
var bookAuther = doc.QuerySelector(".author")
?? throw new EpubDocumentException($"Failed to get auther properly.\nYou may be able to get proper URL at {GetCardUrl(url)}");
?? throw new EbookException(ExceptionType.WebScrapingFailed, $"著者の取得に失敗しました。\n以下のリンクから正しい小説のリンクを取得してください。\n{GetCardUrl(url)}");

// EpubDocument の生成
var document = new EpubDocument(TextReplace(bookTitle.InnerHtml), TextReplace(bookAuther.InnerHtml), coverFilePath, id)
Expand Down Expand Up @@ -111,10 +112,10 @@ public async ValueTask<EpubDocument> ScrapingAsync(string url, string coverFileP
if (midashi != null)
{
if (midashi.Id == null)
throw new EpubDocumentException("Unecpected structure of HTML File: div tag with class=\"midashi_anchor\", but id=\"midashi___\" exist");
throw new EbookException(ExceptionType.WebScrapingFailed, "予期しないHTMLの構造です。\nclass=\"midashi_anchor\"ではなくid=\"midashi___\"が存在します。");

if (!int.TryParse(midashi.Id.Replace("midashi", ""), out var midashiId))
throw new EpubDocumentException($"Unexpected id of Anchor tag was found: id = {midashi.Id}");
throw new EbookException(ExceptionType.WebScrapingFailed, $"予期しないアンカータグが見つかりました。id = {midashi.Id}");

if (contentsIds.Contains(midashiId))
{
Expand Down
27 changes: 14 additions & 13 deletions Epub/KoeBook.Epub/Services/ScrapingNaroService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using AngleSharp.Dom;
using AngleSharp.Html.Dom;
using AngleSharp.Io;
using KoeBook.Core;
using KoeBook.Epub.Contracts.Services;
using KoeBook.Epub.Models;
using static KoeBook.Epub.Utility.ScrapingHelper;
Expand All @@ -26,12 +27,12 @@ public async ValueTask<EpubDocument> ScrapingAsync(string url, string coverFileP

// title の取得
var bookTitleElement = doc.QuerySelector(".novel_title")
?? throw new EpubDocumentException($"Failed to get title properly.\nUrl may be not collect");
?? throw new EbookException(ExceptionType.WebScrapingFailed, $"タイトルを取得できませんでした");
var bookTitle = bookTitleElement.InnerHtml;

// auther の取得
var bookAutherElement = doc.QuerySelector(".novel_writername")
?? throw new EpubDocumentException($"Failed to get auther properly.\nUrl may be not collect");
?? throw new EbookException(ExceptionType.WebScrapingFailed, $"著者を取得できませんでした");
var bookAuther = string.Empty;
if (bookAutherElement.QuerySelector("a") is IHtmlAnchorElement bookAutherAnchorElement)
{
Expand All @@ -54,13 +55,13 @@ public async ValueTask<EpubDocument> ScrapingAsync(string url, string coverFileP
var result = await client.SendAsync(message, ct).ConfigureAwait(false);
var test = await result.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
if (!result.IsSuccessStatusCode)
throw new EpubDocumentException("Url may be not Correct");
throw new EbookException(ExceptionType.HttpResponseError, $"URLが正しいかどうかやインターネットに正常に接続されているかどうかを確認してください。\nステータスコード: {result.StatusCode}\nメッセージ:{test}");

var content = await result.Content.ReadFromJsonAsync<BookInfo[]>(ct).ConfigureAwait(false);
if (content != null)
{
if (content[1].noveltype == null)
throw new EpubDocumentException("faild to get data by Narou API");
throw new EbookException(ExceptionType.NarouApiFailed);

if (content[1].noveltype == 2)
{
Expand All @@ -73,7 +74,7 @@ public async ValueTask<EpubDocument> ScrapingAsync(string url, string coverFileP
}

if (allNum == 0)
throw new EpubDocumentException("faild to get data by Narou API");
throw new EbookException(ExceptionType.NarouApiFailed);
}

var document = new EpubDocument(bookTitle, bookAuther, coverFilePath, id);
Expand All @@ -91,7 +92,7 @@ public async ValueTask<EpubDocument> ScrapingAsync(string url, string coverFileP
foreach (var sectionWithChapterTitle in SectionWithChapterTitleList)
{
if (sectionWithChapterTitle == null)
throw new EpubDocumentException("failed to get page");
throw new EbookException(ExceptionType.WebScrapingFailed, "ページの取得に失敗しました");

if (sectionWithChapterTitle.title != null)
{
Expand Down Expand Up @@ -162,20 +163,20 @@ private static async Task<SectionWithChapterTitle> ReadPageAsync(string url, boo
}

if (sectionTitleElement == null)
throw new EpubDocumentException("Can not find title of page");
throw new EbookException(ExceptionType.WebScrapingFailed, "ページのタイトルが見つかりません");

var sectionTitle = sectionTitleElement.InnerHtml;

var section = new Section(sectionTitleElement.InnerHtml);


var main_text = doc.QuerySelector("#novel_honbun")
?? throw new EpubDocumentException("There is no honbun.");
?? throw new EbookException(ExceptionType.WebScrapingFailed, "本文がありません");

foreach (var item in main_text.Children)
{
if (item is not IHtmlParagraphElement)
throw new EpubDocumentException("Unexpected structure");
throw new EbookException(ExceptionType.UnexpectedStructure);

if (item.ChildElementCount == 0)
{
Expand All @@ -192,12 +193,12 @@ private static async Task<SectionWithChapterTitle> ReadPageAsync(string url, boo
if (item.Children[0] is IHtmlAnchorElement aElement)
{
if (aElement.ChildElementCount != 1)
throw new EpubDocumentException("Unexpected structure");
throw new EbookException(ExceptionType.UnexpectedStructure);

if (aElement.Children[0] is IHtmlImageElement img)
{
if (img.Source == null)
throw new EpubDocumentException("Unexpected structure");
throw new EbookException(ExceptionType.UnexpectedStructure);

// 画像のダウンロード
var loader = context.GetService<IDocumentLoader>();
Expand Down Expand Up @@ -225,7 +226,7 @@ private static async Task<SectionWithChapterTitle> ReadPageAsync(string url, boo
}
}
else if (item.Children[0] is not IHtmlBreakRowElement)
throw new EpubDocumentException("Unexpected structure");
throw new EbookException(ExceptionType.UnexpectedStructure);
}
else
{
Expand All @@ -240,7 +241,7 @@ private static async Task<SectionWithChapterTitle> ReadPageAsync(string url, boo
}

if (!isAllRuby)
throw new EpubDocumentException("Unexpected structure");
throw new EbookException(ExceptionType.UnexpectedStructure);

if (!string.IsNullOrWhiteSpace(item.InnerHtml))
{
Expand Down
11 changes: 10 additions & 1 deletion KoeBook.Core/EbookException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,14 @@ public enum ExceptionType
Gpt4TalkerAndStyleSettingFailed,

[EnumMember(Value = "webページの解析に失敗しました")]
WebScrapingFailed
WebScrapingFailed,

[EnumMember(Value = "小説情報の取得に失敗しました")]
NarouApiFailed,

[EnumMember(Value = "Webページが予期しない構造です")]
UnexpectedStructure,

[EnumMember(Value = "HTTPリクエストエラー")]
HttpResponseError
}
1 change: 1 addition & 0 deletions KoeBook/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public App()
.AddSingleton<IScrapingService, ScrapingAozoraService>()
.AddSingleton<IScrapingService, ScrapingNaroService>();
services.AddSingleton<IEpubCreateService, EpubCreateService>();
services.AddSingleton<IFileExtensionService, FileExtensionService>();
// Views and ViewModels
services.AddTransient<SettingsViewModel>();
Expand Down

0 comments on commit 059eaf5

Please sign in to comment.