feat: add oschina topic (#2570)

* feat: add oschina topic

* remove ua

* format
This commit is contained in:
Jiaqi Li
2019-07-10 14:14:22 +08:00
committed by DIYgod
parent 1c352ffc69
commit 6efdff8b76
3 changed files with 60 additions and 0 deletions

View File

@@ -367,6 +367,7 @@ GitHub 官方也提供了一些 RSS:
| xxiaobian |
</Route>
### 数字型账号用户博客
<Route author="dxmpalb" example="/oschina/u/3920392" path="/oschina/u/:id" :paramsDesc="['用户 id, 可通过查看用户博客网址得到,以 u/数字结尾,数字即为 id']">
@@ -377,6 +378,10 @@ GitHub 官方也提供了一些 RSS:
</Route>
### 问答主题
<Route author="loveely7" example="/oschina/topic/weekly-news" path="/oschina/topic/:topic" :paramsDesc="['主题名, 可从[全部主题](https://www.oschina.net/question/topics)进入主题页, 在 URL 中找到']"/>
## 看雪
### 论坛

View File

@@ -420,6 +420,7 @@ router.get('/linkedkeeper/:type/:id?', require('./routes/linkedkeeper/index'));
router.get('/oschina/news/:category?', require('./routes/oschina/news'));
router.get('/oschina/user/:id', require('./routes/oschina/user'));
router.get('/oschina/u/:id', require('./routes/oschina/u'));
router.get('/oschina/topic/:topic', require('./routes/oschina/topic'));
// 安全客
router.get('/aqk/vul', require('./routes/aqk/vul'));

View File

@@ -0,0 +1,54 @@
const url = require('url');
const got = require('@/utils/got');
const date = require('@/utils/date');
const cheerio = require('cheerio');
async function load(link) {
const res = await got({
method: 'get',
url: link,
});
const content = cheerio.load(res.data);
content('.ad-wrap').remove();
return content;
}
module.exports = async (ctx) => {
const topic = ctx.params.topic;
const topicUrl = `https://www.oschina.net/question/topic/${topic}?show=time`;
const $ = await load(topicUrl);
const topicName = $('.topic-info > .topic-header > h3').text();
const list = $('#questionList').find('.question-item');
const count = [];
for (let i = 0; i < Math.min(list.length, 10); i++) {
count.push(i);
}
const resultItem = await Promise.all(
count.map(async (i) => {
const each = $(list[i]);
const originalUrl = each.find('.header').attr('href');
const item = {
title: each.find('.header').text(),
link: url.resolve('https://www.oschina.net', encodeURI(originalUrl)),
author: date(each.find('.extra > .list > .item:nth-of-type(1)').text()),
pubDate: date(each.find('.extra > .list > .item:nth-of-type(2)').text()),
};
item.description = await ctx.cache.tryGet(originalUrl, async () => {
const content = await load(item.link);
return content('#articleContent').html();
});
return Promise.resolve(item);
})
);
ctx.state.data = {
title: `开源中国-${topicName}`,
link: topicUrl,
item: resultItem,
};
};