diff --git a/docs/new-media.md b/docs/new-media.md index 0ce5fba5f..7b0d4efb4 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -1731,6 +1731,26 @@ Supported sub-sites: +## 北京市科学技术协会 + +### 通用 + + + +::: tip 提示 + +路径处填写对应页面 URL 中 `https://www.bast.net.cn/` 后的字段。下面是两个例子。 + +若订阅 [通知公告](https://www.bast.net.cn/col/col31266) 则将对应页面 URL 中 `https://www.bast.net.cn/` 后的字段 `col/col31266` 作为路径填入。此时路由为 [`/bast/col/col31266`](https://rsshub.app/bast/col/col31266) + +若订阅 [学术动态](https://www.bast.net.cn/col/col31530) 则将对应页面 URL 中 `https://www.bast.net.cn/` 后的字段 `col/col31530` 作为路径填入。此时路由为 [`/bast/col/col31530`](https://rsshub.app/bast/col/col31530) + +如果路由符合 `/col/colXXXXX` 的格式,可以由 [`/bast/col/col31266`](https://rsshub.app/bast/col/col31266) 精简为 [`/bast/31266`](https://rsshub.app/bast/31266) + +::: + + + ## 北屋 diff --git a/lib/v2/bast/index.js b/lib/v2/bast/index.js new file mode 100644 index 000000000..3f55d7181 --- /dev/null +++ b/lib/v2/bast/index.js @@ -0,0 +1,74 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const colPath = ctx.path.replace(/^\//, '') || '32942'; + const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 50; + + const rootUrl = 'https://www.bast.net.cn'; + const currentUrl = `${rootUrl}/${isNaN(colPath) ? colPath : `col/col${colPath}`}/`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + let $ = cheerio.load(response.data); + + $('.list-title-bif').remove(); + + const title = $('title').text(); + let selection = $('a[title]'); + + if (selection.length === 0) { + $ = cheerio.load($('ul.cont-list div script').first().text()); + + $('.list-title-bif').remove(); + + selection = $('a[title]'); + } + + let items = selection + .slice(0, limit) + .toArray() + .map((item) => { + item = $(item); + + return { + title: item.text().trim(), + link: item.attr('href'), + }; + }); + + items = await Promise.all( + items.map((item) => + ctx.cache.tryGet(item.link, async () => { + if (/bast\.net\.cn/.test(item.link)) { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data); + + item.title = content('meta[name="ArticleTitle"]').attr('content'); + item.author = content('meta[name="contentSource"]').attr('content'); + item.pubDate = timezone(parseDate(content('meta[name="pubdate"]').attr('content')), +8); + item.category = [content('meta[name="ColumnName"]').attr('content')]; + + item.description = content('.arccont').html(); + } + + return item; + }) + ) + ); + + ctx.state.data = { + title, + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/bast/maintainer.js b/lib/v2/bast/maintainer.js new file mode 100644 index 000000000..57f872453 --- /dev/null +++ b/lib/v2/bast/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/:path+': ['nczitzk'], +}; diff --git a/lib/v2/bast/radar.js b/lib/v2/bast/radar.js new file mode 100644 index 000000000..e956fbbdc --- /dev/null +++ b/lib/v2/bast/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'bast.net.cn': { + _name: '北京市科学技术协会', + '.': [ + { + title: '通用', + docs: 'https://docs.rsshub.app/new-media.html#bei-jing-shi-ke-xue-ji-shu-xie-hui-tong-yong', + source: ['/col', '/'], + target: (params, url) => `/bast/${new URL(url).href.match(/bast\.net\.cn\/(.*)/)[1].replace(/\/index\.html/, '')}`, + }, + ], + }, +}; diff --git a/lib/v2/bast/router.js b/lib/v2/bast/router.js new file mode 100644 index 000000000..406315685 --- /dev/null +++ b/lib/v2/bast/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get(/([\w-/]+)?/, require('./index')); +};