Files
RSSHub/lib/v2/dapenti/utils.js
github-actions[bot] 472e0d8f19 style: auto format
2022-04-13 10:11:44 +00:00

76 lines
3.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 iconv = require('iconv-lite');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
module.exports = {
parseFeed: async ({ subjectid }, ctx) => {
const url = `https://www.dapenti.com/blog/blog.asp?name=xilei&subjectid=${subjectid}`;
const listRes = await got({
method: 'get',
url,
headers: {
Referer: url,
},
// 喷嚏网编码为GBK需要转码
// 转码需要设定返回数据的格式其可选项是arraybuffer,blob,document,json,text,stream
// 默认为json
responseType: 'buffer',
});
// 转码
const data = iconv.decode(listRes.data, 'gb2312');
const $ = cheerio.load(data);
// 只取最近的三个取全文rss
const list = $('li', 'ul').slice(0, 3).get();
const result_item = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(`https://www.dapenti.com/blog/${$(item).find('a').attr('href')}`, async () => {
const el = $(item);
const url = `https://www.dapenti.com/blog/${el.find('a').attr('href')}`;
const original_data = await got({
method: 'get',
url,
headers: {
Referer: url,
},
responseType: 'buffer',
});
const convert_data = iconv.decode(original_data.data, 'gbk');
const description = cheerio.load(convert_data, {
decodeEntities: false,
})('body > table > tbody > tr > td.oblog_t_2 > div > table > tbody > tr:nth-child(2) > td');
const pubInfo = description.find('span span.oblog_text').text().split('发布于');
description.find('table, .adsbygoogle').remove();
// remove header
const count = subjectid.toString() === '70' ? 7 : 3;
for (let index = 0; index < count; index++) {
description.children().first().remove();
}
// remove footer
for (let index = 0; index < 8; index++) {
description.children().last().remove();
}
const single = {
title: el.text(),
author: pubInfo[0].trim(),
description: description.html(),
pubDate: timezone(parseDate(pubInfo[1]?.trim()), +8),
link: url,
};
return single;
})
)
);
return {
title: `喷嚏-${subjectid}`,
link: url,
item: result_item,
};
},
};