Files
RSSHub/lib/v2/watchout/index.js
2021-11-27 08:56:09 +00:00

52 lines
1.3 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const rootUrl = 'https://watchout.tw';
const currentUrl = `${rootUrl}/docs`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
let items = $('.title .a-text')
.toArray()
.map((item) => {
item = $(item);
return {
title: item.text(),
link: item.attr('href'),
pubDate: parseDate(item.parent().parent().find('.date').text()),
};
});
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
item.author = content('.name').eq(0).text();
item.description = content('.content').html();
return item;
})
)
);
ctx.state.data = {
title: $('title').text(),
link: currentUrl,
item: items,
};
};