From 8f91cd763eb797bbd920af2b8605e00e1ce0d236 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Fri, 22 May 2020 16:18:51 +0800 Subject: [PATCH] feat: add monotype featured article (#4836) --- docs/design.md | 6 ++++++ docs/university.md | 1 - lib/router.js | 3 +++ lib/routes/monotype/article.js | 39 ++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 lib/routes/monotype/article.js diff --git a/docs/design.md b/docs/design.md index af3f4b05d..ab2c22cff 100755 --- a/docs/design.md +++ b/docs/design.md @@ -67,6 +67,12 @@ pageClass: routes +## Monotype + +### Featured Article + + + ## Sun Creature ### Works diff --git a/docs/university.md b/docs/university.md index 7a29d7a45..3415a5665 100644 --- a/docs/university.md +++ b/docs/university.md @@ -38,7 +38,6 @@ pageClass: routes - ## 安徽农业大学 ### 计算机学院 diff --git a/lib/router.js b/lib/router.js index a782f3e73..4d82214b3 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2742,4 +2742,7 @@ router.get('/aljazeera/news', require('./routes/aljazeera/news')); router.get('/pbc/goutongjiaoliu', require('./routes/pbc/goutongjiaoliu')); router.get('/pbc/tradeAnnouncement', require('./routes/pbc/tradeAnnouncement')); +// Monotype +router.get('/monotype/article', require('./routes/monotype/article')); + module.exports = router; diff --git a/lib/routes/monotype/article.js b/lib/routes/monotype/article.js new file mode 100644 index 000000000..02ac2943a --- /dev/null +++ b/lib/routes/monotype/article.js @@ -0,0 +1,39 @@ +const url = require('url'); +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const rootUrl = 'https://www.monotype.com/resources'; + + const response = await got({ + method: 'get', + url: rootUrl, + }); + const $ = cheerio.load(response.data); + const list = $('h3.component-title') + .map((_, item) => { + item = $(item); + return { + title: item.find('a').text(), + link: url.resolve(rootUrl, item.find('a').attr('href')), + }; + }) + .get(); + + ctx.state.data = { + title: 'Monotype - Feature Articles', + link: rootUrl, + item: await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const res = await got({ method: 'get', url: item.link }); + const content = cheerio.load(res.data); + item.pubDate = new Date(content('meta[itemprop="acquia_lift:published_date"]').attr('content') * 1000).toUTCString(); + item.description = content('div.left-region').html(); + return item; + }) + ) + ), + }; +};