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