mirror of
https://github.com/Redume/Kekkai.git
synced 2025-02-23 20:51:25 +03:00
теперь данные добавляются в бд
This commit is contained in:
parent
fa432e1f5b
commit
61fc956e19
1 changed files with 29 additions and 6 deletions
|
@ -5,20 +5,43 @@ const axios = 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 saveCourse() {
|
||||||
|
|
||||||
let pairs = [];
|
|
||||||
let currencies = ["USD", "RUB", "UAH", "JDF"]
|
|
||||||
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
|
||||||
|
}
|
||||||
)
|
)
|
||||||
console.log(res.data)
|
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
|
||||||
|
|
||||||
|
pool.query('SELECT * FROM currency WHERE from_currency = $1 AND date = $2',
|
||||||
|
[
|
||||||
|
value,
|
||||||
|
data['timestamp']
|
||||||
|
]
|
||||||
|
).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),
|
||||||
|
data['timestamp']
|
||||||
|
]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = saveCourse;
|
module.exports = saveCourse;
|
Loading…
Add table
Reference in a new issue