diff --git a/docs/bbs.md b/docs/bbs.md index e043c6467..0a30cabd7 100644 --- a/docs/bbs.md +++ b/docs/bbs.md @@ -507,6 +507,16 @@ pageClass: routes +## 丁香园 + +### 专题 + + + +### 个人帖子 + + + ## 斗鱼 ### 鱼吧帖子 diff --git a/lib/v2/dxy/maintainer.js b/lib/v2/dxy/maintainer.js new file mode 100644 index 000000000..ca98bea3f --- /dev/null +++ b/lib/v2/dxy/maintainer.js @@ -0,0 +1,4 @@ +module.exports = { + '/bbs/profile/thread/:userId': ['TonyRL'], + '/bbs/special/:specialId': ['TonyRL'], +}; diff --git a/lib/v2/dxy/profile/thread.js b/lib/v2/dxy/profile/thread.js new file mode 100644 index 000000000..571744d3d --- /dev/null +++ b/lib/v2/dxy/profile/thread.js @@ -0,0 +1,82 @@ +const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); +const { webBaseUrl, generateNonce, sign, getPost } = require('../utils'); +const config = require('@/config').value; + +module.exports = async (ctx) => { + const { userId } = ctx.params; + const { limit = '30' } = ctx.query; + + const userInfo = await ctx.cache.tryGet(`dxy:user-info:${userId}`, async () => { + const userInfoParams = { + userId, + serverTimestamp: Date.now(), + timestamp: Date.now(), + noncestr: generateNonce(8, 'number'), + }; + + const { data: userInfo } = await got(`${webBaseUrl}/bbs/newweb/personal-page/user-info`, { + searchParams: { + ...userInfoParams, + sign: sign(userInfoParams), + }, + }); + if (userInfo.code !== 'success') { + throw Error(userInfo.message); + } + + return userInfo.data; + }); + + const postList = await ctx.cache.tryGet( + `dxy:user:post:${userId}`, + async () => { + const postListParams = { + userId, + type: '0', + pageNum: '1', + pageSize: limit, + serverTimestamp: Date.now(), + timestamp: Date.now(), + noncestr: generateNonce(8, 'number'), + }; + + const { data: postList } = await got(`${webBaseUrl}/bbs/newweb/user/post/page`, { + searchParams: { + ...postListParams, + sign: sign(postListParams), + }, + }); + if (postList.code !== 'success') { + throw Error(postList.message); + } + + return postList.data; + }, + config.cache.routeExpire, + false + ); + + const list = postList.result.map((item) => { + const { postInfo } = item; + return { + title: postInfo.subject, + description: postInfo.simpleBody, + pubDate: parseDate(item.createdTime, 'x'), + author: postInfo.postUser.nickname, + category: postInfo.boardInfo.title, + link: `${webBaseUrl}/bbs/newweb/pc/post/${item.entityId}`, + postId: item.entityId, + }; + }); + + const items = await Promise.all(list.map((item) => getPost(item, ctx.cache.tryGet))); + + ctx.state.data = { + title: `${userInfo.nickname} 的个人主页 - 丁香园论坛 - 专业医生社区,医学、药学、生命科学、科研学术交流`, + description: `${userInfo.identificationTitle} ${userInfo.signature}`, + link: `${webBaseUrl}/bbs/newweb/pc/profile/${userId}/threads`, + image: userInfo.avatar, + item: items, + }; +}; diff --git a/lib/v2/dxy/radar.js b/lib/v2/dxy/radar.js new file mode 100644 index 000000000..11f627247 --- /dev/null +++ b/lib/v2/dxy/radar.js @@ -0,0 +1,21 @@ +module.exports = { + 'dxy.cn': { + _name: '丁香园', + '.': [ + { + title: '个人帖子', + docs: 'https://docs.rsshub.app/bbs.html#ding-xiang-yuan', + source: ['/bbs/newweb/pc/profile/:userId/threads', '/bbs/newweb/pc/profile/:userId'], + target: '/dxy/bbs/profile/thread/:userId', + }, + ], + '3g': [ + { + title: '专题', + docs: 'https://docs.rsshub.app/bbs.html#ding-xiang-yuan', + source: ['/bbs/special'], + target: (_, url) => `/dxy/bbs/special/${new URL(url).searchParams.get('specialId')}`, + }, + ], + }, +}; diff --git a/lib/v2/dxy/router.js b/lib/v2/dxy/router.js new file mode 100644 index 000000000..ac9dec042 --- /dev/null +++ b/lib/v2/dxy/router.js @@ -0,0 +1,4 @@ +module.exports = (router) => { + router.get('/bbs/profile/thread/:userId', require('./profile/thread')); + router.get('/bbs/special/:specialId', require('./special')); +}; diff --git a/lib/v2/dxy/special.js b/lib/v2/dxy/special.js new file mode 100644 index 000000000..4ee245dd2 --- /dev/null +++ b/lib/v2/dxy/special.js @@ -0,0 +1,79 @@ +const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); +const { phoneBaseUrl, webBaseUrl, generateNonce, sign, getPost } = require('./utils'); +const config = require('@/config').value; + +module.exports = async (ctx) => { + const { specialId } = ctx.params; + const { limit = '10' } = ctx.query; + + const specialDetail = await ctx.cache.tryGet(`dxy:special:detail:${specialId}`, async () => { + const detailParams = { + specialId, + requestType: 'h5', + timestamp: Date.now(), + noncestr: generateNonce(8, 'number'), + }; + + const { data: detail } = await got(`${phoneBaseUrl}/newh5/bbs/special/detail`, { + searchParams: { + ...detailParams, + sign: sign(detailParams), + }, + }); + if (detail.code !== 'success') { + throw Error(detail.message); + } + return detail.data; + }); + + const recommendList = await ctx.cache.tryGet( + `dxy:special:recommend-list-v3:${specialId}`, + async () => { + const listParams = { + specialId, + requestType: 'h5', + pageNum: '1', + pageSize: limit, + timestamp: Date.now(), + noncestr: generateNonce(8, 'number'), + }; + + const { data: recommendList } = await got(`${phoneBaseUrl}/newh5/bbs/special/post/recommend-list-v3`, { + searchParams: { + ...listParams, + sign: sign(listParams), + }, + }); + if (recommendList.code !== 'success') { + throw Error(recommendList.message); + } + return recommendList.data; + }, + config.cache.routeExpire, + false + ); + + const list = recommendList.result.map((item) => { + const { postInfo } = item; + return { + title: postInfo.subject, + description: postInfo.simpleBody, + pubDate: parseDate(item.dataTime, 'x'), + author: postInfo.postUser.nickname, + category: postInfo.postSpecial.specialName, + link: `${webBaseUrl}/bbs/newweb/pc/post/${item.entityId}`, + postId: item.entityId, + }; + }); + + const items = await Promise.all(list.map((item) => getPost(item, ctx.cache.tryGet))); + + ctx.state.data = { + title: specialDetail.name, + description: `${specialDetail.content} ${specialDetail.postCount} 內容 ${specialDetail.followCount} 关注`, + link: `${phoneBaseUrl}/bbs/special?specialId=${specialId}`, + image: specialDetail.specialAvatar, + item: items, + }; +}; diff --git a/lib/v2/dxy/utils.js b/lib/v2/dxy/utils.js new file mode 100644 index 000000000..e5ed6a519 --- /dev/null +++ b/lib/v2/dxy/utils.js @@ -0,0 +1,72 @@ +const CryptoJS = require('crypto-js'); +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +const APP_SIGN_KEY = '4bTogwpz7RzNO2VTFtW7zcfRkAE97ox6ZSgcQi7FgYdqrHqKB7aGqEZ4o7yssa2aEXoV3bQwh12FFgVNlpyYk2Yjm9d2EZGeGu3'; +const phoneBaseUrl = 'https://3g.dxy.cn'; +const webBaseUrl = 'https://www.dxy.cn'; + +const generateNonce = (length = 8, type = 'alphabet') => { + const characters = { + alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', + number: '0123456789', + }; + + let result = ''; + const selectedCharacters = characters[type] || characters.alphabet; + + for (let i = 0; i < length; i++) { + result += selectedCharacters.charAt(Math.floor(Math.random() * selectedCharacters.length)); + } + + return result; +}; + +const sign = (params) => { + const searchParams = new URLSearchParams(params); + searchParams.append('appSignKey', APP_SIGN_KEY); + searchParams.sort(); + return CryptoJS.SHA1(searchParams.toString()).toString(); +}; + +const getPost = (item, tryGet) => + tryGet(item.link, async () => { + const postParams = { + postId: item.postId, + serverTimestamp: Date.now(), + timestamp: Date.now(), + noncestr: generateNonce(8, 'number'), + }; + + const { data: post } = await got('https://www.dxy.cn/bbs/newweb/post/detail', { + searchParams: { + ...postParams, + sign: sign(postParams), + }, + }); + if (post.code !== 'success') { + throw Error(post.message); + } + + const $ = cheerio.load(post.data.body, null, false); + + $('img').each((_, img) => { + img = $(img); + img.removeAttr('data-osrc'); + img.removeAttr('data-hsrc'); + }); + + item.description = $.html(); + item.updated = parseDate(post.data.lastEditTime, 'x'); + + return item; + }); + +module.exports = { + phoneBaseUrl, + webBaseUrl, + generateNonce, + sign, + getPost, +};