diff --git a/lib/router.js b/lib/router.js index 921eaa57a..4293ac9e1 100644 --- a/lib/router.js +++ b/lib/router.js @@ -413,10 +413,6 @@ router.get('/gov/mohurd/policy', lazyloadRouteHandler('./routes/gov/mohurd/polic // 国家新闻出版广电总局 router.get('/gov/sapprft/approval/:channel/:detail?', lazyloadRouteHandler('./routes/gov/sapprft/7026')); -// 国家新闻出版署 -router.get('/gov/nppa/:channel', lazyloadRouteHandler('./routes/gov/nppa/channels')); -router.get('/gov/nppa/:channel/:content', lazyloadRouteHandler('./routes/gov/nppa/contents')); - // 北京卫生健康委员会 router.get('/gov/beijing/mhc/:caty', lazyloadRouteHandler('./routes/gov/beijing/mhc')); diff --git a/lib/routes-deprecated/gov/nppa/channels.js b/lib/routes-deprecated/gov/nppa/channels.js deleted file mode 100644 index bf3e5621c..000000000 --- a/lib/routes-deprecated/gov/nppa/channels.js +++ /dev/null @@ -1,58 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); -const { parseRelativeDate } = require('@/utils/parse-date'); -const timezone = require('@/utils/timezone'); - -module.exports = async (ctx) => { - const { channel } = ctx.params; - const host = `http://www.nppa.gov.cn`; - const link = host + `/nppa/channels/${channel}.shtml`; - const fullpage = await got.get(link + '?' + Date.now()); // 避免CDN缓存 - if (~fullpage.data.indexOf('-404')) { - ctx.throw(404); - } - const $ = cheerio.load(fullpage.data); - const target = $('ul.m2c2ul li, ul.m2nrul li'); - ctx.state.data = { - title: '国家新闻出版署 - ' + $('.m2nRt, .m2Top_em').text().trim(), - link, - item: await Promise.all( - target - .map(async (index, item) => { - item = $(item); - const href = item.find('a').attr('href'); - let contenlUrl, - description = ''; - if (/^https?:\/\//.test(href)) { - contenlUrl = href; - } else { - contenlUrl = host + href; - description = await ctx.cache.tryGet(contenlUrl, async () => { - const fullText = await got.get(contenlUrl); - const $$ = cheerio.load(fullText.data); - if (~$$('.m2pos').text().indexOf('游戏审批结果')) { - let fullTextData = ''; - $$('.m3pageCon table.trStyle tbody tr') - .slice(1) - .each((index, item) => { - item = $$(item).find('td'); - fullTextData += $$(item[1]).text().trim() + ' | '; - }); - return fullTextData.slice(0, -3); - } else { - $$('.m3pageCon style, .m3pageCon script').remove(); - return $$('.m3pageCon').text().trim().slice(0, 1024); - } - }); - } - return { - title: item.find('a').text().trim(), - description, - pubDate: timezone(parseRelativeDate(item.find('span').text()), 8), - link: contenlUrl, - }; - }) - .get() - ), - }; -}; diff --git a/lib/routes-deprecated/gov/nppa/contents.js b/lib/routes-deprecated/gov/nppa/contents.js deleted file mode 100644 index 4ce146424..000000000 --- a/lib/routes-deprecated/gov/nppa/contents.js +++ /dev/null @@ -1,38 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); -const { parseRelativeDate } = require('@/utils/parse-date'); -const timezone = require('@/utils/timezone'); - -module.exports = async (ctx) => { - const { channel, content } = ctx.params; - const host = `http://www.nppa.gov.cn`; - const link = host + `/nppa/contents/${channel}/${content}.shtml`; - const fullpage = await got.get(link + '?' + Date.now()); // 避免CDN缓存 - if (~fullpage.data.indexOf('-404')) { - ctx.throw(404); - } - const $ = cheerio.load(fullpage.data); - const list = $('.m3pageCon table.trStyle tbody tr'); - - ctx.state.data = { - title: '国家新闻出版署 - ' + $('.m3page_t').text().trim(), - link, - item: await Promise.all( - list - .slice(1) - .map((index, item) => { - item = $(item).find('td'); - return { - title: $(item[1]).text().trim(), - category: $(item[2]).text().trim(), - description: $(item[5]).text().trim(), - author: $(item[3]).text().trim() + ' | ' + $(item[4]).text().trim(), - pubDate: timezone(parseRelativeDate($(item[7]).text().trim()), 8), - guid: $(item[6]).text().trim(), - link, - }; - }) - .get() - ), - }; -}; diff --git a/lib/routes/gov/nppa/channels.ts b/lib/routes/gov/nppa/channels.ts new file mode 100644 index 000000000..913cf4cf3 --- /dev/null +++ b/lib/routes/gov/nppa/channels.ts @@ -0,0 +1,55 @@ +import { load } from 'cheerio'; +import type { Context } from 'hono'; + +import { type Data, type DataItem, type Route } from '@/types'; +import cache from '@/utils/cache'; +import ofetch from '@/utils/ofetch'; +import { parseDate } from '@/utils/parse-date'; +import timezone from '@/utils/timezone'; + +export const route: Route = { + path: '/:path{.+}', + name: '通用', + example: '/gov/nppa/xxfb/ywxx', + parameters: { path: '路径,留空默认 `xxfb/ywxx`' }, + maintainers: ['y2361547758'], + handler, +}; + +const host = 'https://www.nppa.gov.cn'; + +async function handler(ctx: Context): Promise { + const { path = 'xxfb/ywxx' } = ctx.req.param(); + const link = `${host}/${path}/`; + const response = await ofetch(link); + const $ = load(response); + + const list = $('.m2nRcon li') + .toArray() + .map((item) => { + const $item = $(item); + const a = $item.find('a'); + return { + link: new URL(a.attr('href')!, link).href, + pubDate: timezone(parseDate($item.find('span').text().replaceAll(/[[\]]/g, '')), 8), + }; + }) as DataItem[]; + + return { + title: `国家新闻出版署 - ${$('.m2nRt').text().trim()}`, + link, + item: await Promise.all( + list.map((item) => + cache.tryGet(item.link!, async () => { + const response = await ofetch(item.link!); + const $ = load(response); + + item.title = $('.m3page_t').text().trim() || $('head title').text(); + item.description = $('.m3pageEdit').html() ?? ''; + + return item; + }) + ) + ), + }; +} diff --git a/lib/routes/gov/nppa/namespace.ts b/lib/routes/gov/nppa/namespace.ts new file mode 100644 index 000000000..d52e77cd3 --- /dev/null +++ b/lib/routes/gov/nppa/namespace.ts @@ -0,0 +1,8 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '国家新闻出版署', + url: 'www.nppa.gov.cn', + categories: ['government'], + lang: 'zh-CN', +};