feat(route): nppa (#22335)
This commit is contained in:
parent
54b15849d7
commit
c70d087a9a
|
|
@ -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'));
|
||||
|
||||
|
|
|
|||
|
|
@ -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</title>')) {
|
||||
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()
|
||||
),
|
||||
};
|
||||
};
|
||||
|
|
@ -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</title>')) {
|
||||
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()
|
||||
),
|
||||
};
|
||||
};
|
||||
|
|
@ -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<Data> {
|
||||
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;
|
||||
})
|
||||
)
|
||||
),
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
import type { Namespace } from '@/types';
|
||||
|
||||
export const namespace: Namespace = {
|
||||
name: '国家新闻出版署',
|
||||
url: 'www.nppa.gov.cn',
|
||||
categories: ['government'],
|
||||
lang: 'zh-CN',
|
||||
};
|
||||
Loading…
Reference in New Issue