From 350dfa45358b496f807e6732d9ce509fa48fedfc Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 9 Jan 2023 03:22:07 +0800 Subject: [PATCH] feat(route): hub.baai (#11580) * feat(route): hub.baai * docs: fix table --- docs/programming.md | 28 ++++++++++++++++++ lib/v2/baai/comments.js | 35 +++++++++++++++++++++++ lib/v2/baai/events.js | 35 +++++++++++++++++++++++ lib/v2/baai/hub.js | 60 +++++++++++++++++++++++++++++++++++++++ lib/v2/baai/maintainer.js | 5 ++++ lib/v2/baai/radar.js | 31 ++++++++++++++++++++ lib/v2/baai/router.js | 7 +++++ lib/v2/baai/utils.js | 33 +++++++++++++++++++++ 8 files changed, 234 insertions(+) create mode 100644 lib/v2/baai/comments.js create mode 100644 lib/v2/baai/events.js create mode 100644 lib/v2/baai/hub.js create mode 100644 lib/v2/baai/maintainer.js create mode 100644 lib/v2/baai/radar.js create mode 100644 lib/v2/baai/router.js create mode 100644 lib/v2/baai/utils.js diff --git a/docs/programming.md b/docs/programming.md index 1481dd168..f5795ad4d 100644 --- a/docs/programming.md +++ b/docs/programming.md @@ -781,6 +781,34 @@ GitHub 官方也提供了一些 RSS: +## 北京智源人工智能研究院 + +### 智源社区 + + + +分类 + +| 最热 | 最新 | 头条 | +| --- | --- | ------- | +| hot | new | readCnt | + +时间跨度 + +| 3 天 | 7 天 | 30 天 | +| --- | --- | ---- | +| 3 | 7 | 30 | + + + +### 智源社区 - 活动 + + + +### 智源社区 - 评论 + + + ## 饿了么开放平台 ### 商家开放平台公告 diff --git a/lib/v2/baai/comments.js b/lib/v2/baai/comments.js new file mode 100644 index 000000000..60b333b57 --- /dev/null +++ b/lib/v2/baai/comments.js @@ -0,0 +1,35 @@ +const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); + +const { baseUrl, apiHost } = require('./utils'); + +module.exports = async (ctx) => { + const responses = await got.all( + Array.from( + { + // first 2 pages + length: (ctx.query.limit ? parseInt(ctx.query.limit, 10) : 12) / 6, + }, + (_, v) => `${apiHost}/api/v1/commentsall?page=${v + 1}` + ).map((url) => got.post(url)) + ); + + const items = responses + .map((response) => response.data.data.list) + .flat() + .map((item) => ({ + title: `${item.user_info.name} ${item.title_desc} ${item.title}`, + description: item.desc, + pubDate: timezone(parseDate(item.created_at), +8), + author: item.user_info.name, + link: `${baseUrl}/view/${item.source_id || item.story_id}`, + guid: `baai:hub:comments:${item.id}`, + })); + + ctx.state.data = { + title: '评论 - 智源社区', + link: `${baseUrl}/comments`, + item: items, + }; +}; diff --git a/lib/v2/baai/events.js b/lib/v2/baai/events.js new file mode 100644 index 000000000..178d1104b --- /dev/null +++ b/lib/v2/baai/events.js @@ -0,0 +1,35 @@ +const got = require('@/utils/got'); + +const { baseUrl, apiHost, parseEventDetail, parseItem } = require('./utils'); + +module.exports = async (ctx) => { + const responses = await got.all( + Array.from( + { + // first 2 pages + length: (ctx.query.limit ? parseInt(ctx.query.limit, 10) : 16) / 8, + }, + (_, v) => `${apiHost}/api/v1/events?page=${v + 1}` + ).map((url) => got.post(url)) + ); + + const list = responses + .map((response) => response.data.data) + .flat() + .map((item) => parseItem(item)); + + const items = await Promise.all( + list.map((item) => + ctx.cache.tryGet(item.link, async () => { + item.description = await parseEventDetail(item); + return item; + }) + ) + ); + + ctx.state.data = { + title: '活动 - 智源社区', + link: `${baseUrl}/events`, + item: items, + }; +}; diff --git a/lib/v2/baai/hub.js b/lib/v2/baai/hub.js new file mode 100644 index 000000000..794173d87 --- /dev/null +++ b/lib/v2/baai/hub.js @@ -0,0 +1,60 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const { baseUrl, apiHost, parseEventDetail, parseItem } = require('./utils'); + +module.exports = async (ctx) => { + const { tagId, sort = 'new', range } = ctx.params; + + let title, description, brief, iconUrl; + if (tagId) { + const { data } = await got(`${apiHost}/api/v1/tags`, { + searchParams: { + id: tagId, + }, + }); + title = data.data[0].title; + description = data.data[0].description; + brief = data.data[0].brief; + iconUrl = data.data[0].icon_url; + } + + const responses = await got.all( + Array.from( + { + // first 3 pages + length: (ctx.query.limit ? parseInt(ctx.query.limit, 10) : 18) / 6, + }, + (_, v) => `${apiHost}/api/v1/story/list?page=${v + 1}&sort=${sort}${tagId ? `&tag_id=${tagId}` : ''}${range ? `&time_range=${range}` : ''}` + ).map((url) => got.post(url)) + ); + + const list = responses + .filter((response) => response.data.data) + .map((response) => response.data.data) + .flat() + .map((item) => parseItem(item)); + + const items = await Promise.all( + list.map((item) => + ctx.cache.tryGet(item.link, async () => { + if (!item.eventId) { + const { data: response } = await got(item.link); + const $ = cheerio.load(response); + item.description = item.is_event ? $('div.box2').html() : $('.post-content').html(); + } else { + item.description = await parseEventDetail(item); + } + return item; + }) + ) + ); + + ctx.state.data = { + title: `${title ? `${title} - ` : ''}${description ? `${description} - ` : ''}智源社区`, + description: brief, + link: `${baseUrl}/?${tagId ? `tag_id=${tagId}&` : ''}sort=${sort}${range ? `&time_range=${range}` : ''}`, + image: iconUrl, + item: items, + }; +}; diff --git a/lib/v2/baai/maintainer.js b/lib/v2/baai/maintainer.js new file mode 100644 index 000000000..c2632713b --- /dev/null +++ b/lib/v2/baai/maintainer.js @@ -0,0 +1,5 @@ +module.exports = { + '/hub/comments': ['TonyRL'], + '/hub/events': ['TonyRL'], + '/hub/:tagId?/:sort?/:range?': ['TonyRL'], +}; diff --git a/lib/v2/baai/radar.js b/lib/v2/baai/radar.js new file mode 100644 index 000000000..b5e6bf2d2 --- /dev/null +++ b/lib/v2/baai/radar.js @@ -0,0 +1,31 @@ +module.exports = { + 'baai.ac.cn': { + _name: '北京智源人工智能研究院', + hub: [ + { + title: '智源社区', + docs: 'https://docs.rsshub.app/programming.html#bei-jing-zhi-yuan-ren-gong-zhi-neng-yan-jiu-yuan', + source: ['/'], + target: (params, url) => { + const searchParams = new URL(url).searchParams; + const tagId = searchParams.get('tag_id'); + const sort = searchParams.get('sort'); + const range = searchParams.get('time_range'); + return `/baai/hub${tagId ? `/${tagId}` : ''}${sort ? `/${sort}` : ''}${range ? `/${range}` : ''}`; + }, + }, + { + title: '活动 - 智源社区', + docs: 'https://docs.rsshub.app/programming.html#bei-jing-zhi-yuan-ren-gong-zhi-neng-yan-jiu-yuan', + source: ['/events', '/'], + target: '/baai/hub/events', + }, + { + title: '评论 - 智源社区', + docs: 'https://docs.rsshub.app/programming.html#bei-jing-zhi-yuan-ren-gong-zhi-neng-yan-jiu-yuan', + source: ['/comments', '/'], + target: '/baai/hub/comments', + }, + ], + }, +}; diff --git a/lib/v2/baai/router.js b/lib/v2/baai/router.js new file mode 100644 index 000000000..cf7e38a5b --- /dev/null +++ b/lib/v2/baai/router.js @@ -0,0 +1,7 @@ +module.exports = (router) => { + router.get('/hub/comments', require('./comments')); + router.get('/hub/events', require('./events')); + router.get('/hub/:tagId/:sort?/:range?', require('./hub')); + router.get('/hub/:tagId/:sort?', require('./hub')); + router.get('/hub/:sort?', require('./hub')); +}; diff --git a/lib/v2/baai/utils.js b/lib/v2/baai/utils.js new file mode 100644 index 000000000..f54d94a93 --- /dev/null +++ b/lib/v2/baai/utils.js @@ -0,0 +1,33 @@ +const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); + +const baseUrl = 'https://hub.baai.ac.cn'; +const eventUrl = 'https://event.baai.ac.cn'; +const apiHost = 'https://hub-api.baai.ac.cn'; + +const parseItem = (item) => ({ + link: item.is_event ? `${eventUrl}/activities/${item.event_info.id}` : `${baseUrl}/view/${item.story_id}`, + title: item.is_event ? item.event_info.name : item.story_info.title, + pubDate: timezone(parseDate(item.is_event ? item.event_info.time_desc : item.story_info.created_at.replace('发布', '').replace('分享', '')), 8), + author: item.is_event ? item.event_info.company : item.story_info.user_name, + category: item.is_event ? null : item.story_info.tag_names.map((tag) => tag.title), + eventId: item.is_event ? item.event_info.id : null, +}); + +const parseEventDetail = async (item) => { + const { data } = await got(`${eventUrl}/api/api/Activity/IntroductionTypes`, { + searchParams: { + activityId: item.eventId, + }, + }); + return data.data.ac_desc + data.data.ac_desc_two; +}; + +module.exports = { + baseUrl, + eventUrl, + apiHost, + parseItem, + parseEventDetail, +};