feat: 添加Lofter话题 (#3146)

This commit is contained in:
hoilc 2019-09-26 23:27:46 +08:00 committed by DIYgod
parent ffeb1abdd5
commit 930880170f
3 changed files with 80 additions and 0 deletions

View File

@ -270,6 +270,22 @@ pageClass: routes
<Route author="Dectinc DIYgod" example="/keep/user/556b02c1ab59390afea671ea" path="/keep/user/:id" :paramsDesc="['Keep 用户 id']"/>
## Lofter
::: tip 提示
官方提供了用户主页 RSS: http://**:username**.lofter.com/rss
:::
### 话题(标签)
<Route author="hoilc" example="/lofter/tag/名侦探柯南/date" path="/lofter/tag/:name/:type?" :paramsDesc="['话题名(标签名) 例如 `名侦探柯南`', '排行类型, 默认显示最新话题, 取值如下']"/>
| new | date | week | month | total |
| ---- | ---- | ---- | ----- | ----- |
| 最新 | 日榜 | 周榜 | 月榜 | 总榜 |
## pixiv
### 用户收藏

View File

@ -1754,4 +1754,7 @@ router.get('/taptap/topic/:id/:label?', require('./routes/taptap/topic'));
router.get('/taptap/changelog/:id', require('./routes/taptap/changelog'));
router.get('/taptap/review/:id/:order?', require('./routes/taptap/review'));
// lofter
router.get('/lofter/tag/:name/:type?', require('./routes/lofter/tag'));
module.exports = router;

61
lib/routes/lofter/tag.js Normal file
View File

@ -0,0 +1,61 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const titles = {
date: '日榜',
week: '周榜',
month: '月榜',
total: '总榜',
new: '最新',
};
module.exports = async (ctx) => {
const name = ctx.params.name;
const type = ctx.params.type ? ctx.params.type : 'new';
const url = `http://www.lofter.com/tag/${encodeURIComponent(name)}/${type}/`;
const response = await got.get(url);
const $ = cheerio.load(response.data);
const list = $('.m-mlist > .mlistcnt')
.slice(1)
.toArray();
ctx.state.data = {
title: `lofter话题 - ${name} - ${titles[type]}`,
link: url,
item: list.map((item) => {
item = $(item);
const author = item
.find('.w-who > .ptag')
.first()
.text()
.trim();
const images = item.find('img[data-origin]');
for (let k = 0; k < images.length; k++) {
$(images[k]).replaceWith(`<img src="${$(images[k]).attr('data-origin')}" />`);
}
item.find('.imgc[style]').removeAttr('style');
item.find('.js-digest').remove();
item.find('a:not([href])').each(function() {
$(this).replaceWith($(this).html());
});
const title = item.find('.tit');
return {
title: title.length === 0 ? `${author}的图片` : title.text().trim(),
author: author,
description: item.find('.m-icnt.ctag > .cnt').html(),
link: item.find('a.isayc').attr('href'),
pubDate: new Date(item.find('a.isayc').attr('data-time') * 1),
};
}),
};
};