add: OSChina blog (#1807)

增加开源中国用户博客订阅,增加开源中国新闻rss的作者元素,便于参数过滤。
This commit is contained in:
dxmpalb
2019-03-22 23:46:57 +08:00
committed by DIYgod
parent bbe99c559c
commit a55406df34
5 changed files with 133 additions and 0 deletions

View File

@@ -767,6 +767,20 @@ GitHub 官方也提供了一些 RSS:
### 开源中国
<route name="资讯" author="tgly307" example="/oschina/news" path="/oschina/news"/>
<route name="用户博客" author="dxmpalb" example="/oschina/user/xxiaobian" path="/oschina/user/:id" :paramsDesc="['用户 id, 可通过查看用户博客网址得到,如果博客以 u/数字结尾,使用下一条路由']">
| 小小编辑 |
| --------- |
| xxiaobian |
</route>
<route name="数字型账号用户博客" author="dxmpalb" example="/oschina/u/3920392" path="/oschina/u/:id" :paramsDesc="['用户 id, 可通过查看用户博客网址得到,以 u/数字结尾,数字即为 id']">
| EAWorld 的博客 |
| -------------- |
| 3920392 |
</route>
### GitLab

View File

@@ -485,6 +485,8 @@ router.get('/linkedkeeper/:type/:id?', require('./routes/linkedkeeper/index'));
// 开源中国
router.get('/oschina/news', require('./routes/oschina/news'));
router.get('/oschina/user/:id', require('./routes/oschina/user'));
router.get('/oschina/u/:id', require('./routes/oschina/u'));
// 安全客
router.get('/aqk/vul', require('./routes/aqk/vul'));

View File

@@ -41,6 +41,9 @@ module.exports = async (ctx) => {
const content = cheerio.load(detail.data);
content('.ad-wrap').remove();
item.description = content('#articleContent').html();
item.author = content('a.__user')
.find('span')
.text();
ctx.cache.set(key, item.description, 24 * 60 * 60);
}
}

57
lib/routes/oschina/u.js Normal file
View File

@@ -0,0 +1,57 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const id = ctx.params.id;
const res = await axios({
method: 'get',
url: `https://my.oschina.net/u/${id}`,
headers: {
Referer: `https://my.oschina.net/u/${id}`,
},
});
const $ = cheerio.load(res.data);
$('div[data-tooltip]').remove();
const author = $('[data-user-name]').attr('data-user-name');
const list = $('#newestBlogList').find('.blog-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('a.header', '.content').attr('href');
const item = {
title: each.find('a.header', '.content').text(),
description: each.find('p', '.description').text(),
link: encodeURI(originalUrl),
};
const key = 'oschina' + item.link;
const value = await ctx.cache.get(key);
if (value) {
item.description = value;
} else {
const detail = await axios({
method: 'get',
url: item.link,
headers: {
Referer: item.link,
},
});
const content = cheerio.load(detail.data);
content('.ad-wrap').remove();
item.description = content('#articleContent').html();
ctx.cache.set(key, item.description, 24 * 60 * 60);
}
return Promise.resolve(item);
})
);
ctx.state.data = {
title: author + '的博客',
link: `https://my.oschina.net/u/${id}`,
item: resultItem,
};
};

View File

@@ -0,0 +1,57 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const id = ctx.params.id;
const res = await axios({
method: 'get',
url: `https://my.oschina.net/${id}`,
headers: {
Referer: `https://my.oschina.net/${id}`,
},
});
const $ = cheerio.load(res.data);
$('div[data-tooltip]').remove();
const author = $('[data-user-name]').attr('data-user-name');
const list = $('#newestBlogList').find('.blog-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('a.header', '.content').attr('href');
const item = {
title: each.find('a.header', '.content').text(),
description: each.find('p', '.description').text(),
link: encodeURI(originalUrl),
};
const key = 'oschina' + item.link;
const value = await ctx.cache.get(key);
if (value) {
item.description = value;
} else {
const detail = await axios({
method: 'get',
url: item.link,
headers: {
Referer: item.link,
},
});
const content = cheerio.load(detail.data);
content('.ad-wrap').remove();
item.description = content('#articleContent').html();
ctx.cache.set(key, item.description, 24 * 60 * 60);
}
return Promise.resolve(item);
})
);
ctx.state.data = {
title: author + '的博客',
link: `https://my.oschina.net/${id}`,
item: resultItem,
};
};