-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
92 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters