From 3796548afbb74b09ebf3e7c5e005d1e33c1b416e Mon Sep 17 00:00:00 2001 From: Jianing Wang Date: Mon, 15 Oct 2018 16:17:30 +0800 Subject: [PATCH] add: 99% Invisible Transcript (#896) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 99% Invisible 的对话记录,方便查看听力原文 --- docs/README.md | 4 +++ router.js | 3 ++ routes/99percentinvisible/transcript.js | 47 +++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 routes/99percentinvisible/transcript.js diff --git a/docs/README.md b/docs/README.md index faf2efb5b..2873a2478 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1890,3 +1890,7 @@ IATA 国际航空运输协会机场代码, 参见[维基百科 国际航空运 + +### 99% Invisible + + diff --git a/router.js b/router.js index 83df06ea3..54a67c28d 100644 --- a/router.js +++ b/router.js @@ -646,4 +646,7 @@ router.get('/houxu/:type/:id', require('./routes/houxu/houxu')); router.get('/laosiji/hot', require('./routes/laosiji/hot')); router.get('/laosiji/feed', require('./routes/laosiji/feed')); +// 99% Invisible +router.get('/99percentinvisible/transcript', require('./routes/99percentinvisible/transcript')); + module.exports = router; diff --git a/routes/99percentinvisible/transcript.js b/routes/99percentinvisible/transcript.js new file mode 100644 index 000000000..a41001b85 --- /dev/null +++ b/routes/99percentinvisible/transcript.js @@ -0,0 +1,47 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const url = require('url'); + +const host = 'https://99percentinvisible.org/'; + +module.exports = async (ctx) => { + const link = url.resolve(host, 'archives/'); + const response = await axios.get(link); + + const $ = cheerio.load(response.data); + + const list = $('.content .post-blocks .post-actions a.transcript') + .map((i, e) => $(e).attr('href')) + .get(); + + const out = await Promise.all( + list.filter((e) => e.startsWith('/episode')).map(async (itemUrl) => { + itemUrl = url.resolve(host, itemUrl); + const cache = await ctx.cache.get(itemUrl); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const response = await axios.get(itemUrl); + const $ = cheerio.load(response.data); + + const single = { + title: $('article header h1') + .text() + .trim(), + link: itemUrl, + author: '99% Invisible', + description: $('article .transcript-content').html(), + pubDate: new Date($('article header .entry-meta time').text()), + }; + ctx.cache.set(itemUrl, JSON.stringify(single), 24 * 60 * 60); + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: '99% Invisible Transcript', + link, + item: out, + }; +};