feat: add 文学城焦点新闻 (#5845)

This commit is contained in:
Henry Wang 2020-10-10 15:31:02 +01:00 committed by GitHub
commit 485e100e32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 0 deletions

View File

@ -449,6 +449,10 @@ pageClass: routes
<Route author="changlan" example="/wenxuecity/bbs/tzlc" path="/wenxuecity/bbs/:cat/:elite?" :paramsDesc="['版面名, 可在 URL 中找到', '是否精华区, 1 为精华区']" radar="1" />
### 焦点新闻
<Route author="nczitzk" example="/wenxuecity/news" path="/wenxuecity/news" />
## 小米社区
### 圈子

View File

@ -3198,6 +3198,7 @@ router.get('/itslaw/judgements/:conditions', require('./routes/itslaw/judgements
router.get('/wenxuecity/blog/:id', require('./routes/wenxuecity/blog'));
router.get('/wenxuecity/bbs/:cat/:elite?', require('./routes/wenxuecity/bbs'));
router.get('/wenxuecity/hot/:cid', require('./routes/wenxuecity/hot'));
router.get('/wenxuecity/news', require('./routes/wenxuecity/news'));
// 不安全
router.get('/buaq', require('./routes/buaq/index'));

View File

@ -0,0 +1,56 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const rootUrl = 'https://www.wenxuecity.com';
const currentUrl = `${rootUrl}/news/`;
const response = await got({
method: 'get',
url: currentUrl,
https: {
rejectUnauthorized: false,
},
});
const $ = cheerio.load(response.data);
const list = $('div.mainwrap div.block div.wrapper ul li a')
.slice(0, 15)
.map((_, item) => {
item = $(item);
return {
title: item.text(),
link: `${rootUrl}${item.attr('href')}`,
};
})
.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,
https: {
rejectUnauthorized: false,
},
headers: {
Referer: currentUrl,
},
});
const content = cheerio.load(detailResponse.data);
item.description = content('#articleContent').html();
item.pubDate = new Date(content('time[itemprop="datePublished"]').text() + ' GMT+8').toUTCString();
return item;
})
)
);
ctx.state.data = {
title: '焦点新闻 - 文学城',
link: currentUrl,
item: items,
};
};