fix(utils): 支持将微信公众号转载文章的正文抓取回来 (#9534)

* feat: 修正日期时间匹配规则、移除一些不必要评论元素

* fix(route)(fortunechina): 修正财富中文网1、双语文章中文内容重复问题;2、移除 kol 大头像

* fix(route)(wechat): 支持将微信公众号转载文章的正文抓取回来
This commit is contained in:
任平生 2022-04-15 19:23:09 +08:00 committed by GitHub
parent 47715045ba
commit eb467afae1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 1 deletions

View File

@ -35,6 +35,18 @@ const replaceTag = ($, oldTag, newTagName) => {
oldTag.replaceWith(NewTag);
};
const detectOriginalArticleUrl = ($) => {
// No article content get, try the original url
if (!$('#js_content').text()) {
return $('#js_share_source').attr('data-url');
}
// Article content is too short, try the first link
if ($('#js_content').text().length < 80) {
return $('#js_content a').attr('href');
}
return null;
};
/**
* Articles from WeChat MP have weird formats, this function is used to fix them.
*
@ -147,7 +159,16 @@ const fetchArticle = async (ctx, url, bypassHostCheck = false) => {
const author = $('meta[name=author]').attr('content');
let summary = $('meta[name=description]').attr('content');
summary = summary !== title ? summary : '';
const description = fixArticleContent($('div#js_content.rich_media_content'));
let description = fixArticleContent($('div#js_content.rich_media_content'));
// No article get or article is too short, try the original url
const originalUrl = detectOriginalArticleUrl($);
if (originalUrl) {
// try to fetch the description from the original article
const originalResponse = await got(normalizeUrl(originalUrl, bypassHostCheck));
const original$ = cheerio.load(originalResponse.data);
description += fixArticleContent(original$('#js_content'));
}
let pubDate;
const publish_time_script = $('script[nonce][type="text/javascript"]:contains("var ct")').first().html();