Files
RSSHub/lib/routes/lwn/alerts.js
2019-06-26 02:04:32 +08:00

58 lines
1.8 KiB
JavaScript

const url = require('url');
const got = require('@/utils/got');
const date = require('@/utils/date');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const distributor = ctx.params.distributor;
const listUrl = `https://lwn.net/Alerts/${distributor}/`;
const res = await got({
method: 'get',
url: listUrl,
});
const $ = cheerio.load(res.data);
const title = $('.PageHeadline').text();
const list = $('.ArticleText').find('table tbody tr:not(:first-of-type)');
const count = [];
for (let i = 0; i < list.length; i++) {
count.push(i);
}
const resultItem = await Promise.all(
count.map(async (i) => {
const each = $(list[i]);
const originalUrl = each.find('td > a').attr('href');
const link = url.resolve('https://lwn.net', originalUrl);
const pubDate = date(each.find('td:nth-of-type(3)').text());
const cacheKey = `LWN.net:${link}`;
const cacheValue = await ctx.cache.get(cacheKey);
let item;
if (cacheValue) {
item = JSON.parse(cacheValue);
} else {
const detail = await got({
method: 'get',
url: link,
});
const content = cheerio.load(detail.data);
item = {
title: content('.PageHeadline').text(),
description: content('.ArticleText').html(),
link: link,
pubDate: pubDate,
};
ctx.cache.set(cacheKey, JSON.stringify(item));
}
return Promise.resolve(item);
})
);
ctx.state.data = {
title: title,
link: listUrl,
item: resultItem,
};
};