diff --git a/docs/README.md b/docs/README.md index 444b9ca6a2..df8377e186 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2250,7 +2250,7 @@ category 对应的关键词有 ### 中国地震局 - + 可通过全局过滤参数订阅您感兴趣的地区. diff --git a/lib/router.js b/lib/router.js index e18202bec2..b3b804f8e5 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/earthquake/index.js b/lib/routes/earthquake/index.js index 57aa3af721..3daa3b1753 100644 --- a/lib/routes/earthquake/index.js +++ b/lib/routes/earthquake/index.js @@ -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 = $('
'); + 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($('

')) - .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(/