add 牛客网讨论区 (#1539)

This commit is contained in:
Chenyang Shi
2019-02-19 11:25:01 +08:00
committed by DIYgod
parent f14a24558e
commit 02e8d64b8b
3 changed files with 85 additions and 0 deletions

View File

@@ -816,6 +816,16 @@ GitHub 官方也提供了一些 RSS:
<route name="Patch Comments" author="ysc3839" example="/patchwork.kernel.org/comments/10723629" path="/patchwork.kernel.org/comments/:id" :paramsDesc="['Patch ID']"/> <route name="Patch Comments" author="ysc3839" example="/patchwork.kernel.org/comments/10723629" path="/patchwork.kernel.org/comments/:id" :paramsDesc="['Patch ID']"/>
### 牛客网
<route name="讨论区" author="LogicJake" example="/nowcoder/discuss/2/4" path="/nowcoder/discuss/:type/:order" :paramsDesc="['讨论区分区id 在 URL 中可以找到', '排序方式']">
| 最新回复 | 最新发表 | 最新 | 精华 |
| -------- | -------- | ---- | ---- |
| 0 | 3 | 1 | 4 |
</router>
## 直播 ## 直播
### 哔哩哔哩直播 ### 哔哩哔哩直播

View File

@@ -1037,4 +1037,7 @@ router.get('/gaoqing/latest', require('./routes/gaoqing/latest'));
// 轻小说文库 // 轻小说文库
router.get('/wenku8/chapter/:id', require('./routes/wenku8/chapter')); router.get('/wenku8/chapter/:id', require('./routes/wenku8/chapter'));
// 牛客网
router.get('/nowcoder/discuss/:type/:order', require('./routes/nowcoder/discuss'));
module.exports = router; module.exports = router;

View File

@@ -0,0 +1,72 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
const url = require('url');
const date = require('../../utils/date');
const host = 'https://www.nowcoder.com';
module.exports = async (ctx) => {
const type = ctx.params.type;
const order = ctx.params.order;
const link = `https://www.nowcoder.com/discuss?type=${type}&order=${order}`;
const response = await axios.get(link);
const $ = cheerio.load(response.data);
const type_name = $('a.discuss-tab.selected').text();
const order_name = $('li.selected a').text();
const list = $('li.clearfix')
.map(function() {
const info = {
title: $(this)
.find('div.discuss-main.clearfix a')
.text()
.trim()
.replace('\n', ' '),
link: $(this)
.find('div.discuss-main.clearfix a')
.attr('href'),
};
return info;
})
.get();
const out = await Promise.all(
list.map(async (info) => {
const title = info.title || 'tzgg';
const itemUrl = url.resolve(host, info.link);
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await axios.get(itemUrl);
const $ = cheerio.load(response.data);
const date_value = $('span.post-time')
.text()
.split(' ')[1];
const description = $('.post-topic-main')
.html()
.trim();
const single = {
title: title,
link: itemUrl,
description: description,
pubDate: date(date_value, 8),
};
ctx.cache.set(itemUrl, JSON.stringify(single), 24 * 60 * 60);
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `${type_name}${order_name}——牛客网讨论区`,
link: link,
item: out,
};
};