diff --git a/lib/routes/neea/jlpt.ts b/lib/routes/neea/jlpt.ts index 5255ee71de549b..ea5214c52c88e6 100644 --- a/lib/routes/neea/jlpt.ts +++ b/lib/routes/neea/jlpt.ts @@ -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'; -export const route: Route = { - path: '/global/jlpt', - name: '日本语能力测试(JLPT)通知', - url: 'jlpt.neea.edu.cn', - maintainers: ['nczitzk'], - example: '/neea/global/jlpt', - parameters: {}, - categories: ['study'], - features: { - supportRadar: true, - }, - radar: [ - { - source: ['jlpt.neea.edu.cn', 'jlpt.neea.cn'], - target: '/global/jlpt', - }, - ], - handler, -}; +import { type CheerioAPI, type Cheerio, type Element, load } from 'cheerio'; +import { type Context } from 'hono'; -async function handler() { - const rootUrl = 'https://news.neea.cn'; - const currentUrl = `${rootUrl}/JLPT/1/newslist.htm`; +export const handler = async (ctx: Context): Promise => { + const limit: number = Number.parseInt(ctx.req.query('limit') ?? '30', 10); - const response = await got({ - method: 'get', - url: currentUrl, - }); + const baseUrl: string = 'https://jlpt.neea.cn'; + const targetUrl: string = new URL('index.do', baseUrl).href; - const $ = load(response.data); + const response = await ofetch(targetUrl); + const $: CheerioAPI = load(response); + const language = $('html').attr('lang') ?? 'zh-CN'; - let items = $('a') + let items: DataItem[] = []; + + items = $('div.indexcontent a') + .slice(0, limit) .toArray() - .map((item) => { - item = $(item); + .map((el): Element => { + const $el: Cheerio = $(el); - const matches = item.text().match(/(\d{4}-\d{2}-\d{2})/); + 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; - return { - title: item.text(), - link: `${rootUrl}/JLPT/1/${item.attr('href')}`, - pubDate: matches ? timezone(parseDate(matches[1]), +8) : '', + 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) => - cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); + items = ( + await Promise.all( + items.map((item) => { + if (!item.link) { + return item; + } + + return cache.tryGet(item.link, async (): Promise => { + const detailResponse = await ofetch(item.link); + const $$: CheerioAPI = load(detailResponse); - const content = load(detailResponse.data); + const title: string = $$('div.dvTitle').text(); + const description: string = $$('div.dvContent').html() ?? ''; - item.description = content('.dvContent').html(); + const processedItem: DataItem = { + title, + description, + }; - return item; + return { + ...item, + ...processedItem, + }; + }); }) ) - ); + ).filter((_): _ is DataItem => true); + + const title: string = $('title').text(); return { - title: '日本语能力测试(JLPT)通知', - link: currentUrl, + 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: '/jlpt', + name: '日本语能力测试JLPT通知', + url: 'jlpt.neea.cn', + maintainers: ['nczitzk'], + 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.cn'], + target: '/jlpt', + }, + ], + view: ViewType.Articles, +}; diff --git a/lib/routes/neea/namespace.ts b/lib/routes/neea/namespace.ts index e208712425d73a..272cd338a8df3b 100644 --- a/lib/routes/neea/namespace.ts +++ b/lib/routes/neea/namespace.ts @@ -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', };