feat(utils): add utils for WeChat MP (#9487)

Motivation:
There are multiple routes that need to fetch articles from WeChat MP.
However, letting them fetch articles by themselves could potentially
lead to cache key collisions. Even if cache key collisions do not occur,
un-normalized URL could potentially lead to duplicated requests.
What's more, articles from WeChat MP have weird formats and need to be
fixed. Creating a universal function to do this work can create some
ease for new route contributors.

Note:
In order to make this PR atomic as much as possible, I did not touch
those broken routes. Once this PR is merged, I will try to fix them.

Signed-off-by: Rongrong <15956627+Rongronggg9@users.noreply.github.com>
This commit is contained in:
Rongrong
2022-04-07 21:46:15 +08:00
committed by GitHub
parent e0b7ca676d
commit a79cc20ec1
12 changed files with 305 additions and 158 deletions

View File

@@ -1,6 +1,8 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const dayjs = require('dayjs');
const { finishArticleItem } = require('@/utils/wechat-mp');
module.exports = async (ctx) => {
const { biz, aid } = ctx.params;
const aidurl = `&album_id=${aid}`;
@@ -16,33 +18,12 @@ module.exports = async (ctx) => {
list.map(async (item) => {
const link = $(item).attr('data-link').replace('http://', 'https://');
const title = $(item).attr('data-title');
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response2 = await got({
method: 'get',
url: link,
});
const articleHtml = response2.data;
const $2 = cheerio.load(articleHtml);
$2('img').removeAttr('src');
$2('div#js_profile_qrcode').remove();
const content = $2('div#js_content.rich_media_content')
.html()
.replace('iframe/preview.html?width=500&amp;height=375&amp;', 'txp/iframe/player.html?')
.replace('<iframe ', '<iframe width="640" height="360"')
.replace(/data-src/g, 'src');
const author = $2('div#meta_content:not(:last-child)').text();
const single = {
content,
author,
link,
title,
link,
guid: link,
};
ctx.cache.set(link, JSON.stringify(single));
return Promise.resolve(single);
return await finishArticleItem(ctx, single);
})
);
ctx.state.data = {
@@ -50,8 +31,9 @@ module.exports = async (ctx) => {
link: `https://mp.weixin.qq.com/mp/appmsgalbum?__biz=${biz}&action=getalbum${aidurl}`,
item: list.map((item, index) => ({
title: articledata[index].title,
description: $(item).find('.album__item-img').html() + `<br><br>${articledata[index].content}`,
description: $(item).find('.album__item-img').html() + `<br><br>${articledata[index].description}`,
link: articledata[index].link,
guid: articledata[index].guid,
author: articledata[index].author,
pubDate: dayjs.unix($(item).find('.js_article_create_time').text()).format(),
})),