From a60663ee916ab807c065c979e2aface39b83d306 Mon Sep 17 00:00:00 2001 From: MisteryMonster <40703811+MisteryMonster@users.noreply.github.com> Date: Thu, 26 May 2022 23:05:27 +0800 Subject: [PATCH] fix(route): behance v2 & fix (#9767) * behance v2 & fix * behance v2 & fix * Try get & remove old radar * fetch limits * fix: maintainer --- assets/radar-rules.js | 14 -------- docs/design.md | 4 +-- lib/radar-rules.js | 14 -------- lib/router.js | 3 -- lib/routes/behance/index.js | 58 ------------------------------- lib/v2/behance/maintainer.js | 3 ++ lib/v2/behance/radar.js | 26 ++++++++++++++ lib/v2/behance/router.js | 3 ++ lib/v2/behance/user.js | 67 ++++++++++++++++++++++++++++++++++++ 9 files changed, 101 insertions(+), 91 deletions(-) delete mode 100644 lib/routes/behance/index.js create mode 100644 lib/v2/behance/maintainer.js create mode 100644 lib/v2/behance/radar.js create mode 100644 lib/v2/behance/router.js create mode 100644 lib/v2/behance/user.js diff --git a/assets/radar-rules.js b/assets/radar-rules.js index 28334d819..d5f5fa733 100644 --- a/assets/radar-rules.js +++ b/assets/radar-rules.js @@ -817,20 +817,6 @@ { title: '用户投稿', docs: 'https://docs.rsshub.app/anime.html#acfun-yong-hu-tou-gao', source: '/u/:id', target: '/acfun/user/video/:id' }, ], }, - 'behance.net': { - _name: 'Behance', - www: [ - { - title: 'User', - docs: 'https://docs.rsshub.app/design.html#behance-user-works', - source: ['/:user'], - target: (params, url, document) => { - const uid1 = document && document.querySelector('html').innerHTML.match(/([^/]+)\/insights/)[1]; - return `/behance/${uid1}`; - }, - }, - ], - }, 'jjmhw.cc': { _name: '漫小肆', www: [{ title: '漫画更新', docs: 'https://docs.rsshub.app/anime.html#man-xiao-si', source: '/book/:id', target: '/manxiaosi/book/:id' }] }, 'wenxuecity.com': { _name: '文学城', diff --git a/docs/design.md b/docs/design.md index e126a49d9..b0af1f6ed 100644 --- a/docs/design.md +++ b/docs/design.md @@ -18,9 +18,9 @@ pageClass: routes ## Behance -### User Works +### 用户作品 - + Behance 用户主页 URL 获取用户名,如 的用户名为 `mishapetrick`。 diff --git a/lib/radar-rules.js b/lib/radar-rules.js index 048ebada5..570756883 100644 --- a/lib/radar-rules.js +++ b/lib/radar-rules.js @@ -1673,20 +1673,6 @@ module.exports = { }, ], }, - 'behance.net': { - _name: 'Behance', - www: [ - { - title: 'User', - docs: 'https://docs.rsshub.app/design.html#behance-user-works', - source: ['/:user'], - target: (params, url, document) => { - const uid1 = document && document.querySelector('html').innerHTML.match(/([^/]+)\/insights/)[1]; - return `/behance/${uid1}`; - }, - }, - ], - }, 'jjmhw.cc': { _name: '漫小肆', www: [ diff --git a/lib/router.js b/lib/router.js index e5dfb8001..2073cc168 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3022,9 +3022,6 @@ router.get('/touhougarakuta/:language/:type', lazyloadRouteHandler('./routes/tou // 猎趣TV router.get('/liequtv/room/:id', lazyloadRouteHandler('./routes/liequtv/room')); -// Behance -router.get('/behance/:user/:type?', lazyloadRouteHandler('./routes/behance/index')); - // 北京物资学院 router.get('/bwu/news', lazyloadRouteHandler('./routes/universities/bwu/news')); diff --git a/lib/routes/behance/index.js b/lib/routes/behance/index.js deleted file mode 100644 index 2b28b268d..000000000 --- a/lib/routes/behance/index.js +++ /dev/null @@ -1,58 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); -const dayjs = require('dayjs'); - -module.exports = async (ctx) => { - const user = ctx.params.user || ''; - const type = ctx.params.type || 'projects'; - - const response = await got({ - method: 'get', - url: `https://www.behance.net/${user}/${type}`, // 接口只获取12个项目 - headers: { - 'X-Requested-With': 'XMLHttpRequest', - }, - }); - const data = response.data; - const list = data.profile.activeSection.work.projects; - const articledata = await Promise.all( - list.map(async (item) => { - if (type !== 'projects') { - item = item.project; - } - const url = `${item.url}?ilo0=1`; - const cache = await ctx.cache.get(url); - if (cache) { - return Promise.resolve(JSON.parse(cache)); - } - const response2 = await got({ - method: 'get', - url, - }); - const articleHtml = response2.data; - const $2 = cheerio.load(articleHtml); - - const content = $2('div.project-styles').html(); - const single = { - content, - }; - ctx.cache.set(url, JSON.stringify(single)); - return Promise.resolve(single); - }) - ); - ctx.state.data = { - title: `${data.profile.owner.first_name} ${data.profile.owner.last_name}'s Work`, - link: data.profile.owner.url, - item: list.map((item, index) => { - if (type !== 'projects') { - item = item.project; - } - return { - title: item.name, - description: articledata[index].content, - link: item.url, - pubDate: dayjs.unix(item.published_on).format(), - }; - }), - }; -}; diff --git a/lib/v2/behance/maintainer.js b/lib/v2/behance/maintainer.js new file mode 100644 index 000000000..5f58bae46 --- /dev/null +++ b/lib/v2/behance/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/:user/:type?': ['MisteryMonster'], +}; diff --git a/lib/v2/behance/radar.js b/lib/v2/behance/radar.js new file mode 100644 index 000000000..298884994 --- /dev/null +++ b/lib/v2/behance/radar.js @@ -0,0 +1,26 @@ +module.exports = { + 'behance.net': { + _name: 'Behance', + www: [ + { + title: 'User', + docs: 'https://docs.rsshub.app/design.html#behance-yong-hu-zuo-pin', + source: ['/:user', '/:user/:types', '/gallery/:galleryid/:galleryname'], + target: (params, url, document) => { + let uid; + let type = ''; + if (params.types && params.types.match('appreciated')) { + type = '/appreciated'; + } + if (url.match(/gallery\/\d+/)) { + uid = document && document.querySelector('.e2e-project-avatar').childNodes[0].attributes[1].value.match(/behance.net\/(.*)/)[1]; + } else { + uid = document && document.querySelector('html').innerHTML.match(/([^/]+)\/insights/)[1]; + } + + return `/behance/${uid}${type}`; + }, + }, + ], + }, +}; diff --git a/lib/v2/behance/router.js b/lib/v2/behance/router.js new file mode 100644 index 000000000..7121f90dd --- /dev/null +++ b/lib/v2/behance/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/:user/:type?', require('./user')); +}; diff --git a/lib/v2/behance/user.js b/lib/v2/behance/user.js new file mode 100644 index 000000000..515b36f12 --- /dev/null +++ b/lib/v2/behance/user.js @@ -0,0 +1,67 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const user = ctx.params.user ?? ''; + const type = ctx.params.type ?? 'projects'; + + const response = await got({ + method: 'get', + url: `https://www.behance.net/${user}/${type}`, // 接口只获取12个项目 + headers: { + 'X-Requested-With': 'XMLHttpRequest', + }, + }); + const data = response.data; + let list; + if (type === 'projects') { + list = data.profile.activeSection.work.projects.slice(0, 12); + } + if (type === 'appreciated') { + list = data.profile.activeSection.appreciations.appreciations.slice(0, 12); + } + const articledata = await Promise.all( + list.map(async (item) => { + if (type === 'appreciated') { + item = item.project; + } + const url = `${item.url}?ilo0=1`; + const description = await ctx.cache.tryGet(url, async () => { + const response2 = await got({ + method: 'get', + url, + }); + const articleHtml = response2.data; + const $2 = cheerio.load(articleHtml); + $2('.ImageElement-root-kir').remove(); + $2('.embed-dimensions').remove(); + $2('script.js-lightbox-slide-content').each((_, elem) => { + elem = $2(elem); + elem.replaceWith(elem.html()); + }); + const content = $2('div.project-styles').html(); + const single = { + content, + }; + return single; + }); + return description; + }) + ); + ctx.state.data = { + title: `${data.profile.owner.first_name} ${data.profile.owner.last_name}'s ${type}`, + link: data.profile.owner.url, + item: list.map((item, index) => { + if (type === 'appreciated') { + item = item.project; + } + return { + title: item.name, + description: articledata[index].content, + link: item.url, + pubDate: parseDate(item.published_on * 1000), + }; + }), + }; +};