From f3e069d399c81a4cbcd0bff28fe65b0c5d3ce9a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=BB=E5=B9=B3=E7=94=9F?= Date: Tue, 19 Apr 2022 01:29:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(utils):=20=E6=94=AF=E6=8C=81=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E5=85=AC=E4=BC=97=E5=8F=B7=E5=8D=95=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E6=96=87=E7=AB=A0=E6=8A=93=E5=8F=96;=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E9=98=85=E8=AF=BB=E5=8E=9F=E6=96=87=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=20(#9557)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(utils): 支持微信公众号单图片文章抓取 * fix(utils): 支持输出微信公众号转载文章阅读原文链接 --- lib/utils/wechat-mp.js | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/utils/wechat-mp.js b/lib/utils/wechat-mp.js index e24b64db0..ba948c7aa 100644 --- a/lib/utils/wechat-mp.js +++ b/lib/utils/wechat-mp.js @@ -38,16 +38,29 @@ const replaceTag = ($, oldTag, newTagName) => { const detectOriginalArticleUrl = ($) => { // No article content get, try the original url + // example: https://mp.weixin.qq.com/s/f6sKObaZZhADTYU2Jl5Bnw if (!$('#js_content').text()) { return $('#js_share_source').attr('data-url'); } // Article content is too short, try the first link + // example: https://mp.weixin.qq.com/s/9saVB4KaolRyJfpajzeFRg if ($('#js_content').text().length < 80) { return $('#js_content a').attr('href'); } return null; }; +const detectSourceUrl = ($) => { + const matchs = $.root() + .html() + .match(/msg_source_url = '(.+)';/); + + if (matchs) { + return matchs[1]; + } + return null; +}; + /** * Articles from WeChat MP have weird formats, this function is used to fix them. * @@ -90,6 +103,18 @@ const fixArticleContent = (html, skipImg = false) => { replaceTag($, section, 'div'); } }); + + // fix single picture article + // example: https://mp.weixin.qq.com/s/4p5YmYuASiQSYFiy7KqydQ + $('script').each((_, script) => { + script = $(script); + const matchs = script.html().match(/document\.getElementById\('js_image_desc'\)\.innerHTML = "(.*)"\.replace/); + + if (matchs) { + script.replaceWith(matchs[1].replace(/\r/g, '').replace(/\n/g, '
').replace(/\\x0d/g, '').replace(/\\x0a/g, '
')); + } + }); + // clean scripts $('script').remove(); return $.html(); @@ -156,11 +181,11 @@ const fetchArticle = async (ctx, url, bypassHostCheck = false) => { const response = await got(url); const $ = cheerio.load(response.data); - const title = $('meta[property="og:title"]').attr('content'); + const title = $('meta[property="og:title"]').attr('content').replace(/\\r/g, '').replace(/\\n/g, ' '); const author = $('meta[name=author]').attr('content'); let summary = $('meta[name=description]').attr('content'); summary = summary !== title ? summary : ''; - let description = fixArticleContent($('div#js_content.rich_media_content')); + let description = fixArticleContent($('#js_content')); // No article get or article is too short, try the original url const originalUrl = detectOriginalArticleUrl($); @@ -171,6 +196,11 @@ const fetchArticle = async (ctx, url, bypassHostCheck = false) => { description += fixArticleContent(original$('#js_content')); } + const sourceUrl = detectSourceUrl($); + if (sourceUrl) { + description += `阅读原文`; + } + let pubDate; const publish_time_script = $('script[nonce][type="text/javascript"]:contains("var ct")').first().html(); const publish_time_match = publish_time_script && publish_time_script.match(/var ct *= *"?(\d{10})"?/);