Skip to content

Commit

Permalink
fix(route): 日本语能力测试JLPT通知 (#18210)
Browse files Browse the repository at this point in the history
  • Loading branch information
nczitzk authored Jan 25, 2025
1 parent 478bf25 commit 0793b7a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 54 deletions.
144 changes: 91 additions & 53 deletions lib/routes/neea/jlpt.ts
Original file line number Diff line number Diff line change
@@ -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<Data> => {
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<Element> = $(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<DataItem> => {
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,
};
2 changes: 1 addition & 1 deletion lib/routes/neea/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};

0 comments on commit 0793b7a

Please sign in to comment.