feat: add 半月谈时事大事库 (#6711)

* feat: add 半月谈时事大事库

* fix: fetch 10 posts each time
This commit is contained in:
Ethan Shen 2021-01-18 04:49:11 +08:00 committed by GitHub
parent fc6ad8a74d
commit eb972671b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 0 deletions

View File

@ -684,6 +684,18 @@ Supported sub-sites:
<Route author="nwindz" example="/hinatazaka46/blog" path="/hinatazaka46/blog" />
## 半月谈
### 时事大事库
<Route author="nczitzk" example="/banyuetan/byt" path="/banyuetan/byt/:time?" :paramsDesc="['时间,见下表,默认为每周']">
| 每周 | 每月 |
| ------------- | ----- |
| shishidashiku | yiyue |
</Route>
## 報導者
### 最新

View File

@ -1536,6 +1536,7 @@ router.get('/zjgsu/xszq', require('./routes/universities/zjgsu/xszq/scripts'));
router.get('/dianping/user/:id?', require('./routes/dianping/user'));
// 半月谈
router.get('/banyuetan/byt/:time?', require('./routes/banyuetan/byt'));
router.get('/banyuetan/:name', require('./routes/banyuetan'));
// 人民日报

View File

@ -0,0 +1,51 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const time = ctx.params.time || 'shishidashiku';
const rootUrl = 'http://www.banyuetan.org';
const currentUrl = `${rootUrl}/byt/${time}/index.html`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
const list = $('.byt_mian_image_list_con ul li a')
.slice(0, 10)
.map((_, item) => {
item = $(item);
return {
link: item.attr('href'),
title: item.attr('title'),
};
})
.get();
const items = await Promise.all(
list.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
item.description = content('#detail_content').html();
item.author = content('.detail_tit_source').text().replace('来源:', '');
item.pubDate = new Date(content('meta[property="og:release_date"]').attr('content')).toUTCString();
return item;
})
)
);
ctx.state.data = {
title: $('title').text(),
link: currentUrl,
item: items,
};
};