From a0475c17465bfe64b98dce3bcab42e8d4a1e12fe Mon Sep 17 00:00:00 2001 From: fengkx Date: Sun, 29 Jul 2018 20:49:33 +0800 Subject: [PATCH] #289 Hexo Next theme RSS support (#395) * #289 Hexo Next theme RSS support * docs * better title & description * parese 5 article by default --- README.md | 2 ++ docs/README.md | 10 +++++++++ router.js | 3 +++ routes/hexo/next.js | 53 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 routes/hexo/next.js diff --git a/README.md b/README.md index 05837dba8..6a2058ebb 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,8 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇 - Release note - 推酷 - 周刊 +- Hexo + - Next 主题 - 小米 - 众筹 diff --git a/docs/README.md b/docs/README.md index 284a22e49..43cbe535f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1548,6 +1548,16 @@ id, 专辑 id, 可在对应专辑页面的 URL 中找到 | -------- | -------- | -------- | -------- | | prog | design | startup | tech | +## Hexo + +### Next 主题 + +举例:[http://rsshub.app/hexo/next/fengkx.top](http://rsshub.app/hexo/next/fengkx.top) + +路由: `/hexo/next/:url` + +参数: url 博客 Url 不带协议头 + ## 小米 ### 众筹 diff --git a/router.js b/router.js index bd1a05f4e..04a2b8203 100755 --- a/router.js +++ b/router.js @@ -354,6 +354,9 @@ router.get('/firefox/release/:platform', require('./routes/firefox/release')); // tuicool router.get('/tuicool/mags/:type', require('./routes/tuicool/mags')); +// Hexo +router.get('/hexo/next/:url', require('./routes/hexo/next')); + // 小米 router.get('/mi/crowdfunding', require('./routes/mi/crowdfunding')); diff --git a/routes/hexo/next.js b/routes/hexo/next.js new file mode 100644 index 000000000..f7a4cecdf --- /dev/null +++ b/routes/hexo/next.js @@ -0,0 +1,53 @@ +const cheerio = require('cheerio'); +const config = require('../../config'); +const axios = require('../../utils/axios'); + +const axios_ins = axios.create({ + headers: { + 'User-Agent': config.ua, + }, +}); + +module.exports = async (ctx) => { + const url = `http://${ctx.params.url}`; + const res = await axios_ins.get(`${url}/archives`); + const data = res.data; + const $ = cheerio.load(data); + + const list = $('.post-header'); + + const count = []; + for (let i = 0; i < Math.min(list.length, 5); i++) { + count.push(i); + } + const out = await Promise.all( + count.map(async (i) => { + const each = $(list[i]); + const storyLink = each.find('.post-title-link').attr('href'); + const item = { + title: each.find('[itemprop=name]').text(), + link: encodeURI(`${url}${storyLink}`), + }; + const key = item.link; + const value = ctx.cache.get(key); + + if (value) { + item.description = value; + } else { + const storyDeatil = await axios_ins.get(item.link); + const data = storyDeatil.data; + const $ = cheerio.load(data); + item.pubDate = $('time').attr('datetime'); + item.description = $('.post-body').html(); + ctx.cache.set(key, item.descriptio, 6 * 60 * 60); + } + return Promise.resolve(item); + }) + ); + ctx.state.data = { + title: $('.site-title').text(), + link: url, + description: $('[name=description]').attr('content'), + item: out, + }; +};