Skip to content

Commit

Permalink
refactor: improve syosetu route params and search handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SnowAgar25 committed Nov 13, 2024
1 parent 2618afa commit 6ab963f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 51 deletions.
11 changes: 8 additions & 3 deletions lib/routes/syosetu/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import { Route, Data, DataItem } from '@/types';
import { fetchNovelInfo, fetchChapterContent } from './utils';
import { Context } from 'hono';
import { NovelType } from 'narou';
import querystring from 'querystring';

export const route: Route = {
path: '/:ncode',
path: '/:ncode/:routeParams?',
categories: ['reading'],
example: '/syosetu/n9292ii',
parameters: { ncode: 'Novel code, can be found in URL' },
parameters: {
ncode: 'Novel code, can be found in URL',
routeParams: 'Optional: limit=N (max: 20, default: 5)',
}, // TOWRITE: Limit to 20, deault is 5
features: {
requireConfig: false,
requirePuppeteer: false,
Expand All @@ -30,7 +34,8 @@ export const route: Route = {

async function handler(ctx: Context): Promise<Data> {
const { ncode } = ctx.req.param();
const limit = Number.parseInt(ctx.req.query('limit') ?? '5');
const routeParams = querystring.parse(ctx.req.param('routeParams'));
const limit = Math.min(Number(routeParams.limit || 5), 20);

const { baseUrl, novel } = await fetchNovelInfo(ncode);
novel.story = novel.story.replaceAll('\n', '<br>') || '';
Expand Down
77 changes: 37 additions & 40 deletions lib/routes/syosetu/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ export const route: Route = {
handler,
};

const setIfExists = (value) => value ?? undefined;

/**
* This function converts query string generated by Syosetu website into API-compatible format.
* It is not intended for users to freely adjust values.
Expand All @@ -172,44 +174,39 @@ function mapToSearchParams(query: string): SearchParams {
lim: 40,
};

// 検索ワード関連のパラメータ / Search word related parameters
params.word && (searchParams.word = params.word);
params.notword && (searchParams.notword = params.notword);

// 検索範囲フラグ / Search scope flags
params.title && (searchParams.title = 1);
params.ex && (searchParams.ex = 1);
params.keyword && (searchParams.keyword = 1);
params.wname && (searchParams.wname = 1);

// Include
params.iszankoku && (searchParams.iszankoku = 1);
params.isbl && (searchParams.isbl = 1);
params.isgl && (searchParams.isgl = 1);
params.istensei && (searchParams.istensei = 1);
params.istenni && (searchParams.istenni = 1);

// Exclude
params.stop && (searchParams.stop = 1);
params.notzankoku && (searchParams.notzankoku = 1);
params.notbl && (searchParams.notbl = 1);
params.notgl && (searchParams.notgl = 1);
params.nottensei && (searchParams.nottensei = 1);
params.nottenni && (searchParams.nottenni = 1);

// 読了時間の処理 / Reading Time range processing
params.minlen && (searchParams.minlen = params.minlen);
params.maxlen && (searchParams.maxlen = params.maxlen);

// 特殊パラメータ / Special parameters
params.type && (searchParams.type = params.type as NovelTypeParam);
params.order && (searchParams.order = params.order as Order);

params.genre && (searchParams.genre = params.genre as Join<Genre> | Genre);
params.nocgenre && (searchParams.nocgenre = params.nocgenre as Join<R18Site> | R18Site);

params.sasie === '1-' && (searchParams.sasie = '1-');
(params.mintime || params.maxtime) && (searchParams.time = `${params.mintime || ''}-${params.maxtime || ''}`);
searchParams.word = setIfExists(params.word);
searchParams.notword = setIfExists(params.notword);

searchParams.title = setIfExists(params.title);
searchParams.ex = setIfExists(params.ex);
searchParams.keyword = setIfExists(params.keyword);
searchParams.wname = setIfExists(params.wname);

searchParams.sasie = setIfExists(params.sasie);
searchParams.iszankoku = setIfExists(params.iszankoku);
searchParams.isbl = setIfExists(params.isbl);
searchParams.isgl = setIfExists(params.isgl);
searchParams.istensei = setIfExists(params.istensei);
searchParams.istenni = setIfExists(params.istenni);

searchParams.stop = setIfExists(params.stop);
searchParams.notzankoku = setIfExists(params.notzankoku);
searchParams.notbl = setIfExists(params.notbl);
searchParams.notgl = setIfExists(params.notgl);
searchParams.nottensei = setIfExists(params.nottensei);
searchParams.nottenni = setIfExists(params.nottenni);

searchParams.minlen = setIfExists(params.minlen);
searchParams.maxlen = setIfExists(params.maxlen);

searchParams.type = setIfExists(params.type as NovelTypeParam);
searchParams.order = setIfExists(params.order as Order);
searchParams.genre = setIfExists(params.genre as Join<Genre> | Genre);
searchParams.nocgenre = setIfExists(params.nocgenre as Join<R18Site> | R18Site);

if (params.mintime || params.maxtime) {
searchParams.time = `${params.mintime || ''}-${params.maxtime || ''}`;
}

return searchParams;
}
Expand Down Expand Up @@ -270,8 +267,8 @@ async function handler(ctx: Context): Promise<Data> {
// Skip pubDate - search results prioritize search sequence over timestamps
// pubDate: novel.general_lastup,
author: novel.writer,
// Split by slash, full-width slash, half-width space, full-width space
category: novel.keyword.split(/[/\uFF0F \u3000]/).filter(Boolean),
// Split by slash(/), full-width slash(/) and whitespace characters(\s)
category: novel.keyword.split(/[/\uFF0F\s]/).filter(Boolean),
}));

const searchTerms: string[] = [];
Expand Down
15 changes: 7 additions & 8 deletions lib/routes/syosetu/templates/description.art
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<tr>
<td>状態</td>
<td>
{{if novel.end === 0}}
完結済
{{if novel.novel_type === 2}}
短編
{{else}}
連載中
{{/if}}
{{if novel.novel_type === 1}}
{{if novel.end === 0}}
完結済
{{else}}
連載中
{{/if}}
(全{{ novel.general_all_no }}エピソード)
{{else if novel.novel_type === 2}}
(短編)
{{/if}}
</td>
</tr>
Expand Down Expand Up @@ -69,6 +69,5 @@
</table>

{{ if novel.sasie_cnt > 0 }}
<h2>イラスト</h2>
<p>挿絵数:{{ novel.sasie_cnt }}枚</p>
{{ /if }}

0 comments on commit 6ab963f

Please sign in to comment.