Files
RSSHub/lib/routes/syosetu/utils.ts
T
Tsuyumi 5cb34379c3 feat(route): fix syosetu & add more routes (#17500)
* fix(route): syosetu

* feat: add search route & narou package

* chore: cleanup

* feat: cache search & art template

* fix: __dirname

* chore: cleanup

* feat: add dev route

* chore: cleanup

* refactor: improve syosetu route params and search handling

* feat: add ranking routes

* feat: add radar items for syosetu rankings
- Add BEST5 radar items for rankings
- Standardize title format
- Improve params naming

* chore: add space

* chore: cleanup

* chore: add ranking options type hint & cleanup

* refactor: change route parameters from path params to query strings
- Change optional parameters from path params (/:params) to query strings (?limit=N)
- Remove unnecessary cache
- Extract ranking-related types and constants to separate files
- Extract isekai ranking handling to a separate file
- Enhance code structure and readability

* feat: improve usability and short novel update handling

- Add chapter display support
- Fix chapter route radar
- Optimize ranking and search limits
- Use novelupdated_at as pubDate for short novels

* fix: request more items to handle tensei/tenni duplicates

* fix: limit ranking items to maximum of 300
2024-11-23 00:09:33 +08:00

51 lines
1.8 KiB
TypeScript

import { DataItem } from '@/types';
import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import InvalidParameterError from '@/errors/types/invalid-parameter';
import { config } from '@/config';
import { NarouNovelFetch, NarouSearchResult, SearchBuilder, SearchBuilderR18 } from 'narou';
export async function fetchNovelInfo(ncode: string): Promise<{ baseUrl: string; novel: NarouSearchResult }> {
const api = new NarouNovelFetch();
const [generalRes, r18Res] = await Promise.all([new SearchBuilder({ gzip: 5, of: 't-s-k-ga-nt-nu' }, api).ncode(ncode).execute(), new SearchBuilderR18({ gzip: 5, of: 't-s-k-ga-nt-nu' }, api).ncode(ncode).execute()]);
const isGeneral = generalRes.allcount !== 0;
const novelData = isGeneral ? generalRes : r18Res;
const baseUrl = isGeneral ? 'https://ncode.syosetu.com' : 'https://novel18.syosetu.com';
if (novelData.allcount === 0) {
throw new InvalidParameterError('Novel not found in both APIs');
}
return {
baseUrl,
novel: novelData.values[0] as NarouSearchResult,
};
}
export async function fetchChapterContent(chapterUrl: string, chapter?: number): Promise<DataItem> {
return (await cache.tryGet(chapterUrl, async () => {
const response = await ofetch(chapterUrl, {
headers: {
Cookie: 'over18=yes',
'User-Agent': config.ua,
},
});
const $ = load(response);
const title = `${chapter ? `#${chapter} ` : ''}${$('.p-novel__title').html() || ''}`;
const description = $('.p-novel__body').html() || '';
const pubDate = $('meta[name=WWWC]').attr('content');
return {
title,
description,
link: chapterUrl,
pubDate,
language: 'ja',
};
})) as DataItem;
}