fix 地震速报 (#1681)

close #1677
This commit is contained in:
Chenyang Shi
2019-03-05 11:09:08 +08:00
committed by DIYgod
parent 46b2bbac60
commit 48e3ca8700
3 changed files with 36 additions and 69 deletions

View File

@@ -2250,7 +2250,7 @@ category 对应的关键词有
### 中国地震局
<route name="地震速报" author="ylc395" example="/earthquake" path="/earthquake">
<route name="地震速报" author="LogicJake" example="/earthquake" path="/earthquake/:region?" :paramsDesc="['区域0全部1国内默认2国外']">
可通过全局过滤参数订阅您感兴趣的地区.

View File

@@ -535,7 +535,7 @@ router.get('/hopper/:lowestOnly/:from/:to?', require('./routes/hopper/index'));
router.get('/mafengwo/note/:type', require('./routes/mafengwo/note'));
// 中国地震局震情速递(与地震台网同步更新)
router.get('/earthquake', require('./routes/earthquake'));
router.get('/earthquake/:region?', require('./routes/earthquake'));
// 笔趣阁
router.get('/biquge/novel/latestchapter/:id', require('./routes/novel/biquge'));

View File

@@ -1,82 +1,49 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
const Datetime = require('luxon').DateTime;
const qs = require('querystring');
module.exports = async (ctx) => {
const link = 'http://www.cea.gov.cn/publish/dizhenj/464/479/index.html';
const html = (await axios.get(link)).data;
const $ = cheerio.load(html);
const $items = $('.list_main_right_conbg_con li');
const items = await Promise.all(
$items.toArray().map(async (el) => {
const $el = $(el);
const url = 'http://www.cea.gov.cn' + $el.find('a').attr('href');
let html = await ctx.cache.get(url);
if (!html) {
html = (await axios.get(url)).data;
ctx.cache.set(url, html, 3 * 24 * 60 * 60);
}
const $1 = cheerio.load(html);
const $content = $1('.detail_main_right_conbg_con > div');
const $container = $('<div>');
const region = ctx.params.region || 1;
const api = 'https://www.cea.gov.cn/eportal/ui?struts.portlet.mode=view&struts.portlet.action=/portlet/expressEarthquake!queryExpressEarthquakeList.action&pageId=363409&moduleId=a852ba487b534470a84a30f00e7d6670';
const link = 'https://www.cea.gov.cn/cea/xwzx/zqsd/index.html';
const response = await axios({
method: 'post',
url: api,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
data: qs.stringify({
region: region,
dateType: 2,
magnitude: 0,
}),
});
$content
.find('img')
.each((i, el) => {
const $el = $(el);
const src = $el.attr('src');
$el.attr('src', 'http://www.cea.gov.cn' + src);
$el.attr('width', '');
})
.appendTo($('<p>'))
.parent()
.appendTo($container);
const data = response.data;
const $scriptEls = $content.find('script');
const info = [
$($scriptEls[0])
.html()
.match(/origTime\("(.+分)/)[1],
$($scriptEls[1])
.html()
.match(/"(.+)"/)[1] + ', ',
$($scriptEls[2])
.html()
.match(/"(.+)"/)[1],
$($scriptEls[3])
.html()
.match(/"(.+)"/)[1],
];
const out = await Promise.all(
data.map(async (item) => {
const id = item.id;
const epicenter = item.epicenter;
const date = item.orig_time.slice(0, -2);
const num = item.num_mag;
const latitudes = item.latitudes;
const longitudes = item.longitudes;
const depth = item.depth;
const text = info.reduce(
(text, field) => text.replace(/<script>[\s\S]+?<\/script>/, field),
$content
.children()
.not('center')
.parent()
.html()
);
$container.prepend(`<p>${text}</p>`);
const dateString = $1('.detail_main_right_conbg_tit')
.children()
.last()
.text()
.trim()
.substring(5);
return {
title: $el.find('a').text(),
link: url,
pubDate: Datetime.fromFormat(dateString, 'yyyy-LL-dd HH:mm:ss').toRFC2822(),
description: $container.html(),
const description = `北京时间${date}${epicenter}(纬度${latitudes}度,经度${longitudes}度)发生${num}级地震,震源深度${depth}千米`;
const single = {
title: `${epicenter}发生${num}级地震`,
link: `https://www.cea.gov.cn/eportal/ui?struts.portlet.mode=view&struts.portlet.action=/portlet/expressEarthquake!toNewInfoView.action&pageId=366521&id=${id}`,
pubDate: new Date(date).toUTCString(),
description: description,
};
return Promise.resolve(single);
})
);
ctx.state.data = {
title: '中国地震局震情速递',
link,
item: items,
item: out,
};
};