Files
RSSHub/lib/v2/gov/cmse/index.js
Ethan Shen e761af6c86 feat(route): add 中国载人航天 (#10355)
* feat(route): add 中国载人航天

* docs: add category for /fxrw

* update lib/v2/gov/cmse/index.js

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* docs: add xwzt
2022-08-03 01:27:19 +08:00

68 lines
2.1 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
module.exports = async (ctx) => {
ctx.path = ctx.path.replace(/(^\/cmse|\/$)/g, '');
const rootUrl = 'http://www.cmse.gov.cn';
const currentUrl = `${rootUrl}${ctx.path === '' ? '/xwzx/zhxw/' : `${ctx.path}/`}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
let items = $('#list li a')
.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 15)
.toArray()
.map((item) => {
item = $(item);
const pubDate = item.next().text();
const link = new URL(item.attr('href'), currentUrl).href;
return {
title: item.text(),
pubDate: parseDate(pubDate),
link: /\.html$/.test(link) ? link : `${link}#${pubDate}`,
};
});
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
content('.share').remove();
const detailPubTimeMatches = detailResponse.data.match(/__\$pubtime='(.*?)';var/);
item.pubDate = detailPubTimeMatches ? timezone(parseDate(detailPubTimeMatches[1]), +8) : item.pubDate;
item.description = art(path.join(__dirname, 'templates/description.art'), {
video: content('#con_video').html(),
description: content('.TRS_Editor, #content').html(),
});
return item;
})
)
);
ctx.state.data = {
title: $('title').text(),
link: currentUrl,
item: items,
};
};