const got = require('@/utils/got'); const cheerio = require('cheerio'); const iconv = require('iconv-lite'); const timezone = require('@/utils/timezone'); const { parseDate } = require('@/utils/parse-date'); module.exports = async (ctx) => { const type = ctx.params.type ?? 'recommend'; const time = ctx.params.time ?? '5'; const sort = ctx.params.sort ?? '1'; const rootUrl = 'http://blog.sciencenet.cn'; const currentUrl = `${rootUrl}/blog.php?mod=${type}&type=list&op=${time}&ord=${sort}`; const response = await got({ method: 'get', url: currentUrl, responseType: 'buffer', }); const $ = cheerio.load(iconv.decode(response.data, 'gbk')); let items = $('tr td a[title]') .slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 50) .toArray() .map((item) => { item = $(item); return { title: item.text(), link: `${rootUrl}/${item.attr('href')}`, pubDate: new Date(item.next().text()).toUTCString(), }; }); items = await Promise.all( items.map((item) => ctx.cache.tryGet(item.link, async () => { const detailResponse = await got({ method: 'get', url: item.link, responseType: 'buffer', }); const content = cheerio.load(iconv.decode(detailResponse.data, 'gbk')); item.author = content('.xs2').text(); item.description = content('#blog_article').html(); item.pubDate = timezone(parseDate(content('.xg1').eq(5).text()), +8); return item; }) ) ); ctx.state.data = { title: '科学网 - 精选博文', link: currentUrl, item: items, }; };