From ef8ccc9d82a71d6bac95bc12361bd274782d95b8 Mon Sep 17 00:00:00 2001 From: fengkx Date: Sat, 28 Jul 2018 11:52:03 +0800 Subject: [PATCH] National Geographic RSS support (#389) * National Geographic RSS * fix format warning * docs * full text by default && correct docs * format fix ci check * correct docs --- .gitignore | 1 + README.md | 8 +++---- docs/README.md | 16 +++++++++++++ router.js | 3 +++ routes/natgeo/natgeo.js | 50 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 routes/natgeo/natgeo.js diff --git a/.gitignore b/.gitignore index 98d3d47cc9..44e1c666da 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ error.log combined.log package-lock.json .vscode +.idea docs/.vuepress/dist config/app.json diff --git a/README.md b/README.md index c84d03dcfb..e6c9010cde 100644 --- a/README.md +++ b/README.md @@ -180,11 +180,11 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇 - 公告通知 - 机核网 - 分类 +- 国家地理 National Geographic + - 分类 - ONE · 一个 -- Firefox - - Release note - - +- Firefox - Release note + ## 鸣谢 diff --git a/docs/README.md b/docs/README.md index f0acce8d6b..48aca3b8db 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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) diff --git a/router.js b/router.js index 0b821642c6..43d311f767 100644 --- a/router.js +++ b/router.js @@ -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')); diff --git a/routes/natgeo/natgeo.js b/routes/natgeo/natgeo.js new file mode 100644 index 0000000000..190bcaeffa --- /dev/null +++ b/routes/natgeo/natgeo.js @@ -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, + }; +};