From 849058b119e5f0fc9a3063264a6246354edd8fd4 Mon Sep 17 00:00:00 2001 From: Jiaju Chen <619507631@qq.com> Date: Fri, 25 Oct 2019 17:25:50 +0800 Subject: [PATCH] feat: add pigprice (#3314) --- docs/other.md | 6 +++++ lib/router.js | 3 +++ lib/routes/pork-price/index.js | 48 ++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 lib/routes/pork-price/index.js diff --git a/docs/other.md b/docs/other.md index 4c66281019..04d0b7cb5e 100644 --- a/docs/other.md +++ b/docs/other.md @@ -231,6 +231,12 @@ type 为 all 时,category 参数不支持 cost 和 free +## 每日生猪价格 + +### 每日生猪价格更新 + + + ## 米坛社区 ### 表盘更新 diff --git a/lib/router.js b/lib/router.js index 1de3b458c4..897ce2b227 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1888,4 +1888,7 @@ router.get('/hk01/hot', require('./routes/hk01/hot')); // 码农周刊 router.get('/manong-weekly', require('./routes/manong-weekly/issues')); +// 每日猪价 +router.get('/pork-price', require('./routes/pork-price')); + module.exports = router; diff --git a/lib/routes/pork-price/index.js b/lib/routes/pork-price/index.js new file mode 100644 index 0000000000..b8d83f7f61 --- /dev/null +++ b/lib/routes/pork-price/index.js @@ -0,0 +1,48 @@ +const got = require('@/utils/got'); + +const base_url = 'https://zhujia.zhuwang.cc/'; +module.exports = async (ctx) => { + const now = new Date(); + const date = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`; + const response = await got({ + method: 'get', + url: `https://zhujia.zhuwang.cc/index/api/chartData?areaId=-1`, + headers: { + Referer: base_url, + }, + }); + + const names = { + pigprice: '生猪(外三元)', + pig_local: '生猪(土杂猪)', + pig_in: '生猪(内三元)', + }; + + const priceItems = []; + for (const key of Object.keys(names)) { + const items = response.data[key]; + const today = items[items.length - 1]; + const yesterday = items[items.length - 2]; + const change = (today - yesterday).toFixed(2); + let description = `较昨日价格, 每公斤上涨${change}元`; + if (yesterday > today) { + description = `较昨日价格, 下跌${change}元`; + } + if (yesterday === today) { + description = '较昨日价格持平'; + } + + priceItems.push({ + title: `${date} ${names[key]} ${today}元/公斤. ${description}`, + description, + link: base_url, + }); + } + + ctx.state.data = { + title: `全国今日生猪价格`, + desription: '中国养猪网猪价频道是中国猪价权威平台,提供每日猪评,猪价和行情分析,并且预测猪价和分析每天的猪价排行。', + link: base_url, + item: priceItems, + }; +};