Skip to content

Commit

Permalink
DIYgod#289 Hexo Next theme RSS support (DIYgod#395)
Browse files Browse the repository at this point in the history
* DIYgod#289 Hexo Next theme RSS support

* docs

* better title & description

* parese 5 article by default
  • Loading branch information
fengkx authored and DIYgod committed Jul 29, 2018
1 parent ddc7ff1 commit a0475c1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇
- Release note
- 推酷
- 周刊
- Hexo
- Next 主题
- 小米
- 众筹

Expand Down
10 changes: 10 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 不带协议头

## 小米

### 众筹
Expand Down
3 changes: 3 additions & 0 deletions router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down
53 changes: 53 additions & 0 deletions routes/hexo/next.js
Original file line number Diff line number Diff line change
@@ -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,
};
};

0 comments on commit a0475c1

Please sign in to comment.