Skip to content

Commit

Permalink
feat: cache search & art template
Browse files Browse the repository at this point in the history
  • Loading branch information
SnowAgar25 committed Nov 9, 2024
1 parent 9085c64 commit a262832
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 76 deletions.
52 changes: 28 additions & 24 deletions lib/routes/syosetu/search.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -256,23 +255,28 @@ async function handler(ctx: Context): Promise<Data> {
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;
}
76 changes: 76 additions & 0 deletions lib/routes/syosetu/templates/description.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{{ if novel.story }}
<h2>あらすじ</h2>
<p>{{ novel.story.replaceAll('\n', '<br>') }}</p>
{{ /if }}

<h2>作品情報</h2>
<table>
<tr>
<td>状態</td>
<td>
{{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}}
</td>
</tr>
<tr>
<td>ジャンル</td>
<td>{{genreText}}</td>
</tr>
<tr>
<td>キーワード</td>
<td>{{ novel.keyword }}</td>
</tr>
<tr>
<td>最終掲載</td>
<td>{{ novel.general_lastup }}</td>
</tr>
<tr>
<td>Nコード</td>
<td>{{ novel.ncode }}</td>
</tr>
<tr>
<td>読了時間</td>
<td>約{{ novel.time }}分({{ novel.length.toLocaleString() }}文字)</td>
</tr>
</table>
<br>
<table>
{{ if novel.weekly_unique }}
<tr>
<td>週別ユニークユーザ</td>
<td>{{ novel.weekly_unique < 100 ? '100未満' : novel.weekly_unique.toLocaleString() }}</td>
</tr>
{{ /if }}
<tr>
<td>総合ポイント</td>
<td>{{ novel.global_point.toLocaleString() }} pt</td>
</tr>
<tr>
<td>評価人数</td>
<td>{{ novel.all_hyoka_cnt.toLocaleString() }} 人</td>
</tr>
<tr>
<td>評価ポイント</td>
<td>{{ novel.all_point.toLocaleString() }} pt</td>
</tr>
<tr>
<td>ブックマーク</td>
<td>{{ novel.fav_novel_cnt.toLocaleString() }} 件</td>
</tr>
</table>

{{ if novel.sasie_cnt > 0 }}
<h2>イラスト</h2>
<p>挿絵数:{{ novel.sasie_cnt }}枚</p>
{{ /if }}
52 changes: 0 additions & 52 deletions lib/routes/syosetu/templates/result.art

This file was deleted.

0 comments on commit a262832

Please sign in to comment.