Files
RSSHub/lib/v2/oilchem/index.js
T
Ethan ShenandTonyRL 1cd363ef42 feat(route): add 隆众资讯 (#7992)
* feat(route): add 隆众资讯

* fix timezone

* refactor: migrate to v2

Co-authored-by: TonyRL <TonyRL@users.noreply.github.com>
2022-02-23 01:23:59 +08:00

68 lines
2.1 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
const routes = require('./routes');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const type = ctx.params.type ?? '';
const category = ctx.params.category ?? '';
const subCategory = ctx.params.subCategory ?? '';
const route = category === '' ? '' : `/${category}${subCategory === '' ? '' : `/${subCategory}`}`;
const rootUrl = `https://${type === '' ? 'www' : 'list'}.oilchem.net`;
const currentUrl = `${rootUrl}${type === '' ? '/1/' : type === 'list' ? route : `/${routes[`/${type}${route}`]}`}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
const list = $('.list ul ul li a')
.map((_, item) => {
item = $(item);
return {
title: item.text(),
link: item.attr('href'),
};
})
.get();
const items = await Promise.all(
list.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
item.description = content('#content').html();
item.pubDate = timezone(
parseDate(
content('.xq-head')
.find('span')
.text()
.match(/发布时间:\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}/)[0],
'YYYY-MM-DD HH:mm'
),
+8
);
return item;
})
)
);
ctx.state.data = {
title: `${$('.hdbox h3').text()} - 隆众资讯`,
link: currentUrl,
item: items,
};
};