Files
RSSHub/lib/routes/aliyun-kernel/index.js
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

61 lines
1.6 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const baseUrl = 'https://kernel.taobao.org/';
const response = await got({
method: 'get',
url: baseUrl,
headers: {
Referer: baseUrl,
},
});
const data = response.data;
const $ = cheerio.load(data);
const list = $('.container .post-item').get();
const ProcessFeed = (data) => {
const $ = cheerio.load(data);
// 提取内容
return {
desc: $('.container .post-content').html(),
publish_time: $('.post-meta > time').attr('datetime'),
};
};
const items = await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const $a = $('.article-title > a');
const link = baseUrl + $a.attr('href');
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await got({
method: 'get',
url: link,
});
const feed = ProcessFeed(response.data);
const description = feed.desc;
const pubDate = feed.publish_time;
const single = {
title: $a.text(),
description,
link,
pubDate,
};
ctx.cache.set(link, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: 'Kernel Aliyun',
link: baseUrl,
item: items,
};
};