From 3f9fa7c46eb33c16e15fb5c5c184a5b816e021d5 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Thu, 24 Dec 2020 01:34:44 +0800 Subject: [PATCH] feat: add day one blog (#6478) --- docs/en/new-media.md | 7 +++++ docs/new-media.md | 6 +++++ lib/router.js | 2 ++ lib/routes/dayone/blog.js | 57 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 lib/routes/dayone/blog.js diff --git a/docs/en/new-media.md b/docs/en/new-media.md index 713b6e495..86e1e7d15 100644 --- a/docs/en/new-media.md +++ b/docs/en/new-media.md @@ -80,6 +80,13 @@ Compared to the official one, the RSS feed generated by RSSHub not only has more | | 2 | 6 | 14 | 3 | 4 | 10 | 8 | 1 | 9 | 7 | + +## Day One + +### Blog + + + ## DeepMind ### Blog diff --git a/docs/new-media.md b/docs/new-media.md index f0bec4aac..1c91abba0 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -124,6 +124,12 @@ pageClass: routes +## Day One + +### Blog + + + ## DeepL ### Blog diff --git a/lib/router.js b/lib/router.js index bb186030b..680f013f8 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3724,6 +3724,8 @@ router.get('/mathunion/fields-medal', require('./routes/mathunion/fields-medal') // ACM router.get('/acm/amturingaward', require('./routes/acm/amturingaward')); +// Day One +router.get('/dayone/blog', require('./routes/dayone/blog')); // 滴答清单 router.get('/dida365/habit/checkins', require('./routes/dida365/habit-checkins')); diff --git a/lib/routes/dayone/blog.js b/lib/routes/dayone/blog.js new file mode 100644 index 000000000..2b0c7ee87 --- /dev/null +++ b/lib/routes/dayone/blog.js @@ -0,0 +1,57 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const rootUrl = 'https://dayoneapp.com'; + const currentUrl = `${rootUrl}/blog`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('h3 a') + .slice(0, 20) + .map((_, item) => { + item = $(item); + const info = item.parent().next().text().split(' by '); + + return { + author: info[1], + title: item.text(), + link: `${rootUrl}${item.attr('href')}`, + pubDate: new Date(info[0]).toUTCString(), + }; + }) + .get(); + + 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); + + const description = content('.blog-layout'); + + description.children().last().remove(); + content('h1').next().remove(); + content('h1').remove(); + + item.description = description.html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +};