add leetcode articles (#1574)

This commit is contained in:
Chenyang Shi 2019-02-19 12:00:19 +08:00 committed by DIYgod
parent f091d80ea3
commit 6f0a80fcd4
3 changed files with 72 additions and 0 deletions

View File

@ -822,6 +822,10 @@ GitHub 官方也提供了一些 RSS:
<route name="Patch Comments" author="ysc3839" example="/patchwork.kernel.org/comments/10723629" path="/patchwork.kernel.org/comments/:id" :paramsDesc="['Patch ID']"/>
### LeetCode
<route name="文章" author="LogicJake" example="/leetcode/articles" path="/leetcode/articles"/>
### segmentfault
<route name="频道" author="LogicJake" example="/segmentfault/channel/frontend" path="/segmentfault/channel/:name" :paramsDesc="['频道名称,在频道 URL 可以找到']"/>

View File

@ -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'));

View File

@ -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,
};
};