Skip to content

Commit

Permalink
National Geographic RSS support (DIYgod#389)
Browse files Browse the repository at this point in the history
* National Geographic RSS

* fix format warning

* docs

* full text by default && correct docs

* format fix ci check

* correct docs
  • Loading branch information
fengkx authored and DIYgod committed Jul 28, 2018
1 parent 2a289ed commit ef8ccc9
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ error.log
combined.log
package-lock.json
.vscode
.idea
docs/.vuepress/dist

config/app.json
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇
- 公告通知
- 机核网
- 分类
- 国家地理 National Geographic
- 分类
- ONE · 一个
- Firefox
- Release note

</details>
- Firefox - Release note
</details>

## 鸣谢

Expand Down
16 changes: 16 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1490,6 +1490,22 @@ id, 专辑 id, 可在对应专辑页面的 URL 中找到
| ---- | ---- | ---- |
| 1 | 2 | 9 |

## 国家地理 National Geographic

### 分类

举例:

[https://rsshub.app/natgeo/travel](https://rsshub.app/natgeo/travel)

[https://rsshub.app/natgeo/news/ngnews](https://rsshub.app/natgeo/news/ngnews)

路由: `/natgeo/:cat/:type?/:option?`

参数: cat, 分类; type, 类型

可在 url 中获取,例如`https://www.natgeomedia.com/category/news/ngnews`对应 cat, type 分别为 news, ngnews

## ONE · 一个

举例: [https://rsshub.app/one](https://rsshub.app/one)
Expand Down
3 changes: 3 additions & 0 deletions router.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ router.get('/pku/eecs/:type?', require('./routes/pku/eecs'));
// 机核
router.get('/gcores/category/:category', require('./routes/gcores/category'));

// 国家地理 National Geographic
router.get('/natgeo/:cat/:type?', require('./routes/natgeo/natgeo'));

// 一个
router.get('/one', require('./routes/one/index'));

Expand Down
50 changes: 50 additions & 0 deletions routes/natgeo/natgeo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const cheerio = require('cheerio');
const config = require('../../config');
const axios = require('../../utils/axios');

const axios_ins = axios.create({
headers: {
'User-Agent': config.ua,
Reference: 'https://www.natgeomedia.com',
},
});

module.exports = async (ctx) => {
const type = `${ctx.params.type ? ctx.params.type : ''}`;
const url = `https://www.natgeomedia.com/category/${ctx.params.cat}/${type}`;
const res = await axios_ins.get(url);
const data = res.data;
const $ = cheerio.load(data);

const list = $('.td-ss-main-content').find('.td-animation-stack');

const out = [];
for (let i = 0; i < list.length; i++) {
const each = $(list[i]);
const storyLink = each.find('a[itemprop=url]').attr('href');
const item = {
title: each.find('a[itemprop=url]').attr('title'),
pubDate: each.find('time').attr('datetime'),
link: storyLink,
guid: storyLink.match(/\d+/g)[0],
};
const key = `${ctx.params.cat}${type}${item.guid}`;
const value = await ctx.cache.get(key);
if (value) {
item.description = value;
} else {
// 获取全文
const storyDetail = await axios_ins.get(item.link);
const data = storyDetail.data;
const $ = cheerio.load(data);
item.description = $('.td-post-content').html();
ctx.cache.set(key, item.description, 12 * 60 * 60);
}
out.push(item);
}
ctx.state.data = {
title: $('title').text(),
link: url,
item: out,
};
};

0 comments on commit ef8ccc9

Please sign in to comment.