RSSHub/lib/v2/tangshufang/index.js

60 lines
1.6 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const category = ctx.params.category ?? '';
const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 10;
const rootUrl = 'https://www.tangshufang.com';
const currentUrl = `${rootUrl}${category ? `/${category}` : ''}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
let items = $('article')
.slice(0, limit)
.toArray()
.map((item) => {
item = $(item);
const a = item.find('h2 a');
return {
title: a.text(),
link: a.attr('href'),
pubDate: parseDate(item.find('time').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.description = content('.wxsyncmain').html();
item.category = content('a[rel="category tag"]')
.toArray()
.map((a) => content(a).text());
return item;
})
)
);
ctx.state.data = {
title: $('title').text(),
link: currentUrl,
item: items,
};
};