From 490c1843894c6939a2f7bfcf3a09572d262a4667 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sat, 27 Nov 2021 14:47:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20Odaily=E6=98=9F=E7=90=83?= =?UTF-8?q?=E6=97=A5=E6=8A=A5=E6=B4=BB=E5=8A=A8=20(#7666)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/new-media.md | 6 +++++ lib/router.js | 3 +++ lib/routes/odaily/activity.js | 47 +++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 lib/routes/odaily/activity.js diff --git a/docs/new-media.md b/docs/new-media.md index 2216b5f86b..b2e1034807 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -567,6 +567,12 @@ IPFS 网关有可能失效,那时候换成其他网关。 +## Odaily 星球日报 + +### 活动 + + + ## OpenAI ### Blog diff --git a/lib/router.js b/lib/router.js index 804b09c362..5e6dc9b497 100644 --- a/lib/router.js +++ b/lib/router.js @@ -4221,4 +4221,7 @@ router.get('/questmobile/report/:category?/:label?', require('./routes/questmobi // RSS3 router.get('/rss3/blog', require('./routes/rss3/blog')); +// 星球日报 +router.get('/odaily/activity', require('./routes/odaily/activity')); + module.exports = router; diff --git a/lib/routes/odaily/activity.js b/lib/routes/odaily/activity.js new file mode 100644 index 0000000000..7d344a7538 --- /dev/null +++ b/lib/routes/odaily/activity.js @@ -0,0 +1,47 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); + +module.exports = async (ctx) => { + const rootUrl = 'https://www.odaily.com'; + const currentUrl = `${rootUrl}/service/scheme/group/8?page=1&per_page=10`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const list = response.data.data.items.data.map((item) => ({ + title: item.title, + link: `${rootUrl}/activity/${item.id}`, + pubDate: timezone(new Date(item.published_at), +8), + })); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data.match(/"content":"(.*)"}},"secondaryList":/)[1]); + + content('img').each(function () { + content(this).attr('src', content(this).attr('src').replace(/\\"/g, '')); + }); + + item.description = content.html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: '活动 - Odaily星球日报', + link: `${rootUrl}/activityPage`, + item: items, + }; +};