From a2628324df46fcbe20a4433e5cfda25e1ae443aa Mon Sep 17 00:00:00 2001 From: Tsuyumi Date: Sat, 9 Nov 2024 14:26:30 +0800 Subject: [PATCH] feat: cache search & art template --- lib/routes/syosetu/search.ts | 52 +++++++------- lib/routes/syosetu/templates/description.art | 76 ++++++++++++++++++++ lib/routes/syosetu/templates/result.art | 52 -------------- 3 files changed, 104 insertions(+), 76 deletions(-) create mode 100644 lib/routes/syosetu/templates/description.art delete mode 100644 lib/routes/syosetu/templates/result.art diff --git a/lib/routes/syosetu/search.ts b/lib/routes/syosetu/search.ts index e47d8f30cd9001..ef7560d8f67e86 100644 --- a/lib/routes/syosetu/search.ts +++ b/lib/routes/syosetu/search.ts @@ -1,10 +1,9 @@ import { Route, Data } from '@/types'; -// import cache from '@/utils/cache'; -// import { getCurrentPath } from '@/utils/helpers'; -// import { art } from '@/utils/render'; -// import path from 'node:path'; +import cache from '@/utils/cache'; +import { art } from '@/utils/render'; +import path from 'node:path'; import { Context } from 'hono'; -import { Genre, NarouNovelFetch, NovelTypeParam, Order, R18Site, SearchBuilder, SearchBuilderR18, SearchParams } from 'narou'; +import { Genre, GenreNotation, NarouNovelFetch, NovelTypeParam, Order, R18Site, SearchBuilder, SearchBuilderR18, SearchParams } from 'narou'; import queryString from 'query-string'; import { Join } from 'narou/util/type'; import InvalidParameterError from '@/errors/types/invalid-parameter'; @@ -256,23 +255,28 @@ async function handler(ctx: Context): Promise { const { domain, query } = ctx.req.param(); const searchUrl = `https://${domain}.syosetu.com/search/search/search.php?${query}`; - const searchParams = mapToSearchParams(query); - const builder = createNovelSearchBuilder(domain, searchParams); - const result = await builder.execute(); - - // TODO: richer feed, cache - const items = result.values.map((novel) => ({ - title: novel.title, - link: `https://${isGeneral(domain) ? 'ncode' : 'novel18'}.syosetu.com/${String(novel.ncode).toLowerCase()}`, - description: novel.story, - // Skip pubDate - search results prioritize search sequence over timestamps - // pubDate: novel.general_lastup, - author: novel.writer, - })); - - return { - title: `Syosetu Search`, - link: searchUrl, - item: items, - }; + return (await cache.tryGet(searchUrl, async () => { + const searchParams = mapToSearchParams(query); + const builder = createNovelSearchBuilder(domain, searchParams); + const result = await builder.execute(); + + const items = result.values.map((novel) => ({ + title: novel.title, + link: `https://${isGeneral(domain) ? 'ncode' : 'novel18'}.syosetu.com/${String(novel.ncode).toLowerCase()}`, + description: art(path.join(__dirname, 'templates', 'description.art'), { + novel, + genreText: GenreNotation[novel.genre], + }), + // Skip pubDate - search results prioritize search sequence over timestamps + // pubDate: novel.general_lastup, + author: novel.writer, + category: novel.keyword.split(' '), + })); + + return { + title: `Syosetu Search`, + link: searchUrl, + item: items, + }; + })) as Data; } diff --git a/lib/routes/syosetu/templates/description.art b/lib/routes/syosetu/templates/description.art new file mode 100644 index 00000000000000..92d9c6814adb60 --- /dev/null +++ b/lib/routes/syosetu/templates/description.art @@ -0,0 +1,76 @@ +{{ if novel.story }} +

あらすじ

+

{{ novel.story.replaceAll('\n', '
') }}

+{{ /if }} + +

作品情報

+ + + + + + + + + + + + + + + + + + + + + + + + + +
状態 + {{if novel.end === 0}} + 完結済 + {{if novel.novel_type === 1}} + (全{{novel.general_all_no}}エピソード) + {{else}} + (短編) + {{/if}} + {{else}} + 連載中 + {{if novel.novel_type === 1}} + (全{{novel.general_all_no}}エピソード) + {{/if}} + {{/if}} +
ジャンル{{genreText}}
キーワード{{ novel.keyword }}
最終掲載{{ novel.general_lastup }}
Nコード{{ novel.ncode }}
読了時間約{{ novel.time }}分({{ novel.length.toLocaleString() }}文字)
+
+ + {{ if novel.weekly_unique }} + + + + + {{ /if }} + + + + + + + + + + + + + + + + +
週別ユニークユーザ{{ novel.weekly_unique < 100 ? '100未満' : novel.weekly_unique.toLocaleString() }}
総合ポイント{{ novel.global_point.toLocaleString() }} pt
評価人数{{ novel.all_hyoka_cnt.toLocaleString() }} 人
評価ポイント{{ novel.all_point.toLocaleString() }} pt
ブックマーク{{ novel.fav_novel_cnt.toLocaleString() }} 件
+ +{{ if novel.sasie_cnt > 0 }} +

イラスト

+

挿絵数:{{ novel.sasie_cnt }}枚

+{{ /if }} diff --git a/lib/routes/syosetu/templates/result.art b/lib/routes/syosetu/templates/result.art deleted file mode 100644 index 2af3185e157625..00000000000000 --- a/lib/routes/syosetu/templates/result.art +++ /dev/null @@ -1,52 +0,0 @@ -

{{ title }}

-
-

Status: {{ status }}

-

Description

-

{{ description }}

-

Keywords

-

{{ keywords }}

-

Statistics

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Last Update:{{ stats.lastUpdate }}
Novel Code:{{ stats.ncode }}
Reading Time:{{ stats.readTime }}
Weekly Users:{{ stats.weeklyUsers }}
Total Points:{{ stats.totalPoints }}
Evaluations:{{ stats.evaluations }}
Evaluation Points:{{ stats.evalPoints }}
Bookmarks:{{ stats.bookmarks }}
Has Illustrations:{{ stats.hasIllustrations ? "Yes" : "No" }}
- -

- - Read on Syosetu - -