Files
RSSHub/lib/v2/stcn/index.js
T
Ethan Shen 6c024346b9 fix(route): 证券时报网栏目 (#11798)
* fix(route): 证券时报网栏目

* fix: no detail for files
2023-02-08 00:39:07 +08:00

65 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const got = require('@/utils/got');
const cheerio = require('cheerio');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const id = ctx.params.id ?? 'yw';
const rootUrl = 'https://www.stcn.com';
const currentUrl = `${rootUrl}/article/list/${id}.html`;
const apiUrl = `${rootUrl}/article/list.html?type=${id}`;
const response = await got({
method: 'get',
url: apiUrl,
});
const $ = cheerio.load(response.data);
let items = $('.t, .tt, .title')
.find('a')
.toArray()
.map((item) => {
item = $(item);
const link = item.attr('href');
return {
title: item.text().replace(/(^【|】$)/g, ''),
link: /^http/.test(link) ? link : `${rootUrl}${link}`,
};
});
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
if (/\.html$/.test(item.link)) {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
item.title = content('.detail-title').text();
item.author = content('.detail-info span').first().text().split(':').pop();
item.pubDate = timezone(parseDate(content('.detail-info span').last().text()), +8);
item.category = content('.detail-content-tags div')
.toArray()
.map((t) => content(t).text());
item.description = content('.detail-content').html();
}
return item;
})
)
);
ctx.state.data = {
title: `证券时报网 - ${$('.breadcrumb a').last().text()}`,
link: currentUrl,
item: items,
};
};