Skip to content

Commit

Permalink
#17-1 エラーメッセージの日本語化
Browse files Browse the repository at this point in the history
  • Loading branch information
aiueo-1234 committed Mar 15, 2024
1 parent 29394aa commit 7ff9bd3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
8 changes: 4 additions & 4 deletions Epub/KoeBook.Epub/Services/ScrapingAozoraService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public async ValueTask<EpubDocument> ScrapingAsync(string url, string coverFileP

// title の取得
var bookTitle = doc.QuerySelector(".title")
?? throw new EbookException(ExceptionType.WebScrapingFailed, $"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 EbookException(ExceptionType.WebScrapingFailed, $"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 @@ -112,10 +112,10 @@ public async ValueTask<EpubDocument> ScrapingAsync(string url, string coverFileP
if (midashi != null)
{
if (midashi.Id == null)
throw new EbookException(ExceptionType.WebScrapingFailed, "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 EbookException(ExceptionType.WebScrapingFailed, $"Unexpected id of Anchor tag was found: id = {midashi.Id}");
throw new EbookException(ExceptionType.WebScrapingFailed, $"予期しないアンカータグが見つかりました。id = {midashi.Id}");

if (contentsIds.Contains(midashiId))
{
Expand Down
26 changes: 13 additions & 13 deletions Epub/KoeBook.Epub/Services/ScrapingNaroService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public async ValueTask<EpubDocument> ScrapingAsync(string url, string coverFileP

// title の取得
var bookTitleElement = doc.QuerySelector(".novel_title")
?? throw new EbookException(ExceptionType.WebScrapingFailed, $"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 EbookException(ExceptionType.WebScrapingFailed, $"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 @@ -55,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 EbookException(ExceptionType.WebScrapingFailed, "Url may be not Correct");
throw new EbookException(ExceptionType.WebScrapingFailed, "URLが正しくありません");

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

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

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

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

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

if (sectionTitleElement == null)
throw new EbookException(ExceptionType.WebScrapingFailed, "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 EbookException(ExceptionType.WebScrapingFailed, "There is no honbun.");
?? throw new EbookException(ExceptionType.WebScrapingFailed, "本文がありません");

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

if (item.ChildElementCount == 0)
{
Expand All @@ -193,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 EbookException(ExceptionType.WebScrapingFailed, "Unexpected structure");
throw new EbookException(ExceptionType.UnexpectedStructure);

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

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

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

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

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

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

[EnumMember(Value = "予期しない構造です")]
UnexpectedStructure
}

0 comments on commit 7ff9bd3

Please sign in to comment.