* chore(deps-dev): bump eslint-plugin-unicorn from 65.0.1 to 66.0.0 Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 65.0.1 to 66.0.0. - [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases) - [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v65.0.1...v66.0.0) --- updated-dependencies: - dependency-name: eslint-plugin-unicorn dependency-version: 66.0.0 dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * style: fix unicorn-js/prefer-url-href * style: oxlint disable * style: fix unicorn-js/no-useless-else * style: no-unsafe-string-replacement * style: fix no-duplicate-loops * style: fix prefer-number-is-safe-integer * style: fix prefer-single-array-predicate * style: fix no-object-methods-with-collections * style: fix no-useless-boolean-cast * style: fix no-useless-template-literals * style: fix no-negated-array-predicate * style: fix prefer-direct-iteration * style: fix consistent-optional-chaining * style: fix prefer-simple-sort-comparator * style: fix prefer-global-number-constants * style: fix prefer-type-literal-last * style: fix require-array-sort-compare. * style: fix prefer-early-return * style: fix prefer-short-arrow-method * style: fix prefer-array-from-map * style: fix prefer-scoped-selector * style: fix no-global-object-property-assignment * style: fix no-unnecessary-global-this * style: fix no-unreadable-object-destructuring * style: fix no-invalid-argument-count * style: fix prefer-smaller-scope * style: fix prefer-iterator-to-array. * style: fix prefer-object-iterable-methods * style: fix typo * style: fix max-nested-calls * style: fix no-incorrect-template-string-interpolation * style: fix no-break-in-nested-loop * style: fix prefer-await * style: fix no-computed-property-existence-check * style: fix prefer-number-coercion * style: fix no-empty-file * style: update eslint unicorn v66 recommended preset * style: add unicorn/prefer-spread to oxlint-disable comments * style: fix no-array-reduce * style: delete github/no-then as it's covered by unicorn/prefer-await --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Tony <TonyRL@users.noreply.github.com>
131 lines
3.8 KiB
TypeScript
131 lines
3.8 KiB
TypeScript
import { load } from 'cheerio';
|
||
|
||
import type { Route } from '@/types';
|
||
import got from '@/utils/got';
|
||
import md5 from '@/utils/md5';
|
||
import { parseDate } from '@/utils/parse-date';
|
||
|
||
export const route: Route = {
|
||
path: '/:level?',
|
||
categories: ['reading'],
|
||
example: '/yomujp/n1',
|
||
parameters: { level: '等级,n1~n6,为空默认全部' },
|
||
features: {
|
||
requireConfig: false,
|
||
requirePuppeteer: false,
|
||
antiCrawler: false,
|
||
supportBT: false,
|
||
supportPodcast: false,
|
||
supportScihub: false,
|
||
},
|
||
radar: [
|
||
{
|
||
source: ['yomujp.com/', 'yomujp.com/:level'],
|
||
target: '/:level',
|
||
},
|
||
],
|
||
name: '等级',
|
||
maintainers: ['eternasuno'],
|
||
handler,
|
||
url: 'yomujp.com/',
|
||
};
|
||
|
||
async function handler(ctx) {
|
||
const level = formatLevel(ctx.req.param('level'));
|
||
const url = new URL('https://yomujp.com/wp-json/wp/v2/posts');
|
||
url.searchParams.append('categories', getLevel(level));
|
||
url.searchParams.append('per_page', Number.parseInt(ctx.req.query('limit')) || 10);
|
||
const posts = await get(url);
|
||
|
||
const item = posts.map((post) => {
|
||
const $ = load(post.content.rendered.replaceAll(/[\t\n\r]/g, ''));
|
||
const audio = $('audio.vapfem_audio>source');
|
||
const description = $('section')
|
||
.slice(2, -2)
|
||
.find('.elementor-widget-text-editor>div,.elementor-widget-image>div>img')
|
||
.toArray()
|
||
.map((el) => {
|
||
if (el.tagName === 'img') {
|
||
return `<img src=${el.attribs.src} />`;
|
||
}
|
||
if (el.firstChild.tagName === 'p') {
|
||
return `<p>${$(el.firstChild).html()}</p>`;
|
||
}
|
||
return `<p>${$(el).html()}</p>`;
|
||
})
|
||
.join('');
|
||
|
||
return {
|
||
title: post.title.rendered,
|
||
author: $('section:last-of-type p:first-of-type')
|
||
.text()
|
||
.replace(/^.+:/, ''),
|
||
description,
|
||
pubDate: parseDate(post.date_gmt),
|
||
updated: parseDate(post.modified_gmt),
|
||
guid: md5(post.guid.rendered),
|
||
link: post.link,
|
||
itunes_item_image: $('section:nth-of-type(2) img').attr('src'),
|
||
enclosure_url: audio.attr('src'),
|
||
enclosure_type: audio.attr('type'),
|
||
};
|
||
});
|
||
|
||
return {
|
||
title: level ? `${level.toUpperCase()} | 日本語多読道場` : '日本語多読道場',
|
||
link: `https://yomujp.com/${level}`,
|
||
description: 'みなさん、こんにちは。 「 日本語多読道場(にほんごたどくどうじょう) Yomujp」は日本語を勉強する人のための読みものサイト(website)です。 日本の地理、食べもの、動物、植物、文化や歴史などを紹介します。',
|
||
language: 'ja-jp',
|
||
itunes_author: 'Yomujp',
|
||
image: 'https://yomujp.com/wp-content/uploads/2023/08/top1-2-300x99-1.png',
|
||
item,
|
||
};
|
||
}
|
||
|
||
const formatLevel = (level) => {
|
||
const lowerCaseLevel = level?.toLowerCase();
|
||
|
||
switch (lowerCaseLevel) {
|
||
case 'n6':
|
||
return 'n5l';
|
||
|
||
case 'n5l':
|
||
case 'n5':
|
||
case 'n4':
|
||
case 'n3':
|
||
case 'n2':
|
||
case 'n1':
|
||
return lowerCaseLevel;
|
||
|
||
default:
|
||
return '';
|
||
}
|
||
};
|
||
|
||
const getLevel = (level) => {
|
||
switch (level) {
|
||
case 'n6':
|
||
case 'n5l':
|
||
return '27';
|
||
case 'n5':
|
||
return '26';
|
||
case 'n4':
|
||
return '21';
|
||
case 'n3':
|
||
return '20';
|
||
case 'n2':
|
||
return '19';
|
||
case 'n1':
|
||
return '17';
|
||
|
||
default:
|
||
return '17,19,20,21,26,27';
|
||
}
|
||
};
|
||
|
||
const get = async (url) => {
|
||
const response = await got({ method: 'get', url });
|
||
|
||
return response.data;
|
||
};
|