Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add route for beijing municipal health commission (#3952)
- Loading branch information
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const url = require('url'); | ||
const got = require('@/utils/got'); | ||
const cheerio = require('cheerio'); | ||
|
||
const root_url = 'http://wjw.beijing.gov.cn/'; | ||
|
||
const config = { | ||
wnxw: { | ||
link: '/xwzx_20031/wnxw/', | ||
title: '委内新闻', | ||
}, | ||
jcdt: { | ||
link: '/xwzx_20031/jcdt/', | ||
title: '基层动态', | ||
}, | ||
mtjj: { | ||
link: '/xwzx_20031/mtjj/', | ||
title: '媒体聚焦', | ||
}, | ||
rdxws: { | ||
link: '/xwzx_20031/rdxws/', | ||
title: '热点新闻', | ||
}, | ||
}; | ||
|
||
module.exports = async (ctx) => { | ||
const cfg = config[ctx.params.caty]; | ||
if (!cfg) { | ||
throw Error('Bad category. See <a href="https://docs.rsshub.app/government.html#zhong-yang-ji-wei-guo-jia-jian-wei">docs</a>'); | ||
} | ||
|
||
const current_url = url.resolve(root_url, cfg.link); | ||
const response = await got({ | ||
method: 'get', | ||
url: current_url, | ||
}); | ||
const $ = cheerio.load(response.data); | ||
const list = $('div.weinei_left_con div.weinei_left_con_line') | ||
.slice(0, 10) | ||
.map((_, item) => { | ||
item = $(item); | ||
const a = item.find('a[title]'); | ||
return { | ||
title: a.text(), | ||
link: url.resolve(current_url, a.attr('href')), | ||
pubDate: new Date(item.find('div.weinei_left_con_line_date').text() + ' GMT+8').toUTCString(), | ||
}; | ||
}) | ||
.get(); | ||
|
||
const items = await Promise.all( | ||
list.map( | ||
async (item) => | ||
await ctx.cache.tryGet(item.link, async () => { | ||
const res = await got({ method: 'get', url: item.link }); | ||
const content = cheerio.load(res.data); | ||
|
||
item.description = content('div.weinei_left').html(); | ||
return item; | ||
}) | ||
) | ||
); | ||
|
||
ctx.state.data = { | ||
title: '北京卫健委 - ' + cfg.title, | ||
link: root_url, | ||
item: items, | ||
}; | ||
}; |