diff --git a/docs/new-media.md b/docs/new-media.md index 1f4aeb567..446a6ab3a 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -2235,7 +2235,7 @@ column 为 third 时可选的 category: ## 科学网 -### 博客 +### 精选博客 @@ -2259,6 +2259,10 @@ column 为 third 时可选的 category: +### 用户博客 + + + ## 快科技 ### 新闻 diff --git a/lib/router.js b/lib/router.js index 4a312e249..abe4e0df1 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/sciencenet/blog.js b/lib/v2/sciencenet/blog.js similarity index 68% rename from lib/routes/sciencenet/blog.js rename to lib/v2/sciencenet/blog.js index d561bc13d..d2a977cad 100644 --- a/lib/routes/sciencenet/blog.js +++ b/lib/v2/sciencenet/blog.js @@ -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, }; diff --git a/lib/v2/sciencenet/maintainer.js b/lib/v2/sciencenet/maintainer.js new file mode 100644 index 000000000..848ffbff1 --- /dev/null +++ b/lib/v2/sciencenet/maintainer.js @@ -0,0 +1,4 @@ +module.exports = { + '/blog/:type?/:time?/:sort?': ['nczitzk'], + '/user/:id': ['nczitzk'], +}; diff --git a/lib/v2/sciencenet/radar.js b/lib/v2/sciencenet/radar.js new file mode 100644 index 000000000..d2b548177 --- /dev/null +++ b/lib/v2/sciencenet/radar.js @@ -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', + }, + ], + }, +}; diff --git a/lib/v2/sciencenet/router.js b/lib/v2/sciencenet/router.js new file mode 100644 index 000000000..90bc71342 --- /dev/null +++ b/lib/v2/sciencenet/router.js @@ -0,0 +1,4 @@ +module.exports = function (router) { + router.get('/blog/:type?/:time?/:sort?', require('./blog')); + router.get('/user/:id', require('./user')); +}; diff --git a/lib/v2/sciencenet/user.js b/lib/v2/sciencenet/user.js new file mode 100644 index 000000000..877fc7092 --- /dev/null +++ b/lib/v2/sciencenet/user.js @@ -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, + }; +};