const got = require('@/utils/got'); const cheerio = require('cheerio'); const { parseDate } = require('@/utils/parse-date'); const { defaultDomain, getRootUrl } = require('./utils'); module.exports = async (ctx) => { const id = ctx.params.id; const { domain = defaultDomain } = ctx.query; const rootUrl = getRootUrl(domain, ctx); const currentUrl = `${rootUrl}/album/${id}`; const response = await got({ method: 'get', url: currentUrl, }); const $ = cheerio.load(response.data); const category = $('span[data-type="tags"]') .first() .find('a') .toArray() .map((c) => $(c).text()); const author = $('span[data-type="author"]') .first() .find('a') .toArray() .map((a) => $(a).text()) .join(', '); let items = $('.btn-toolbar') .first() .find('a') .toArray() .map((item) => { item = $(item); return { title: item.text(), link: `${rootUrl}${item.attr('href')}`, guid: `https://18comic.org${item.attr('href')}`, pubDate: parseDate(item.find('.hidden-xs').text()), }; }); items = await Promise.all( items.map((item) => ctx.cache.tryGet(item.guid, async () => { const detailResponse = await got({ method: 'get', url: item.link, }); const content = cheerio.load(detailResponse.data); content('.tab-content').remove(); item.author = author; item.category = category; item.description = ``; return item; }) ) ); ctx.state.data = { title: $('title').text(), link: currentUrl, item: items, description: $('meta[property="og:description"]').attr('content'), }; };