From 5c70eb95f88c66e428fae4006737efb784edc3f1 Mon Sep 17 00:00:00 2001 From: Attenuation Date: Thu, 12 Dec 2019 17:58:14 +0800 Subject: [PATCH] feat: add aliyun kernel blog (#3572) --- docs/blog.md | 6 ++++ lib/router.js | 3 ++ lib/routes/aliyun-kernel/index.js | 60 +++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 lib/routes/aliyun-kernel/index.js diff --git a/docs/blog.md b/docs/blog.md index 31504db20..86ba009d1 100644 --- a/docs/blog.md +++ b/docs/blog.md @@ -32,6 +32,12 @@ pageClass: routes +## 阿里云系统组技术博客 + +### 首页 + + + ## 财新博客 ### 用户博客 diff --git a/lib/router.js b/lib/router.js index fc965309f..bb9f570ab 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2034,4 +2034,7 @@ router.get('/ikea/uk/offer', require('./routes/ikea/uk/offer')); // Mastodon router.get('/mastodon/timeline/:site/:only_media?', require('./routes/mastodon/timeline')); +// Kernel Aliyun +router.get('/aliyun-kernel/index', require('./routes/aliyun-kernel/index')); + module.exports = router; diff --git a/lib/routes/aliyun-kernel/index.js b/lib/routes/aliyun-kernel/index.js new file mode 100644 index 000000000..13297f2fd --- /dev/null +++ b/lib/routes/aliyun-kernel/index.js @@ -0,0 +1,60 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const baseUrl = 'https://kernel.taobao.org/'; + const response = await got({ + method: 'get', + url: baseUrl, + headers: { + Referer: baseUrl, + }, + }); + + const data = response.data; + const $ = cheerio.load(data); + const list = $('.container .post-item').get(); + + const ProcessFeed = (data) => { + const $ = cheerio.load(data); + + // 提取内容 + return $('.container .post-content').html(); + }; + + const items = await Promise.all( + list.map(async (item) => { + const $ = cheerio.load(item); + const $a = $('.article-title > a'); + const link = baseUrl + $a.attr('href'); + // const publish_time = $('.date-label').text(); + + const cache = await ctx.cache.get(link); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const response = await got({ + method: 'get', + url: link, + }); + + const description = ProcessFeed(response.data); + + const single = { + title: $a.text(), + description, + link: link, + // pubDate: publish_time, + }; + ctx.cache.set(link, JSON.stringify(single)); + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: 'Kernel Aliyun', + link: baseUrl, + item: items, + }; +};