From 030f358ed91582e5fa885447830c29a3d24ef9b7 Mon Sep 17 00:00:00 2001 From: tylinux Date: Thu, 22 Apr 2021 14:56:25 +0800 Subject: [PATCH 1/4] add wechat-feeds source --- docs/en/picture.md | 4 +-- docs/new-media.md | 3 +++ docs/picture.md | 6 ++--- lib/router.js | 1 + lib/routes/tencent/wechat/feeds.js | 40 ++++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 lib/routes/tencent/wechat/feeds.js diff --git a/docs/en/picture.md b/docs/en/picture.md index 1c29173078de2c..453e3276bf8653 100644 --- a/docs/en/picture.md +++ b/docs/en/picture.md @@ -74,7 +74,7 @@ pageClass: routes ### Home - + | Home | Hot | Popular | Recent | | ---- | --- | ------- | ------ | @@ -84,7 +84,7 @@ pageClass: routes ### Videos - + | Popular | Recent | | ------- | ------ | diff --git a/docs/new-media.md b/docs/new-media.md index 517115a45782d4..9149a936f79c23 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -2054,6 +2054,9 @@ column 为 third 时可选的 category: +### 公众号 (wechat-feeds 来源) + + ### 公众号栏目 (非推送 & 历史消息) diff --git a/docs/picture.md b/docs/picture.md index 0a1b04379064aa..66e4cde5851c45 100644 --- a/docs/picture.md +++ b/docs/picture.md @@ -88,7 +88,7 @@ pageClass: routes ## Dilbert Comic Strip - + 通过提取漫画,提供比官方源更佳的阅读体验。 @@ -98,7 +98,7 @@ pageClass: routes ### Home - + | Home | Hot | Popular | Recent | | ---- | --- | ------- | ------ | @@ -108,7 +108,7 @@ pageClass: routes ### Videos - + | Popular | Recent | | ------- | ------ | diff --git a/lib/router.js b/lib/router.js index 8e7256c301c98f..0d47bf53ac40d6 100644 --- a/lib/router.js +++ b/lib/router.js @@ -537,6 +537,7 @@ router.get('/wechat/wjdn/:id', require('./routes/tencent/wechat/wjdn')); router.get('/wechat/wxnmh/:id', require('./routes/tencent/wechat/wxnmh')); router.get('/wechat/mp/homepage/:biz/:hid/:cid?', require('./routes/tencent/wechat/mp')); router.get('/wechat/mp/msgalbum/:biz/:aid', require('./routes/tencent/wechat/msgalbum')); +router.get('/wechat/feeds/:id', require('./routes/tencent/wechat/feeds')); // All the Flight Deals router.get('/atfd/:locations/:nearby?', require('./routes/atfd/index')); diff --git a/lib/routes/tencent/wechat/feeds.js b/lib/routes/tencent/wechat/feeds.js new file mode 100644 index 00000000000000..05ab28e149bcd2 --- /dev/null +++ b/lib/routes/tencent/wechat/feeds.js @@ -0,0 +1,40 @@ +const parser = require('@/utils/rss-parser'); +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +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.splice(0, 20).map(async (item) => { + 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: item.pubDate, + link: item.link, + }; + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: `${feed.title}`, + link, + description: feed.description, + item: items, + }; +}; From 2cc9678ee89f885ef5b0b5047b4aea8af674072b Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 22 Apr 2021 06:58:06 +0000 Subject: [PATCH 2/4] style: auto format --- docs/new-media.md | 1 + lib/routes/tencent/wechat/feeds.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/new-media.md b/docs/new-media.md index 9149a936f79c23..11a7734fc6cd0e 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -2055,6 +2055,7 @@ column 为 third 时可选的 category: ### 公众号 (wechat-feeds 来源) + ### 公众号栏目 (非推送 & 历史消息) diff --git a/lib/routes/tencent/wechat/feeds.js b/lib/routes/tencent/wechat/feeds.js index 05ab28e149bcd2..a7cbda6e824cca 100644 --- a/lib/routes/tencent/wechat/feeds.js +++ b/lib/routes/tencent/wechat/feeds.js @@ -4,7 +4,7 @@ const cheerio = require('cheerio'); module.exports = async (ctx) => { const { id } = ctx.params; - const link = `https://github.com/hellodword/wechat-feeds/raw/feeds/${id}.xml` + const link = `https://github.com/hellodword/wechat-feeds/raw/feeds/${id}.xml`; const feed = await parser.parseURL(link); const items = await Promise.all( @@ -12,7 +12,7 @@ module.exports = async (ctx) => { const response = await got.get(item.link); const $ = cheerio.load(response.data); - const post = $('#js_content'); + const post = $('#js_content'); post.find('img').each((_, img) => { const dataSrc = $(img).attr('data-src'); From b2e424631badbd5e1c7837a407045ae1f6fa59e8 Mon Sep 17 00:00:00 2001 From: tylinux Date: Thu, 22 Apr 2021 15:06:43 +0800 Subject: [PATCH 3/4] enable page detail cache --- lib/routes/tencent/wechat/feeds.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/routes/tencent/wechat/feeds.js b/lib/routes/tencent/wechat/feeds.js index a7cbda6e824cca..ca77d278baa407 100644 --- a/lib/routes/tencent/wechat/feeds.js +++ b/lib/routes/tencent/wechat/feeds.js @@ -8,7 +8,12 @@ module.exports = async (ctx) => { const feed = await parser.parseURL(link); const items = await Promise.all( - feed.items.splice(0, 20).map(async (item) => { + 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); @@ -27,6 +32,8 @@ module.exports = async (ctx) => { pubDate: item.pubDate, link: item.link, }; + + ctx.cache.set(item.link, JSON.stringify(single)); return Promise.resolve(single); }) ); From acc30ccf34aff09ba55f716ad3d3118081446bd4 Mon Sep 17 00:00:00 2001 From: tylinux Date: Thu, 13 May 2021 14:11:37 +0800 Subject: [PATCH 4/4] feat(route) fix pubDate format --- lib/routes/tencent/wechat/feeds.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/routes/tencent/wechat/feeds.js b/lib/routes/tencent/wechat/feeds.js index ca77d278baa407..5347a2c87d1bef 100644 --- a/lib/routes/tencent/wechat/feeds.js +++ b/lib/routes/tencent/wechat/feeds.js @@ -29,7 +29,7 @@ module.exports = async (ctx) => { const single = { title: item.title, description: post.html(), - pubDate: item.pubDate, + pubDate: new Date(item.pubDate), link: item.link, };