From 9bc04019625de18aa307ce9fd1864e4ead1b671c Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Thu, 12 Aug 2021 16:14:44 +0800 Subject: [PATCH] feat(route): add World Happiness Report (#7223) Co-authored-by: NeverBehave --- docs/en/new-media.md | 10 ++++++ docs/new-media.md | 10 ++++++ lib/router.js | 3 ++ lib/routes/worldhappiness/archive.js | 50 ++++++++++++++++++++++++++++ lib/routes/worldhappiness/blog.js | 48 ++++++++++++++++++++++++++ 5 files changed, 121 insertions(+) create mode 100644 lib/routes/worldhappiness/archive.js create mode 100644 lib/routes/worldhappiness/blog.js diff --git a/docs/en/new-media.md b/docs/en/new-media.md index 0e7708469..75e17d953 100644 --- a/docs/en/new-media.md +++ b/docs/en/new-media.md @@ -412,6 +412,16 @@ Provides all of the Thrillist articles with the specified tag. +## World Happiness + +### Blog + + + +### Archive + + + ## World Health Organization | WHO ### Newsroom diff --git a/docs/new-media.md b/docs/new-media.md index 3113333b2..3d9468cdb 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -679,6 +679,16 @@ Supported sub-sites: +## World Happiness + +### Blog + + + +### Archive + + + ## ZAKER ### source diff --git a/lib/router.js b/lib/router.js index ded22da6a..dae6604b7 100644 --- a/lib/router.js +++ b/lib/router.js @@ -4158,6 +4158,9 @@ router.get('/tanchinese/:category?', require('./routes/tanchinese')); // Harvard router.get('/harvard/health/blog', require('./routes/universities/harvard/health/blog')); +// World Happiness Report +router.get('/worldhappiness/blog', require('./routes/worldhappiness/blog')); +router.get('/worldhappiness/archive', require('./routes/worldhappiness/archive')); // 中国纺织经济信息网 router.get('/ctei/news/:id?', require('./routes/ctei/news')); // 时事一点通 diff --git a/lib/routes/worldhappiness/archive.js b/lib/routes/worldhappiness/archive.js new file mode 100644 index 000000000..19b1c2f20 --- /dev/null +++ b/lib/routes/worldhappiness/archive.js @@ -0,0 +1,50 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const rootUrl = 'https://worldhappiness.report'; + const currentUrl = `${rootUrl}/archive`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('.archived-report a') + .map((_, item) => { + item = $(item); + return { + title: item.text(), + link: `${rootUrl}${item.attr('href')}`, + pubDate: parseDate(item.text().split(' ').pop(), 'YYYY'), + }; + }) + .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); + + content('.report-title').remove(); + + item.description = content('.report-meta').html() + content('.report-body').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +}; diff --git a/lib/routes/worldhappiness/blog.js b/lib/routes/worldhappiness/blog.js new file mode 100644 index 000000000..35bac50e6 --- /dev/null +++ b/lib/routes/worldhappiness/blog.js @@ -0,0 +1,48 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const rootUrl = 'https://worldhappiness.report'; + const currentUrl = `${rootUrl}/blog`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('.post-link') + .map((_, item) => { + item = $(item); + return { + title: item.children('h3').text(), + link: `${rootUrl}${item.attr('href')}`, + pubDate: parseDate(item.children('.post-date').attr('datetime')), + }; + }) + .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); + + item.description = content('.post-body').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +};