Files
RSSHub/lib/v2/wechat/wxnmh.js
任平生 9d9926d0bf fix(utils): 修复抓取微信已删除文章时遇到的报错 (#9589)
* fix(utils): 支持微信公众号单图片文章抓取

* fix(utils): 支持输出微信公众号转载文章阅读原文链接

* fix(utils): 修复抓取微信已删除文章时遇到的报错

* refactor: migrate to v2

Co-authored-by: blankyu(于海洋) <blankyu@tencent.com>
2022-04-21 21:40:16 +08:00

47 lines
1.6 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
const date = require('@/utils/date');
module.exports = async (ctx) => {
const origin = 'https://www.wxnmh.com/';
const response = await got.get(`${origin}user-${ctx.params.id}.htm`);
const $ = cheerio.load(response.data);
const name = $('#body .col-lg-9 .card-block .col-md-9 h3').text().trim();
const description = $('#body .col-lg-9 .card-block .col-md-9 p').text().trim();
const links = $('#body tr.thread .subject a')
.map((index, ele) => {
const title = $(ele).text();
const link = origin + $(ele).attr('href');
return { title, link };
})
.get();
const item = await Promise.all(
links.map((item) =>
ctx.cache.tryGet(item.link, async () => {
try {
const res = await got.get(item.link);
const $ = cheerio.load(res.data);
const desc = $('#js_content')
.html()
.replace(/data-src="http/g, 'src="http');
item.description = `<div style="max-width: 800px;margin: 0 auto;text-align: center;">${desc}</div>`;
item.pubDate = date($('.date').text());
return item;
} catch (err) {
return Promise.resolve('');
}
})
)
);
ctx.state.data = {
title: `${name} - 微信公众号`,
link: 'https://www.wxnmh.com/',
description,
item,
};
};