-
Notifications
You must be signed in to change notification settings - Fork 0
/
booqfx.sh
executable file
·108 lines (91 loc) · 2.43 KB
/
booqfx.sh
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
#!/bin/bash
function echox { tput setaf $2; echo $1; tput setaf 0; }
function header {
echo "OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:2f1fe7b3-19fd-11e8-b173-14109fe83058
<OFX>
<SIGNONMSGSRSV1>
<SONRS>
<STATUS>
<CODE>0
<SEVERITY>INFO
<MESSAGE>SUCCESS
</STATUS>
<DTSERVER>$(date '+%Y%m%d%H%M%S')
<LANGUAGE>ENG
<INTU.BID>03101
</SONRS>
</SIGNONMSGSRSV1>
" > ~/Downloads/boobank.qfx
}
function footer {
echo "
</OFX>" >> ~/Downloads/boobank.qfx
}
function once_per_day {
# Run at most once per day (useful when crontab'ed)
LAST=$(tail -1 ~/.boobank_last_sync_date)
TODAY=$(date '+%Y-%m-%d')
if (( LAST == TODAY )); then
echox "ALREADY run today." 4
exit
fi
}
function connectivity {
# Leave time to wakeup from standby
RETRIES=4
until (( RETRIES-- == 0 )); do
/usr/local/bin/fping -c1 -t300 8.8.8.8 2>/dev/null 1>/dev/null
if (( $? == 0 )); then
break
fi
if (( RETRIES == 0 )); then
echox "NO network." 4
exit
fi
sleep 10
done
}
function accounts {
if [ ! -f ~/.boobank_accounts.json ]; then
echo "Retrieving accounts..."
/usr/local/bin/boobank ls -f json > ~/.boobank_accounts.json
cat ~/.boobank_accounts.json | jq '.[].id' | tr -d '"' > ~/.boobank_accountids.txt
fi
}
function transactions {
# Retrieve transactions for the selected accounts
cat ~/.boobank_accountids.txt | /usr/local/bin/parallel -j0 ~/bin/boohistory.sh
# Move credit card transactions at the end
sed -ie '/<CREDITCARDMSGSRSV1>/,/<\/CREDITCARDMSGSRSV1>/{1h;1!H;d;};$G' ~/Downloads/boobank.qfx
# Keep one bank and one credit card sections, or else Quicken will not import
gsed -Ezi \
-e 's@</BANKMSGSRSV1>[[:space:]]*<BANKMSGSRSV1>@\n\n@g' \
-e 's@</CREDITCARDMSGSRSV1>[[:space:]]*<CREDITCARDMSGSRSV1>@\n\n@g' ~/Downloads/boobank.qfx
}
function report {
# Report progress and update last download date
COUNT_ACCTS=$(cat ~/Downloads/boobank.qfx | grep ACCTID | uniq | wc -l | tr -d " ")
COUNT_TRANS=$(cat ~/Downloads/boobank.qfx | grep TRNAMT | wc -l | tr -d " ")
if (( COUNT_TRANS > 0 )); then
date '+%Y-%m-%d' >> ~/.boobank_last_sync_date
echox "$COUNT_ACCTS account(s)." 3
echox "$COUNT_TRANS transaction(s) downloaded." 2
else
echox "NO transaction avalaible." 4
fi
}
#once_per_day
#connectivity
accounts
header
transactions
footer
report