Files
Sukka d82847f541 style/chore(eslint): enforce new rules (#8040)
* style: prefer object shorthand syntax
* refactor: prefer Array#map over Array#forEach
* style: prefer arrow callback
* chore(eslint): update rules
* style: auto fix by eslint
2021-08-17 22:23:23 +08:00

46 lines
1.4 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
const baseUrl = 'https://www.jdlingyu.mobi/';
const viewProps = {
tuji: '图集',
as: '文章',
};
module.exports = async (ctx) => {
const type = ctx.params.type || 'tuji';
const url = baseUrl + type;
const response = await got({
method: 'get',
url,
});
const $ = cheerio.load(response.data);
const items = await Promise.all(
$('#post-list > ul.b2_gap > li')
.get()
.map(async (div) => {
const a = $(div).find('.post-info > h2 > a');
const link = a.attr('href');
const description = await ctx.cache.tryGet(link, async () => {
const response = await got.get(link);
const $ = cheerio.load(response.data);
return $('.entry-content > p').html();
});
return Promise.resolve({
title: a.text(),
description,
author: $(div).find('.post-header .users').text(),
link,
pubDate: new Date($(div).find('.post-header time').attr('datetime')).toUTCString(),
});
})
);
ctx.state.data = {
title: `${viewProps[type]} - 绝对领域`,
link: url,
description: `${viewProps[type]} - 绝对领域`,
item: items,
};
};