const got = require('@/utils/got'); const cheerio = require('cheerio'); const config = { this: '2037295071', back: '2330861071', next: '2037296071', }; module.exports = async (ctx) => { const type = ctx.params.type || 'this'; const node = config[type]; if (!node) { throw Error('Bad type. See docs'); } const rootUrl = `https://www.amazon.cn/b?node=${node}`; const response = await got({ method: 'get', url: rootUrl, }); const $ = cheerio.load(response.data); const list = $('.s-access-detail-page') .map((_, item) => { item = $(item); return { link: item.attr('href'), title: item.attr('title'), }; }) .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); item.author = content('span.author').text(); item.description = content('#ebooks-img-canvas').html() + content('#detailBullets_feature_div').html(); return item; }) ) ); ctx.state.data = { title: `KU会员限时免费读 - ${$('title').text().split('-')[1]}`, link: rootUrl, item: items, }; };