feat: add 孔夫子旧书网店铺上架 (#5595)

Co-authored-by: Henry Wang <hi@henry.wang>
This commit is contained in:
Ethan Shen
2020-09-06 03:53:56 +08:00
committed by GitHub
parent 53d2d5bb9e
commit 4dc910a3ff
3 changed files with 73 additions and 10 deletions

View File

@@ -161,6 +161,16 @@ count 的取值范围为 1-12为防止请求次数过多推荐设置为 5
</Route>
## 孔夫子旧书网
### 用户动态
<Route author="nczitzk" example="/kongfz/people/5032170" path="/kongfz/people/:id" :paramsDesc="['用户 id, 可在对应用户页 URL 中找到']"/>
### 店铺上架
<Route author="nczitzk" example="/kongfz/shop/238901/1" path="/kongfz/shop/:id/:cat?" :paramsDesc="['店铺 id, 可在对应店铺页 URL 中找到', '分类 id可在对应分类页 URL 中找到,默认为店铺最新上架']"/>
## 飘天文学
### 章节

View File

@@ -3129,20 +3129,14 @@ router.get('/shiep/:type', require('./routes/universities/shiep/index'));
// 福建新闻
router.get('/fjnews/:city/:limit', require('./routes/fjnews/fznews'));
router.get('/fjnews/fj/30', require('./routes/fjnews/fznews'));
// 福州新闻
router.get('/fjnews/fz/30', require('./routes/fjnews/fznews'));
// 九江新闻jjnews
router.get('/fjnews/jjnews', require('./routes/fjnews/jjnews'));
// XMind
router.get('/xmind/mindmap/:lang?', require('./routes/xmind/mindmap'));
// 孔夫子旧书网
router.get('/kongfz/people/:id', require('./routes/kongfz/people'));
router.get('/kongfz/shop/:id/:cat?', require('./routes/kongfz/shop'));
// XMind
router.get('/xmind/mindmap/:lang?', require('./routes/xmind/mindmap'));
// 小刀娱乐网
router.get('/x6d/:id?', require('./routes/x6d/index'));

59
lib/routes/kongfz/shop.js Normal file
View File

@@ -0,0 +1,59 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
ctx.params.cat = ctx.params.cat ? '/cat_' + ctx.params.cat : '';
const shopUrl = `http://shop.kongfz.com/${ctx.params.id}${ctx.params.cat}`;
const shopResponse = await got({
method: 'get',
url: shopUrl,
});
const $ = cheerio.load(shopResponse.data);
const list = $(ctx.params.cat ? 'div.list-content div.item-row' : 'div.newest_content span.rareBook_content_list')
.slice(0, 15)
.map((_, item) => {
item = $(item);
const a = ctx.params.cat ? item.find('a').eq(0) : item.find('div.rareBook_content_title a');
return {
title: item.text(),
link: a.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,
});
const content = cheerio.load(detailResponse.data);
content('#eventreport').remove();
content('div.buy-group').remove();
content('div.weixin-popup-bg').remove();
const imgHtml = `<img src="${content('ul.lg-list li img').attr('src')}">`;
item.description = content('div.major-function').html() + imgHtml;
item.pubDate = new Date(content('span.up-book-date-time').text());
return item;
})
)
);
const titleSplit = $('title').text().split('_');
ctx.state.data = {
title: `孔夫子旧书网 - ${titleSplit[titleSplit.length - 2]}`,
link: shopUrl,
item: items,
};
};