добавлена поддержка прокси и изменен формат времени

This commit is contained in:
Данил 2024-02-17 14:17:13 +03:00
parent 61fc956e19
commit 874c7ac1f1
2 changed files with 32 additions and 12 deletions

View file

@ -10,4 +10,11 @@ currency:
- EUR - EUR
- UAH - UAH
- TRY - TRY
- KZT - KZT
proxy:
protocol: 'https'
host: '127.0.0.1'
port: 9000
auth:
username: 'mikeymike'
password: 'rapunz3l'

View file

@ -2,29 +2,42 @@ const pool = require("../postgresql.js");
const yaml = require("yaml") const yaml = require("yaml")
const fs = require("fs"); const fs = require("fs");
const axios = require("axios"); const axios = require("axios");
const {AxiosProxyConfig} = require("axios");
const config = yaml.parse(fs.readFileSync("./config.yaml", "utf-8")); const config = yaml.parse(fs.readFileSync("./config.yaml", "utf-8"));
async function saveCourse() { async function saveRate() {
config['currency'].forEach( config['currency'].forEach(
(value) => config['currency'].forEach(async (pair) => { (value) => config['currency'].forEach(async (pair) => {
if(value !== pair) { if(value !== pair) {
const res = await axios.get( const res = await axios.get(
`https://duckduckgo.com/js/spice/currency/1/${value}/${pair}`, `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['terms'];
delete data['privacy']; 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', pool.query('SELECT * FROM currency WHERE from_currency = $1 AND date = $2',
[ [
value, value,
data['timestamp'] new Date(data['timestamp']).toLocaleDateString()
] ]
).then(async (db) => { ).then(async (db) => {
if (!db['rows'][0]) { if (!db['rows'][0]) {
@ -34,14 +47,14 @@ async function saveCourse() {
value, value,
pair, pair,
data['to'][0]['mid'].toString().slice(0, point), data['to'][0]['mid'].toString().slice(0, point),
data['timestamp'] new Date(data['timestamp']).toLocaleDateString()
] ]
) );
} }
}) });
} }
}) })
); );
} }
module.exports = saveCourse; module.exports = saveRate;