From fe3a03be134413ed427232aae0ebb055a2b1fdaa Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Mon, 25 Nov 2019 03:50:46 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20add=20TransferWise=20=E6=98=A8=E6=97=A5?= =?UTF-8?q?=E6=B1=87=E7=8E=87=E5=8F=98=E5=8A=A8=20(#3466)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/en/other.md | 12 ++++++- docs/other.md | 10 ++++++ lib/router.js | 3 ++ lib/routes/transferwise/pair.js | 61 +++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 lib/routes/transferwise/pair.js diff --git a/docs/en/other.md b/docs/en/other.md index e691ab0b8..50c9ad711 100644 --- a/docs/en/other.md +++ b/docs/en/other.md @@ -71,10 +71,20 @@ EZTV provides an official RSS feed of all torrents: https://eztv.ag/ezrss.xml ### Remote.work Job Information - + | All Jobs | Development | Design | Operation | Product | Other | Marketing | Sales | | :------: | :---------: | :----: | :-------: | :-----: | :---: | :-------: | :---: | | all | development | design | operation | product | other | marketing | sales | + +## TransferWise + +### FX Pair Yesterday + + + +See [the list of supported currencies](https://transferwise.com/tools/exchange-rate-alerts/). + + diff --git a/docs/other.md b/docs/other.md index ce3d3cf1f..6829abdc8 100644 --- a/docs/other.md +++ b/docs/other.md @@ -107,6 +107,16 @@ pageClass: routes +## TransferWise + +### 昨日汇率变动 + + + +参见支持的[货币列表](https://transferwise.com/tools/exchange-rate-alerts/)。 + + + ## TSSstatus(iOS 降级通道) ### Status diff --git a/lib/router.js b/lib/router.js index b630f3f96..ade5c9836 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1995,6 +1995,9 @@ router.get('/itslide/new', require('./routes/itslide/new')); // Remote Work router.get('/remote-work/:caty?', require('./routes/remote-work/index')); +// TransferWise +router.get('/transferwise/pair/:source/:target', require('./routes/transferwise/pair')); + // chocolatey router.get('/chocolatey/software/:name?', require('./routes/chocolatey/software')); diff --git a/lib/routes/transferwise/pair.js b/lib/routes/transferwise/pair.js new file mode 100644 index 000000000..bb9ff9162 --- /dev/null +++ b/lib/routes/transferwise/pair.js @@ -0,0 +1,61 @@ +const got = require('@/utils/got'); +const dayjs = require('dayjs'); +const customParseFormat = require('dayjs/plugin/customParseFormat'); + +module.exports = async (ctx) => { + dayjs.extend(customParseFormat); + let yesterday = dayjs().subtract(1, 'day'); + const dayBefore = yesterday.subtract(1, 'day').format('YYYY-MM-DD'); + + yesterday = yesterday.format('YYYY-MM-DD'); + + const guid = `${ctx.params.source}-${ctx.params.target}-${yesterday}`; + const cache = await ctx.cache.get(guid); + + const link = 'https://transferwise.com/tools/exchange-rate-alerts/'; + + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } else { + const source = ctx.params.source.toUpperCase(); + const target = ctx.params.target.toUpperCase(); + const api = `https://api.transferwise.com/v1/rates?source=${source}&target=${target}&from=${dayBefore}&to=${yesterday}&group=day`; + + const response = await got({ + method: 'get', + url: api, + headers: { + Referer: 'https://transferwise.com/tools/exchange-rate-alerts/', + authorization: 'Basic OGNhN2FlMjUtOTNjNS00MmFlLThhYjQtMzlkZTFlOTQzZDEwOjliN2UzNmZkLWRjYjgtNDEwZS1hYzc3LTQ5NGRmYmEyZGJjZA==', + 'Content-Type': 'application/json', + }, + }); + + const data = response.data; + const trend = data[0].rate > data[1].rate; + const diff = (data[0].rate - data[1].rate) / data[1].rate; + + const percent = (Math.abs(diff) * 100).toFixed(4); + + const title = `${source}/${target} ${trend ? '📈' : '📉'} @${data[0].rate} ${diff > 0 ? '' : '-'}${percent}%`; + + const description = `

${source} to ${target}

DateRate
${yesterday}${data[0].rate}
${dayBefore}${data[1].rate}
`; + + const single = { + title, + description, + pubDate: new Date().toUTCString(), + guid, + link, + }; + + ctx.cache.set(guid, JSON.stringify(ctx.state.data)); + + ctx.state.data = { + title: `${source} to ${target} by TransferWise`, + link, + description: `Exchange Rate from TransferWise`, + item: [single], + }; + } +};