diff --git a/docs/en/new-media.md b/docs/en/new-media.md index 0e7708469c..75e17d9530 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 3113333b2e..3d9468cdb5 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 ded22da6a3..dae6604b7a 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 0000000000..19b1c2f206 --- /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 0000000000..35bac50e6c --- /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, + }; +};