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