feat: add TransferWise 昨日汇率变动 (#3466)

This commit is contained in:
Henry Wang
2019-11-25 03:50:46 +00:00
committed by DIYgod
parent be45f45c56
commit fe3a03be13
4 changed files with 85 additions and 1 deletions

View File

@@ -71,10 +71,20 @@ EZTV provides an official RSS feed of all torrents: https://eztv.ag/ezrss.xml
### Remote.work Job Information ### Remote.work Job Information
<Route author="luyuhuang" example="/remote-work/all" path="/remote-work/:caty?" :paramsDesc="['Job category, default all']" radar="1"> <Route author="luyuhuang" example="/remote-work/all" path="/remote-work/:caty?" :paramsDesc="['Job category, default to all']" radar="1">
| All Jobs | Development | Design | Operation | Product | Other | Marketing | Sales | | All Jobs | Development | Design | Operation | Product | Other | Marketing | Sales |
| :------: | :---------: | :----: | :-------: | :-----: | :---: | :-------: | :---: | | :------: | :---------: | :----: | :-------: | :-----: | :---: | :-------: | :---: |
| all | development | design | operation | product | other | marketing | sales | | all | development | design | operation | product | other | marketing | sales |
</Route> </Route>
## TransferWise
### FX Pair Yesterday
<RouteEn author="HenryQW" example="/transferwise/pair/GBP/USD" path="/transferwise/pair/:source/:target" :paramsDesc="['Base currency abbreviation','Quote currency abbreviation']">
See [the list of supported currencies](https://transferwise.com/tools/exchange-rate-alerts/).
</RouteEn>

View File

@@ -107,6 +107,16 @@ pageClass: routes
<Route author="sbilly" example="/sans/summit_archive" path="/sans/summit_archive" /> <Route author="sbilly" example="/sans/summit_archive" path="/sans/summit_archive" />
## TransferWise
### 昨日汇率变动
<Route author="HenryQW" example="/transferwise/pair/GBP/USD" path="/transferwise/pair/:source/:target" :paramsDesc="['本币缩写','外币缩写']">
参见支持的[货币列表](https://transferwise.com/tools/exchange-rate-alerts/)。
</Route>
## TSSstatusiOS 降级通道) ## TSSstatusiOS 降级通道)
### Status ### Status

View File

@@ -1995,6 +1995,9 @@ router.get('/itslide/new', require('./routes/itslide/new'));
// Remote Work // Remote Work
router.get('/remote-work/:caty?', require('./routes/remote-work/index')); router.get('/remote-work/:caty?', require('./routes/remote-work/index'));
// TransferWise
router.get('/transferwise/pair/:source/:target', require('./routes/transferwise/pair'));
// chocolatey // chocolatey
router.get('/chocolatey/software/:name?', require('./routes/chocolatey/software')); router.get('/chocolatey/software/:name?', require('./routes/chocolatey/software'));

View File

@@ -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 = `<h3>${source} to ${target}</h3><table><tbody><tr><th align="left" style="border: 1px solid black;">Date</th><th align="left" style="border: 1px solid black;">Rate</th></tr><tr><td style="border: 1px solid black;">${yesterday}</td><td style="border: 1px solid black;">${data[0].rate}</td></tr><tr><td style="border: 1px solid black;">${dayBefore}</td><td style="border: 1px solid black;">${data[1].rate}</td></tr></tbody></table>`;
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],
};
}
};