feat: add RSS 涂鸦王国 (#2644)

* add RSS 涂鸦王国

* 移除数据为空时的 item 参数

* 添加缓存


Co-authored-by: DIYgod <diy.d.god@gmail.com>
This commit is contained in:
f00bar
2019-07-22 18:17:06 +08:00
committed by DIYgod
parent ac6977a16e
commit b079acb777
3 changed files with 82 additions and 0 deletions

View File

@@ -157,3 +157,9 @@ pageClass: routes
### 主题 ### 主题
<Route author="xyqfer" example="/dapenti/subject/184" path="/dapenti/subject/:id" :paramsDesc="['主题 id']"/> <Route author="xyqfer" example="/dapenti/subject/184" path="/dapenti/subject/:id" :paramsDesc="['主题 id']"/>
## 涂鸦王国
### 用户上传作品和用户喜欢作品
<Route author="LanceZhu" example="/gracg/user11968EIcqS3" path="/gracg/:user/:love?" :paramsDesc="['用户访问ID用户主页URL获取', '是否切换为用户喜欢作品, 不选或为 0 不切换1则切换']"/>

View File

@@ -1515,6 +1515,9 @@ router.get('/ccdi/scdc', require('./routes/ccdi/scdc'));
router.get('/nosetime/:id/:type/:sort?', require('./routes/nosetime/comment')); router.get('/nosetime/:id/:type/:sort?', require('./routes/nosetime/comment'));
router.get('/nosetime/home', require('./routes/nosetime/home')); router.get('/nosetime/home', require('./routes/nosetime/home'));
// 涂鸦王国
router.get('/gracg/:user/:love?', require('./routes/gracg/user'));
// 大侠阿木 // 大侠阿木
router.get('/daxiaamu/home', require('./routes/daxiaamu/home')); router.get('/daxiaamu/home', require('./routes/daxiaamu/home'));

73
lib/routes/gracg/user.js Normal file
View File

@@ -0,0 +1,73 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const url = ctx.params.love ? `https://www.gracg.com/${ctx.params.user}/love` : `https://www.gracg.com/${ctx.params.user}`;
const response = await got({
method: 'get',
url: `${url}`,
});
const html = response.body;
const $ = cheerio.load(html);
const data = $('.TheWorksList').find('li');
if (data.length === 0) {
ctx.state.data = {
title: `${ctx.params.user} 的涂鸦王国作品`,
link: `https://gracg.com/${ctx.params.user}`,
description: `${ctx.params.user} 的涂鸦王国作品`,
};
} else {
const name = $('.userbox .username').text();
const item = await Promise.all(
data
.slice(0, 10)
.map(async (i, el) => {
const link = $(el)
.find('.imgbox a')
.attr('href');
const pics = await ctx.cache.tryGet(link, async () => {
const res = await got.get(link);
const $1 = cheerio.load(res.body);
return $1('.workPage-images img')
.map((i, el) => $1(el).attr('src'))
.get();
});
const description = generateItemDesc(pics);
return {
title: $(el)
.find('.infobox .titles')
.text(),
description,
pubDate: new Date(
$(el)
.find('.infobox .time')
.text()
).toUTCString(),
link,
};
})
.get()
);
ctx.state.data = {
title: `${name} 的涂鸦王国作品`,
link: `https://gracg.com/${ctx.params.user}`,
description: `${name} 的涂鸦王国作品`,
item: item,
};
}
};
/**
* 根据图片链接生成 html
* @param {Array} pics
* @return {String} itemDesc
*/
function generateItemDesc(pics) {
const itemDesc = pics.reduce((acc, pic) => {
acc += `<img referrerpolicy="no-referrer" src='${pic}><br>`;
return acc;
}, '');
return itemDesc;
}