From 3b73533f9544964136def69afeacf2915f2a7cab Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Tue, 26 Apr 2022 13:29:40 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E4=BA=AC=E4=B8=9C?= =?UTF-8?q?=E5=95=86=E5=93=81=E4=BB=B7=E6=A0=BC=20(#9629)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/shopping.md | 14 ++++++++++ lib/v2/jd/maintainer.js | 3 +++ lib/v2/jd/price.js | 41 +++++++++++++++++++++++++++++ lib/v2/jd/radar.js | 13 +++++++++ lib/v2/jd/router.js | 3 +++ lib/v2/jd/templates/description.art | 3 +++ 6 files changed, 77 insertions(+) create mode 100644 lib/v2/jd/maintainer.js create mode 100644 lib/v2/jd/price.js create mode 100644 lib/v2/jd/radar.js create mode 100644 lib/v2/jd/router.js create mode 100644 lib/v2/jd/templates/description.art diff --git a/docs/shopping.md b/docs/shopping.md index 55889d781..52887e7a4 100644 --- a/docs/shopping.md +++ b/docs/shopping.md @@ -171,6 +171,20 @@ For instance, in +## 京东 + +### 商品价格 + + + +::: tip 提示 + +如商品 中的 id 为 `526835`,所以路由为 [`/jd/price/526835`](https://rsshub.app/jd/price/526835) + +::: + + + ## 京东众筹 ### 众筹项目 diff --git a/lib/v2/jd/maintainer.js b/lib/v2/jd/maintainer.js new file mode 100644 index 000000000..6afeec1ff --- /dev/null +++ b/lib/v2/jd/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/price/:id': ['nczitzk'], +}; diff --git a/lib/v2/jd/price.js b/lib/v2/jd/price.js new file mode 100644 index 000000000..630cbb815 --- /dev/null +++ b/lib/v2/jd/price.js @@ -0,0 +1,41 @@ +const got = require('@/utils/got'); +const { art } = require('@/utils/render'); +const path = require('path'); + +module.exports = async (ctx) => { + const id = ctx.params.id; + + const rootUrl = 'https://item.jd.com'; + const currentUrl = `${rootUrl}/${id}.html`; + const apiUrl = `http://p.3.cn/prices/mgets?skuIds=J_${id}`; + + const apiResponse = await got({ + method: 'get', + url: apiUrl, + }); + + const data = apiResponse.data[0]; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const title = response.data.match(/name: '(.*?)'/)[1]; + + ctx.state.data = { + title: `京东商品价格 - ${title}`, + link: currentUrl, + item: [ + { + title, + link: currentUrl, + description: art(path.join(__dirname, 'templates/description.art'), { + p: data.p, + op: data.op, + m: data.m, + }), + }, + ], + }; +}; diff --git a/lib/v2/jd/radar.js b/lib/v2/jd/radar.js new file mode 100644 index 000000000..fc0b43bfc --- /dev/null +++ b/lib/v2/jd/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'jd.com': { + _name: '京东', + item: [ + { + title: '商品价格', + docs: 'https://docs.rsshub.app/shopping.html#jing-dong-shang-pin-jia-ge', + source: ['/'], + target: (params, url) => `/jd/price/${new URL(url).hash.match(/\/(\d+)\.html/)[1]}`, + }, + ], + }, +}; diff --git a/lib/v2/jd/router.js b/lib/v2/jd/router.js new file mode 100644 index 000000000..b4ca0cb79 --- /dev/null +++ b/lib/v2/jd/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/price/:id', require('./price')); +}; diff --git a/lib/v2/jd/templates/description.art b/lib/v2/jd/templates/description.art new file mode 100644 index 000000000..a99951bbd --- /dev/null +++ b/lib/v2/jd/templates/description.art @@ -0,0 +1,3 @@ +

目前价格:{{ p }}

+

指导价:{{ op }}

+

最高价:{{ m }}

\ No newline at end of file