feat: add 致美化最新主题 (#4850)

This commit is contained in:
Ethan Shen 2020-05-26 11:25:37 +08:00 committed by GitHub
parent 2f0205011a
commit b75ea3981f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 0 deletions

View File

@ -232,3 +232,9 @@ pageClass: routes
### 用户上传作品和用户喜欢作品
<Route author="LanceZhu" example="/gracg/user11968EIcqS3" path="/gracg/:user/:love?" :paramsDesc="['用户访问ID用户主页URL获取', '是否切换为用户喜欢作品, 不选或为 0 不切换1则切换']"/>
## 致美化
### 最新主题
<Route author="nczitzk" example="/zhutix/latest" path="/zhutix/latest"/>

View File

@ -2756,4 +2756,7 @@ router.get('/pbc/tradeAnnouncement', require('./routes/pbc/tradeAnnouncement'));
// Monotype
router.get('/monotype/article', require('./routes/monotype/article'));
// 致美化
router.get('/zhutix/latest', require('./routes/zhutix/latest'));
module.exports = router;

View File

@ -0,0 +1,42 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const currentUrl = 'https://zhutix.com/';
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
const list = $('#main div.grid-bor div.content-card')
.slice(0, 10)
.map((_, item) => {
item = $(item);
const a = item.find('a');
return {
title: a.text(),
link: a.attr('href'),
pubDate: new Date(item.find('i.feng-liulanjilu').parent().text()).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('#entry-content').html();
return item;
})
)
);
ctx.state.data = {
title: '致美化 - 最新主题',
link: currentUrl,
item: items,
};
};