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('/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')); router.get('/biquge/novel/latestchapter/:id', require('./routes/novel/biquge'));

View File

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