添加 机核网 RSS (#358)

* 添加 机核网 RSS

* 机核网 RSS 改用 Promise.all 请求
This commit is contained in:
MoguCloud 2018-07-13 15:02:59 +08:00 committed by DIYgod
parent 158a31d23c
commit d11bb2905b
4 changed files with 99 additions and 0 deletions

View File

@ -175,6 +175,8 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇
- 分区帖子
- 科技星球
- 首页
- 机核网
- 分类
## 鸣谢

View File

@ -1447,3 +1447,17 @@ id, 专辑 id, 可在对应专辑页面的 URL 中找到
| 全部 | 学院通知 | 人事通知 | 教务通知 | 学工通知 | 科研通知 | 财务通知 | 工会通知 | 院友通知 |
| ---- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- |
| 0 | 1 | 2 | 6 | 8 | 7 | 5 | 3 | 4 |
## 机核网
### 分类
举例: [https://rsshub.app/gcores/category/1](https://rsshub.app/gcores/category/1)
路由: `/gcores/category/:category`
参数: category分类名
| 文章 | 新闻 | 电台 |
| ---- | ---- | ---- |
| 1 | 2 | 9 |

View File

@ -338,4 +338,7 @@ router.get('/kejixingqiu/home', require('./routes/kejixingqiu/home'));
// PKUEECS
router.get('/pku/eecs/:type?', require('./routes/pku/eecs'));
// 机核
router.get('/gcores/category/:category', require('./routes/gcores/category'));
module.exports = router;

80
routes/gcores/category.js Normal file
View File

@ -0,0 +1,80 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
const config = require('../../config');
const sourceTimezoneOffset = -8;
module.exports = async (ctx) => {
const category = ctx.params.category;
const url = `https://www.g-cores.com/categories/${category}/originals`;
const res = await axios({
method: 'get',
url: url,
headers: {
'User-Agent': config.ua,
},
});
const data = res.data;
let $ = cheerio.load(data);
const list = $('.row .showcase');
const count = [];
const feedTitle = $('title')
.text()
.split('_')[0];
for (let i = 0; i < Math.min(list.length, 10); i++) {
count.push(i);
}
const out = await Promise.all(
count.map(async (i) => {
const item = list[i];
const itemUrl = $(item)
.find('h4 a')
.attr('href');
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const title = $(item)
.find('h4 a')
.text();
let itemRes;
try {
itemRes = await axios({
method: 'get',
url: itemUrl,
headers: {
'User-Agent': config.ua,
},
});
} catch (e) {
return Promise.resolve();
}
const itemPage = itemRes.data;
$ = cheerio.load(itemPage);
const content = $('.story').html();
const pageInfo = $('.story_info').text();
let regex = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}/;
let regRes = regex.exec(pageInfo);
if (!regRes) {
regex = /\d{4}-\d{2}-\d{2}/;
regRes = regex.exec(pageInfo);
}
const time = regRes === null ? new Date() : new Date(regRes[0]);
time.setTime(time.getTime() + (sourceTimezoneOffset - time.getTimezoneOffset() / 60) * 60 * 60 * 1000);
const single = {
title: title,
description: content,
pubDate: time.toUTCString(),
link: itemUrl,
guid: itemUrl,
};
ctx.cache.set(itemUrl, JSON.stringify(single), 24 * 60 * 60);
return Promise.resolve(single);
})
);
ctx.state.data = {
title: feedTitle,
link: url,
item: out,
};
};