From 6f0a80fcd4970b3a961136831e37384a43039935 Mon Sep 17 00:00:00 2001 From: Chenyang Shi Date: Tue, 19 Feb 2019 12:00:19 +0800 Subject: [PATCH] add leetcode articles (#1574) --- docs/README.md | 4 ++ lib/router.js | 3 ++ lib/routes/leetcode/articles.js | 65 +++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 lib/routes/leetcode/articles.js diff --git a/docs/README.md b/docs/README.md index 8e307f318..e7110af38 100644 --- a/docs/README.md +++ b/docs/README.md @@ -822,6 +822,10 @@ GitHub 官方也提供了一些 RSS: +### LeetCode + + + ### segmentfault diff --git a/lib/router.js b/lib/router.js index d3dfc3f95..60f9b15fe 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1045,6 +1045,9 @@ router.get('/gaoqing/latest', require('./routes/gaoqing/latest')); // 轻小说文库 router.get('/wenku8/chapter/:id', require('./routes/wenku8/chapter')); +// LeetCode +router.get('/leetcode/articles', require('./routes/leetcode/articles')); + // segmentfault router.get('/segmentfault/channel/:name', require('./routes/segmentfault/channel')); diff --git a/lib/routes/leetcode/articles.js b/lib/routes/leetcode/articles.js new file mode 100644 index 000000000..4a6679c4b --- /dev/null +++ b/lib/routes/leetcode/articles.js @@ -0,0 +1,65 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const url = require('url'); + +const host = 'https://leetcode.com'; + +module.exports = async (ctx) => { + const link = 'https://leetcode.com/articles/'; + const response = await axios.get(link); + const $ = cheerio.load(response.data); + + const list = $('a.list-group-item') + .slice(0, 10) + .map(function() { + const info = { + title: $(this) + .find('h4.media-heading') + .text(), + link: $(this).attr('href'), + date: $(this) + .find('p.pull-right.media-date strong') + .text(), + }; + return info; + }) + .get(); + + const out = await Promise.all( + list.map(async (info) => { + const title = info.title; + const date = info.date; + const itemUrl = url.resolve(host, info.link); + + 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 description = + $('#question-preview') + .html() + .trim() + + $('.article-body') + .html() + .trim(); + + const single = { + title: title, + link: itemUrl, + description: description, + pubDate: new Date(date).toUTCString(), + }; + ctx.cache.set(itemUrl, JSON.stringify(single), 24 * 60 * 60); + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: 'leetcode文章', + link: link, + item: out, + }; +};