From 874c7ac1f1810921a6237efc33241618b251b1dc Mon Sep 17 00:00:00 2001 From: Redume Date: Sat, 17 Feb 2024 14:17:13 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BF=D1=80=D0=BE=D0=BA=D1=81=D0=B8=20=D0=B8=20=D0=B8?= =?UTF-8?q?=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=20=D1=84=D0=BE=D1=80=D0=BC?= =?UTF-8?q?=D0=B0=D1=82=20=D0=B2=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config_sample.yaml | 9 ++++++++- utils/saveCourse.js | 35 ++++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/config_sample.yaml b/config_sample.yaml index c78fec3..ecc43c4 100644 --- a/config_sample.yaml +++ b/config_sample.yaml @@ -10,4 +10,11 @@ currency: - EUR - UAH - TRY - - KZT \ No newline at end of file + - KZT +proxy: + protocol: 'https' + host: '127.0.0.1' + port: 9000 + auth: + username: 'mikeymike' + password: 'rapunz3l' \ No newline at end of file diff --git a/utils/saveCourse.js b/utils/saveCourse.js index 96b1c2d..af41d0d 100644 --- a/utils/saveCourse.js +++ b/utils/saveCourse.js @@ -2,29 +2,42 @@ const pool = require("../postgresql.js"); const yaml = require("yaml") const fs = require("fs"); const axios = require("axios"); +const {AxiosProxyConfig} = require("axios"); const config = yaml.parse(fs.readFileSync("./config.yaml", "utf-8")); -async function saveCourse() { +async function saveRate() { config['currency'].forEach( (value) => config['currency'].forEach(async (pair) => { if(value !== pair) { const res = await axios.get( `https://duckduckgo.com/js/spice/currency/1/${value}/${pair}`, { - timeout: 3000 + timeout: 3000, } - ) - const data = JSON.parse(res.data.replace('ddg_spice_currency(', '').replace(');', '')) + ); + + if (!config['proxy']['host']) { + res.config.proxy = { + protocol: config['proxy']['protocol'], + host: config['proxy']['host'], + port: config['proxy']['port'], + auth: { + username: config['proxy']['auth']['username'], + password: config['proxy']['auth']['password'], + } + } + } + + const data = JSON.parse(res.data.replace('ddg_spice_currency(', '').replace(');', '')); delete data['terms']; delete data['privacy']; - console.log(data) - const point = data['to'][0]['mid'].toString().indexOf('.') + 4 + const point = data['to'][0]['mid'].toString().indexOf('.') + 4; pool.query('SELECT * FROM currency WHERE from_currency = $1 AND date = $2', [ value, - data['timestamp'] + new Date(data['timestamp']).toLocaleDateString() ] ).then(async (db) => { if (!db['rows'][0]) { @@ -34,14 +47,14 @@ async function saveCourse() { value, pair, data['to'][0]['mid'].toString().slice(0, point), - data['timestamp'] + new Date(data['timestamp']).toLocaleDateString() ] - ) + ); } - }) + }); } }) ); } -module.exports = saveCourse; \ No newline at end of file +module.exports = saveRate; \ No newline at end of file