From 164958d0dcf4a6da2a6fdf89af7c941abf88b1fa Mon Sep 17 00:00:00 2001 From: daijinru Date: Sun, 7 Jul 2019 20:30:22 +0800 Subject: [PATCH] feat: add docschina weekly (#2569) --- docs/programming.md | 6 ++++ lib/router.js | 3 ++ lib/routes/docschina/jsweekly.js | 51 ++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 lib/routes/docschina/jsweekly.js diff --git a/docs/programming.md b/docs/programming.md index 519a7feb6..c1b814cdb 100644 --- a/docs/programming.md +++ b/docs/programming.md @@ -434,6 +434,12 @@ GitHub 官方也提供了一些 RSS: +## 印记中文周刊 + +### 最新一期 + + + ## 知晓程序 ### 文章 diff --git a/lib/router.js b/lib/router.js index f35d91d3f..6bd5d85fd 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1482,4 +1482,7 @@ router.get('/archdaily', require('./routes/archdaily/home')); // aptonic Dropzone actions router.get('/aptonic/action', require('./routes/aptonic/action')); +// 印记中文周刊 +router.get('/docschina/jsweekly', require('./routes/docschina/jsweekly')); + module.exports = router; diff --git a/lib/routes/docschina/jsweekly.js b/lib/routes/docschina/jsweekly.js new file mode 100644 index 000000000..32c10c4d7 --- /dev/null +++ b/lib/routes/docschina/jsweekly.js @@ -0,0 +1,51 @@ +const got = require('../../utils/got.js'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const baseURL = 'https://weekly.docschina.org'; + const res = await got({ + method: 'get', + url: baseURL + '/javascript', + headers: { + Referer: baseURL, + }, + }); + + const $ = cheerio.load(res.data); + + const link = $('.sidebar-link') + .eq(1) + .attr('href'); + const title = $('.site-name').text(); + const description = $('a[href="https://javascriptweekly.com/"]') + .parent() + .text(); + + const articles = await got({ + method: 'get', + url: baseURL + link, + headers: { + Referer: baseURL, + }, + }); + + const $2 = cheerio.load(articles.data); + const articlesList = $2('.page .content').children(); + const items = articlesList + .map((item, element) => { + const $article = cheerio.load(element); + return { + title: $article('h3>a[rel="noopener noreferrer"]').text(), + description: $article('p').text(), + link: $article('h3>a[rel="noopener noreferrer"]').attr('href'), + }; + }) + .get() + .filter((item) => item.title !== ''); + ctx.state.data = { + title, + link: baseURL, + description, + item: items.splice(0), + }; +};