feat(route): add 科学网用户博客 (#9525)

* feat(route): add 科学网用户博客

* update lib/v2/sciencenet/user.js

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

Co-authored-by: Tony <TonyRL@users.noreply.github.com>
This commit is contained in:
Ethan Shen 2022-04-12 11:56:50 +08:00 committed by GitHub
parent 6c7988c481
commit 55ecc71784
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 115 additions and 14 deletions

View File

@ -2235,7 +2235,7 @@ column 为 third 时可选的 category:
## 科学网
### 博客
### 精选博客
<Route author="nczitzk" example="/sciencenet/blog" path="/sciencenet/blog/:type?/:time?/:sort?" :paramsDesc="['类型,见下表,默认为推荐', '时间,见下表,默认为所有时间', '排序,见下表,默认为按发表时间排序']">
@ -2259,6 +2259,10 @@ column 为 third 时可选的 category:
</Route>
### 用户博客
<Route author="nczitzk" example="/sciencenet/user/tony8310" path="/sciencenet/user/:id" :paramsDesc="['用户 id可在对用户博客页 URL 中找到']"/>
## 快科技
### 新闻

View File

@ -3655,7 +3655,7 @@ router.get('/yicas/blog', lazyloadRouteHandler('./routes/yicas/blog'));
router.get('/93/:category?', lazyloadRouteHandler('./routes/93/index'));
// 科学网
router.get('/sciencenet/blog/:type?/:time?/:sort?', lazyloadRouteHandler('./routes/sciencenet/blog'));
// router.get('/sciencenet/blog/:type?/:time?/:sort?', lazyloadRouteHandler('./routes/sciencenet/blog'));
// DailyArt
router.get('/dailyart/:language?', lazyloadRouteHandler('./routes/dailyart/index'));

View File

@ -1,14 +1,17 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const type = ctx.params.type || 'recommend';
const time = ctx.params.time || '5';
const sort = ctx.params.sort || '1';
const type = ctx.params.type ?? 'recommend';
const time = ctx.params.time ?? '5';
const sort = ctx.params.sort ?? '1';
const rootUrl = 'http://blog.sciencenet.cn';
const currentUrl = `${rootUrl}/blog.php?mod=${type}&type=list&op=${time}&ord=${sort}`;
const response = await got({
method: 'get',
url: currentUrl,
@ -17,31 +20,33 @@ module.exports = async (ctx) => {
const $ = cheerio.load(iconv.decode(response.data, 'gbk'));
const list = $('tr td a[title]')
.slice(0, 15)
.map((_, item) => {
let items = $('tr td a[title]')
.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 50)
.toArray()
.map((item) => {
item = $(item);
return {
title: item.text(),
link: `${rootUrl}/${item.attr('href')}`,
pubDate: new Date(item.next().text()).toUTCString(),
};
})
.get();
});
const items = await Promise.all(
list.map((item) =>
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
responseType: 'buffer',
});
const content = cheerio.load(iconv.decode(detailResponse.data, 'gbk'));
item.author = content('.xs2').text();
item.description = content('#blog_article').html();
item.pubDate = new Date(content('.xg1').eq(5).text()).toUTCString();
item.pubDate = timezone(parseDate(content('.xg1').eq(5).text()), +8);
return item;
})
@ -49,7 +54,7 @@ module.exports = async (ctx) => {
);
ctx.state.data = {
title: $('title').text(),
title: '科学网 - 精选博文',
link: currentUrl,
item: items,
};

View File

@ -0,0 +1,4 @@
module.exports = {
'/blog/:type?/:time?/:sort?': ['nczitzk'],
'/user/:id': ['nczitzk'],
};

View File

@ -0,0 +1,19 @@
module.exports = {
'sciencenet.cn': {
_name: '科学网',
blog: [
{
title: '精选博客',
docs: 'https://docs.rsshub.app/new-media.html#ke-xue-wang-jing-xuan-bo-ke',
source: ['/blog.php', '/'],
target: (params, url) => `/sciencenet/blog/${new URL(url).searchParams.get('mod')}/${new URL(url).searchParams.get('op')}/${new URL(url).searchParams.get('ord')}`,
},
{
title: '用户博客',
docs: 'https://docs.rsshub.app/new-media.html#ke-xue-wang-jing-xuan-bo-ke',
source: ['/u/:id', '/'],
target: '/sciencenet/user/:id',
},
],
},
};

View File

@ -0,0 +1,4 @@
module.exports = function (router) {
router.get('/blog/:type?/:time?/:sort?', require('./blog'));
router.get('/user/:id', require('./user'));
};

65
lib/v2/sciencenet/user.js Normal file
View File

@ -0,0 +1,65 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const id = ctx.params.id;
const rootUrl = 'https://blog.sciencenet.cn';
const currentUrl = `${rootUrl}/u/${id}`;
let response = await got({
method: 'get',
url: currentUrl,
responseType: 'buffer',
});
let $ = cheerio.load(iconv.decode(response.data, 'gbk'));
response = await got({
method: 'get',
url: $('.xg1 a').eq(1).attr('href'),
});
$ = cheerio.load(response.data);
let items = $('item')
.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 50)
.toArray()
.map((item) => {
item = $(item);
return {
title: item.find('title').text(),
link: item.find('guid').text(),
};
});
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
responseType: 'buffer',
});
const content = cheerio.load(iconv.decode(detailResponse.data, 'gbk'));
item.author = content('.xs2').text();
item.description = content('#blog_article').html();
item.pubDate = timezone(parseDate(content('.xg1').eq(5).text()), +8);
return item;
})
)
);
ctx.state.data = {
title: `科学网 - ${items[0].author}的博文`,
link: currentUrl,
item: items,
};
};