From a79cc20ec1d79c6af3d7bd046a7b505229f3dbe9 Mon Sep 17 00:00:00 2001
From: Rongrong <15956627+Rongronggg9@users.noreply.github.com>
Date: Thu, 7 Apr 2022 21:46:15 +0800
Subject: [PATCH] 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>
---
docs/new-media.md | 4 +-
lib/routes/tencent/wechat/_README | 1 +
lib/routes/tencent/wechat/feeds.js | 40 ++----
lib/routes/tencent/wechat/mp.js | 34 ++---
lib/routes/tencent/wechat/msgalbum.js | 32 ++---
lib/routes/tencent/wechat/tgchannel.js | 32 +----
lib/utils/wechat-mp.js | 164 +++++++++++++++++++++++++
lib/v2/gzh360/_README | 1 +
lib/v2/gzh360/utils.js | 3 +-
lib/v2/wechat/_README | 1 +
lib/v2/wechat/feeddd.js | 49 +-------
test/utils/wechat-mp.js | 102 +++++++++++++++
12 files changed, 305 insertions(+), 158 deletions(-)
create mode 100644 lib/routes/tencent/wechat/_README
create mode 100644 lib/utils/wechat-mp.js
create mode 100644 lib/v2/gzh360/_README
create mode 100644 lib/v2/wechat/_README
create mode 100644 test/utils/wechat-mp.js
diff --git a/docs/new-media.md b/docs/new-media.md
index b29581aaa..24ade1422 100644
--- a/docs/new-media.md
+++ b/docs/new-media.md
@@ -3344,7 +3344,7 @@ column 为 third 时可选的 category:
### 公众号(CareerEngine 来源)
-
+
### 公众号(Telegram 频道来源)
@@ -3380,7 +3380,7 @@ column 为 third 时可选的 category:
### 公众号 (wxnmh.com 来源)
-
+
### 公众号 (wechat-feeds 来源)
diff --git a/lib/routes/tencent/wechat/_README b/lib/routes/tencent/wechat/_README
new file mode 100644
index 000000000..7b066658f
--- /dev/null
+++ b/lib/routes/tencent/wechat/_README
@@ -0,0 +1 @@
+Make sure you read lib/utils/wechat-mp.js before adding a new route.
diff --git a/lib/routes/tencent/wechat/feeds.js b/lib/routes/tencent/wechat/feeds.js
index 8edfbb3fa..799ad0a16 100644
--- a/lib/routes/tencent/wechat/feeds.js
+++ b/lib/routes/tencent/wechat/feeds.js
@@ -1,42 +1,18 @@
const parser = require('@/utils/rss-parser');
-const got = require('@/utils/got');
-const cheerio = require('cheerio');
+const { finishArticleItem } = require('@/utils/wechat-mp');
module.exports = async (ctx) => {
const { id } = ctx.params;
const link = `https://github.com/hellodword/wechat-feeds/raw/feeds/${id}.xml`;
const feed = await parser.parseURL(link);
- const items = await Promise.all(
- feed.items.map(async (item) => {
- const cache = await ctx.cache.get(item.link);
- if (cache) {
- return Promise.resolve(JSON.parse(cache));
- }
-
- const response = await got.get(item.link);
-
- const $ = cheerio.load(response.data);
- const post = $('#js_content');
-
- post.find('img').each((_, img) => {
- const dataSrc = $(img).attr('data-src');
- if (dataSrc) {
- $(img).attr('src', dataSrc);
- }
- });
-
- const single = {
- title: item.title,
- description: post.html(),
- pubDate: new Date(item.pubDate),
- link: item.link,
- };
-
- ctx.cache.set(item.link, JSON.stringify(single));
- return Promise.resolve(single);
- })
- );
+ const items = feed.items.map((item) => ({
+ title: item.title,
+ pubDate: new Date(item.pubDate),
+ link: item.link,
+ guid: item.link,
+ }));
+ await Promise.all(items.map(async (item) => await finishArticleItem(ctx, item)));
ctx.state.data = {
title: feed.title,
diff --git a/lib/routes/tencent/wechat/mp.js b/lib/routes/tencent/wechat/mp.js
index a59525c0f..308fb2e12 100755
--- a/lib/routes/tencent/wechat/mp.js
+++ b/lib/routes/tencent/wechat/mp.js
@@ -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, hid, cid } = ctx.params;
let cidurl = '';
@@ -26,32 +28,11 @@ module.exports = async (ctx) => {
const mptitle = $('div.articles_header').find('a').text() + `|` + $('div.articles_header > h2.rich_media_title').text();
const articledata = await Promise.all(
list.map(async (item) => {
- const link = item.link.replace('http://', 'https://');
- 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&height=375&', 'txp/iframe/player.html?')
- .replace('