-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·69 lines (61 loc) · 2.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env node
"use strict";
const db = require('sqlite');
const fetch = require('node-fetch');
module.exports = options => db.open(options.file)
/*eslint-disable fp/no-nil */
.then(() => {
try {
const url = "https://www.quandl.com/api/v3/datasets/BOE/XUDLGBD"
+ `?start_date=${options.startDate}&order=asc${options.quandlApiKey ? "&api_key=" + options.quandlApiKey : ""}`;
return fetch(url);
} catch(err) {
const logger = console.error(err);
return 2;
}
})
/*eslint-enable fp/no-nil */
.then(res => res.json())
.then(async function(body) {
const prices = body.dataset.data;
const [currencyGuid, commodityGuid] = await Promise.all([
db.get("SELECT guid FROM commodities WHERE mnemonic = ?", options.currency),
db.get("SELECT guid FROM commodities WHERE mnemonic = ?", options.commodity),
])
.then(res => res.map(v => v.guid))
.catch(err => console.error(err));
const rows = prices.map(d => {
const [date, price] = d;
const priceDenom = +("1" + Array(price.toString().split(".")[1].length).fill(0).join(""));
return {
$guid: getGnuCashGUID(),
$commodity_guid: commodityGuid,
$currency_guid: currencyGuid,
$date: date.replace(/-/g, "") + "230000",
$source: "user:price-editor",
$type: "last",
$value_num: price * priceDenom,
$value_denom: priceDenom
}
});
return Promise.all(rows.map(async function(row) {
const match = await db.get("SELECT * FROM prices WHERE date = ?", row.$date);
return match
? db.run(`UPDATE prices
SET value_num = ?, value_denom = ?
WHERE date = ?`, [row.$value_num, row.$value_denom, row.$date])
: db.run(`INSERT INTO prices VALUES (${Object.keys(row).join(", ")})`, row);
}));
})
.then(() => db.close())
.catch(err => console.error(err));
function randomIndex(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getGnuCashGUID(len = 32){
const letters =['a','b', 'c', 'd','e','f','g','h','i', 'j', 'k', 'l', 'm', 'n','o','p','q', 'r','s','t','u','v','w','x','y','z'];
const chars = letters.concat(Array(10).fill("").map((val, i) => i));
return Array(len).fill("").reduce((guid, position) => {
return guid + chars[randomIndex(0, chars.length - 1)];
}, "");
}