-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
115 lines (110 loc) · 3.64 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const request = require('sync-request');
const config = require("./config.js")
const fs = require("fs")
const ynab = require("ynab");
const accessToken = config.token;
const ynabAPI = new ynab.API(accessToken);
function getAllTransactions(page = 1){
var res = request('GET', `https://bank.hackclub.com/api/v3/organizations/${config.bankID}/transactions?per_page=100`, {
headers: {
'user-agent': 'HCB2YNAB/1.0',
},
qs: {
page: page
}
});
var json = JSON.parse(res.getBody("utf8"))
var cache = []
json.forEach(action=>{
if (action.type == "invoice") return
cache.push({
account_id: config.accountID,
date: action.date,
amount: action.amount_cents * 10,
memo: action.memo
})
})
console.log(cache)
ynabAPI.transactions.bulkCreateTransactions(config.ynabID, {transactions:cache}).catch(e=>console.error(e))
var next = request('GET', `https://bank.hackclub.com/api/v3/organizations/${config.bankID}/transactions?per_page=100`, {
headers: {
'user-agent': 'HCB2YNAB/1.0',
},
qs: {
page: page+1
}
})
if (JSON.parse(next.getBody("utf8")).length > 0)
getAllTransactions(page+1)
else {
var last = request('GET', `https://bank.hackclub.com/api/v3/organizations/${config.bankID}/transactions?per_page=100`, {
headers: {
'user-agent': 'HCB2YNAB/1.0',
},
qs: {
page: 1
}
})
fs.writeFileSync("./lastid", JSON.parse(last.getBody("utf8"))[0].id)
}
}
function getNew(page = 1){
console.log("✅ New sync completed!")
var lastid = fs.readFileSync("./lastid", "utf8")
var res = request('GET', `https://bank.hackclub.com/api/v3/organizations/${config.bankID}/transactions?per_page=100`, {
headers: {
'user-agent': 'HCB2YNAB/1.0',
},
qs: {
page: page
}
});
var json = JSON.parse(res.getBody("utf8"))
var cache = []
var done = false
json.forEach(action=>{
if (done) return
if (action.type == "invoice") return
if (action.id == lastid) { done = true; return }
cache.push({
account_id: config.accountID,
date: action.date,
amount: action.amount_cents * 10,
memo: action.memo
})
})
if (cache.length < 1) return
ynabAPI.transactions.bulkCreateTransactions(config.ynabID, {transactions:cache}).catch(e=>console.error(e))
var next = request('GET', `https://bank.hackclub.com/api/v3/organizations/${config.bankID}/transactions?per_page=100`, {
headers: {
'user-agent': 'HCB2YNAB/1.0',
},
qs: {
page: page+1
}
})
if (JSON.parse(next.getBody("utf8")).length > 0 && !done)
getNew(page+1)
else {
var last = request('GET', `https://bank.hackclub.com/api/v3/organizations/${config.bankID}/transactions?per_page=100`, {
headers: {
'user-agent': 'HCB2YNAB/1.0',
},
qs: {
page: 1
}
})
fs.writeFileSync("./lastid", JSON.parse(last.getBody("utf8"))[0].id)
}
}
(async function() {
if (!fs.existsSync("./lastid")) {
console.log("🪙 1st time user, pulling all transactions...")
getAllTransactions()
console.log("✅ Exiting. Restart the program to start the cronjob")
} else {
console.log("🔃 Starting cronjob to sync YNAB and HCB!")
getNew()
setInterval(getNew, 1000 * 60 * 10)
}
})();