mirror of
https://github.com/Redume/Kekkai.git
synced 2025-02-23 12:43:12 +03:00
Перенес сбор валюты в отдельный сервис
This commit is contained in:
parent
89d3376785
commit
95f7160f5d
4 changed files with 191 additions and 0 deletions
1
collect-currency/.gitignore
vendored
Normal file
1
collect-currency/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
node_modules/
|
115
collect-currency/package-lock.json
generated
Normal file
115
collect-currency/package-lock.json
generated
Normal file
|
@ -0,0 +1,115 @@
|
|||
{
|
||||
"name": "collect-currency",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "collect-currency",
|
||||
"version": "1.0.0",
|
||||
"license": "GPL-3.0-or-later",
|
||||
"dependencies": {
|
||||
"axios": "^1.7.2"
|
||||
}
|
||||
},
|
||||
"node_modules/asynckit": {
|
||||
"version": "0.4.0",
|
||||
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
|
||||
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/axios": {
|
||||
"version": "1.7.2",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-1.7.2.tgz",
|
||||
"integrity": "sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"follow-redirects": "^1.15.6",
|
||||
"form-data": "^4.0.0",
|
||||
"proxy-from-env": "^1.1.0"
|
||||
}
|
||||
},
|
||||
"node_modules/combined-stream": {
|
||||
"version": "1.0.8",
|
||||
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
|
||||
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"delayed-stream": "~1.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/delayed-stream": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
|
||||
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=0.4.0"
|
||||
}
|
||||
},
|
||||
"node_modules/follow-redirects": {
|
||||
"version": "1.15.6",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz",
|
||||
"integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==",
|
||||
"funding": [
|
||||
{
|
||||
"type": "individual",
|
||||
"url": "https://github.com/sponsors/RubenVerborgh"
|
||||
}
|
||||
],
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=4.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"debug": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/form-data": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
|
||||
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"asynckit": "^0.4.0",
|
||||
"combined-stream": "^1.0.8",
|
||||
"mime-types": "^2.1.12"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 6"
|
||||
}
|
||||
},
|
||||
"node_modules/mime-db": {
|
||||
"version": "1.52.0",
|
||||
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
|
||||
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">= 0.6"
|
||||
}
|
||||
},
|
||||
"node_modules/mime-types": {
|
||||
"version": "2.1.35",
|
||||
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
|
||||
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"mime-db": "1.52.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.6"
|
||||
}
|
||||
},
|
||||
"node_modules/proxy-from-env": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
|
||||
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==",
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
}
|
22
collect-currency/package.json
Normal file
22
collect-currency/package.json
Normal file
|
@ -0,0 +1,22 @@
|
|||
{
|
||||
"name": "collect-currency",
|
||||
"version": "1.0.0",
|
||||
"main": "save_fiat.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/Redume/Kekkai.git"
|
||||
},
|
||||
"author": "Redume",
|
||||
"license": "GPL-3.0-or-later",
|
||||
"bugs": {
|
||||
"url": "https://github.com/Redume/Kekkai/issues"
|
||||
},
|
||||
"homepage": "https://github.com/Redume/Kekkai#readme",
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"axios": "^1.7.2"
|
||||
}
|
||||
}
|
53
collect-currency/save_fiat.js
Normal file
53
collect-currency/save_fiat.js
Normal file
|
@ -0,0 +1,53 @@
|
|||
const pool = require('../database/postgresql.js');
|
||||
const yaml = require('yaml');
|
||||
const fs = require('fs');
|
||||
const axios = require('axios');
|
||||
const config = yaml.parse(fs.readFileSync('./config.yaml', 'utf-8'));
|
||||
|
||||
async function save_fiat() {
|
||||
if (!config['currency']['collecting']['fiat']) return;
|
||||
|
||||
config['currency']['fiat'].forEach(
|
||||
(value) => config['currency']['fiat'].forEach(async (pair) => {
|
||||
if(value !== pair) {
|
||||
const res = await axios.get(
|
||||
`https://duckduckgo.com/js/spice/currency/1/${value}/${pair}`,
|
||||
{
|
||||
timeout: 3000,
|
||||
}
|
||||
);
|
||||
|
||||
const regExp = new RegExp('\\(\\s*(.*)\\s*\\);$', 'mg');
|
||||
const data = JSON.parse(Array.from(res.data.matchAll(regExp))[0][1])
|
||||
console.log(data)
|
||||
|
||||
delete data['terms'];
|
||||
delete data['privacy'];
|
||||
|
||||
const point = data['to'][0]['mid'].toString().indexOf('.') + 4;
|
||||
|
||||
pool.query('SELECT * FROM currency WHERE from_currency = $1 AND conv_currency = $2 AND date = $3',
|
||||
[
|
||||
value,
|
||||
pair,
|
||||
new Date(data['timestamp']).toLocaleDateString()
|
||||
],
|
||||
).then(async (db) => {
|
||||
if (!db['rows'][0]) {
|
||||
await pool.query(`INSERT INTO currency (from_currency, conv_currency, rate, date)
|
||||
VALUES ($1, $2, $3, $4) RETURNING *`,
|
||||
[
|
||||
value,
|
||||
pair,
|
||||
data['to'][0]['mid'].toString().slice(0, point),
|
||||
new Date(data['timestamp']).toLocaleDateString()
|
||||
],
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = save_fiat;
|
Loading…
Add table
Reference in a new issue