diff --git a/lib/utils/wechat-mp.js b/lib/utils/wechat-mp.js index 8e3278019..96723029c 100644 --- a/lib/utils/wechat-mp.js +++ b/lib/utils/wechat-mp.js @@ -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();