fix(route): 日本语能力测试JLPT通知 (#18210)
This commit is contained in:
parent
478bf253b2
commit
0793b7a1b7
|
|
@ -1,75 +1,113 @@
|
|||
import { Route } from '@/types';
|
||||
import { type Data, type DataItem, type Route, ViewType } from '@/types';
|
||||
|
||||
import cache from '@/utils/cache';
|
||||
import got from '@/utils/got';
|
||||
import { load } from 'cheerio';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import timezone from '@/utils/timezone';
|
||||
|
||||
import { type CheerioAPI, type Cheerio, type Element, load } from 'cheerio';
|
||||
import { type Context } from 'hono';
|
||||
|
||||
export const handler = async (ctx: Context): Promise<Data> => {
|
||||
const limit: number = Number.parseInt(ctx.req.query('limit') ?? '30', 10);
|
||||
|
||||
const baseUrl: string = 'https://jlpt.neea.cn';
|
||||
const targetUrl: string = new URL('index.do', baseUrl).href;
|
||||
|
||||
const response = await ofetch(targetUrl);
|
||||
const $: CheerioAPI = load(response);
|
||||
const language = $('html').attr('lang') ?? 'zh-CN';
|
||||
|
||||
let items: DataItem[] = [];
|
||||
|
||||
items = $('div.indexcontent a')
|
||||
.slice(0, limit)
|
||||
.toArray()
|
||||
.map((el): Element => {
|
||||
const $el: Cheerio<Element> = $(el);
|
||||
|
||||
const title: string = $el.text();
|
||||
const pubDateStr: string | undefined = title.match(/(\d{4}-\d{2}-\d{2})/)?.[1];
|
||||
const linkUrl: string | undefined = $el.attr('href');
|
||||
const upDatedStr: string | undefined = pubDateStr;
|
||||
|
||||
const processedItem: DataItem = {
|
||||
title,
|
||||
pubDate: pubDateStr ? parseDate(pubDateStr) : undefined,
|
||||
link: linkUrl ? new URL(linkUrl, baseUrl).href : undefined,
|
||||
updated: upDatedStr ? parseDate(upDatedStr) : undefined,
|
||||
language,
|
||||
};
|
||||
|
||||
return processedItem;
|
||||
});
|
||||
|
||||
items = (
|
||||
await Promise.all(
|
||||
items.map((item) => {
|
||||
if (!item.link) {
|
||||
return item;
|
||||
}
|
||||
|
||||
return cache.tryGet(item.link, async (): Promise<DataItem> => {
|
||||
const detailResponse = await ofetch(item.link);
|
||||
const $$: CheerioAPI = load(detailResponse);
|
||||
|
||||
const title: string = $$('div.dvTitle').text();
|
||||
const description: string = $$('div.dvContent').html() ?? '';
|
||||
|
||||
const processedItem: DataItem = {
|
||||
title,
|
||||
description,
|
||||
};
|
||||
|
||||
return {
|
||||
...item,
|
||||
...processedItem,
|
||||
};
|
||||
});
|
||||
})
|
||||
)
|
||||
).filter((_): _ is DataItem => true);
|
||||
|
||||
const title: string = $('title').text();
|
||||
|
||||
return {
|
||||
title: `${title.split(/-/).pop()} - ${$('div.indexcontent h1').text()}`,
|
||||
description: title,
|
||||
link: targetUrl,
|
||||
item: items,
|
||||
allowEmpty: true,
|
||||
image: $('div.header img').attr('arc') ? new URL($('div.header img').attr('arc') as string, baseUrl).href : undefined,
|
||||
author: title.split(/-/)[0],
|
||||
language,
|
||||
id: targetUrl,
|
||||
};
|
||||
};
|
||||
|
||||
export const route: Route = {
|
||||
path: '/global/jlpt',
|
||||
name: '日本语能力测试(JLPT)通知',
|
||||
url: 'jlpt.neea.edu.cn',
|
||||
path: '/jlpt',
|
||||
name: '日本语能力测试JLPT通知',
|
||||
url: 'jlpt.neea.cn',
|
||||
maintainers: ['nczitzk'],
|
||||
example: '/neea/global/jlpt',
|
||||
parameters: {},
|
||||
handler,
|
||||
example: '/neea/jlpt',
|
||||
parameters: undefined,
|
||||
description: undefined,
|
||||
categories: ['study'],
|
||||
features: {
|
||||
requireConfig: false,
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
supportRadar: true,
|
||||
supportBT: false,
|
||||
supportPodcast: false,
|
||||
supportScihub: false,
|
||||
},
|
||||
radar: [
|
||||
{
|
||||
source: ['jlpt.neea.edu.cn', 'jlpt.neea.cn'],
|
||||
target: '/global/jlpt',
|
||||
source: ['jlpt.neea.cn'],
|
||||
target: '/jlpt',
|
||||
},
|
||||
],
|
||||
handler,
|
||||
view: ViewType.Articles,
|
||||
};
|
||||
|
||||
async function handler() {
|
||||
const rootUrl = 'https://news.neea.cn';
|
||||
const currentUrl = `${rootUrl}/JLPT/1/newslist.htm`;
|
||||
|
||||
const response = await got({
|
||||
method: 'get',
|
||||
url: currentUrl,
|
||||
});
|
||||
|
||||
const $ = load(response.data);
|
||||
|
||||
let items = $('a')
|
||||
.toArray()
|
||||
.map((item) => {
|
||||
item = $(item);
|
||||
|
||||
const matches = item.text().match(/(\d{4}-\d{2}-\d{2})/);
|
||||
|
||||
return {
|
||||
title: item.text(),
|
||||
link: `${rootUrl}/JLPT/1/${item.attr('href')}`,
|
||||
pubDate: matches ? timezone(parseDate(matches[1]), +8) : '',
|
||||
};
|
||||
});
|
||||
|
||||
items = await Promise.all(
|
||||
items.map((item) =>
|
||||
cache.tryGet(item.link, async () => {
|
||||
const detailResponse = await got({
|
||||
method: 'get',
|
||||
url: item.link,
|
||||
});
|
||||
|
||||
const content = load(detailResponse.data);
|
||||
|
||||
item.description = content('.dvContent').html();
|
||||
|
||||
return item;
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
return {
|
||||
title: '日本语能力测试(JLPT)通知',
|
||||
link: currentUrl,
|
||||
item: items,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,6 @@ import type { Namespace } from '@/types';
|
|||
|
||||
export const namespace: Namespace = {
|
||||
name: '中国教育考试网',
|
||||
url: 'www.neea.edu.cn',
|
||||
url: 'www.neea.cn',
|
||||
lang: 'zh-CN',
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue