Files
RSSHub/lib/routes/gov/zhengce/govall.js
Cloud 6084a20401 fix: govall some link 404 (#2894)
* fix: govall some link 404

* fix: eslint warning
2019-08-21 11:44:45 +08:00

60 lines
2.0 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const { advance } = ctx.params;
const link = `http://sousuo.gov.cn/list.htm`;
const params = new URLSearchParams({
n: 20,
t: 'govall',
sort: 'pubtime',
advance: 'true',
});
const query = `${params.toString()}&${advance}`;
const res = await got.get(link, {
query: query.replace(/([\u4e00-\u9fa5])/g, (str) => encodeURIComponent(str)),
});
const $list = cheerio.load(res.data);
ctx.state.data = {
title: `信息稿件 - 中国政府网`,
link: `${link}?${query}`,
item: await Promise.all(
$list('body > div.dataBox > table > tbody > tr')
.slice(1)
.map(async (_, el) => {
const item = $list(el);
const number = item.find('td.info').next();
const title = item.find('td.info > a');
const href = title.attr('href');
const key = `gov_zhengce: ${href}`;
let description;
const value = await ctx.cache.get(key);
if (value) {
description = value;
} else {
try {
const contentData = await got.get(href);
const $content = cheerio.load(contentData.data);
description = $content('#UCAP-CONTENT').html();
} catch (error) {
description = '文章已被删除';
}
ctx.cache.set(key, description);
}
return {
title: number.text() + ' | ' + title.text(),
description,
link: href,
};
})
.get()
),
};
};