From 39db63284625f8cd0375a2ce1995ae83d7ba9a6a Mon Sep 17 00:00:00 2001 From: tylinux Date: Sat, 15 May 2021 11:17:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20=E5=BE=AE=E4=BF=A1=E5=85=AC?= =?UTF-8?q?=E4=BC=97=E5=8F=B7=E6=96=B0=E5=A2=9E=20wechat-feeds=20=E6=9D=A5?= =?UTF-8?q?=E6=BA=90=20(#7419)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: GitHub Action --- docs/en/picture.md | 4 +-- docs/new-media.md | 4 +++ docs/picture.md | 6 ++-- lib/router.js | 1 + lib/routes/tencent/wechat/feeds.js | 47 ++++++++++++++++++++++++++++++ 5 files changed, 57 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 2d6f6acd17cbcb..2d053968618a2e 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -2066,6 +2066,10 @@ 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 0219fd5d0f18a2..9bf400d3ffdab8 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..5347a2c87d1bef --- /dev/null +++ b/lib/routes/tencent/wechat/feeds.js @@ -0,0 +1,47 @@ +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.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); + }) + ); + + ctx.state.data = { + title: `${feed.title}`, + link, + description: feed.description, + item: items, + }; +};