-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathup_analytics.py
219 lines (185 loc) · 6.87 KB
/
up_analytics.py
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python
"""
************************************************************************
Playing with the UP API
************************************************************************
"""
__author__ = "Hamish Gunasekara"
__version__ = "1.0.1"
__email__ = "[email protected]"
import requests
import json
import psycopg2
import yaml
import sys
from datetime import datetime, timedelta
from configparser import ConfigParser
def connect_to_db():
# connect to db
con = psycopg2.connect(
host="upbankrds.cmk1szpqwflw.ap-southeast-2.rds.amazonaws.com",
database="upbank",
user="postgres",
password="hamish123")
# cursor
cur = con.cursor()
return cur, con
def connect_to_up(user_id):
config_info = open("config.yaml")
parsed_config_info = yaml.load(config_info, Loader=yaml.FullLoader)
API_KEY = parsed_config_info['config']['api_key'][user_id]
header = {'Authorization': 'Bearer ' + API_KEY}
return header
def test_ping(header):
test_ping = requests.get(
"https://api.up.com.au/api/v1/util/ping",
headers=header)
print("Status code = " + str(test_ping.status_code))
print("Response is = " + str(test_ping.json()))
def extract_latest_transactions(cur, con, header, user_id):
select_statement = "select created_at from d_transaction where user_id = '{}' order by created_at desc limit 1".format(user_id)
cur.execute(select_statement)
latest_dt = cur.fetchall()
latest_date = latest_dt[0][0] + timedelta(seconds=2)
transaction_params = {'page[size]': 100, 'filter[since]': latest_date}
extract_transactions(header, cur, con, transaction_params, user_id)
def upload_to_db(
transaction_id,
tran_type,
description,
message,
roundup,
roundup_value,
roundup_value_base,
amount_value,
amount_value_base,
created_at,
user_id,
account_id,
parent_category,
category,
cur,
con):
print("Uploading transaction from {} to the database".format(description))
cur.execute(
"insert into d_transaction (transaction_id, tran_type, description, message, roundup, roundup_value, roundup_value_base, amount_value, amount_value_base, created_at, user_id, account_id, parent_category, category) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
(transaction_id,
tran_type,
description,
message,
isinstance(
roundup,
dict),
roundup_value,
roundup_value_base,
amount_value,
amount_value_base,
created_at,
user_id,
account_id,
parent_category,
category))
# commit everything
con.commit()
print("Uploaded transaction from {}!".format(description))
def extract_transactions(header, cur, con, transaction_params, user_id):
tran_page_url = "https://api.up.com.au/api/v1/transactions"
while tran_page_url:
r = requests.get(
tran_page_url,
headers=header,
params=transaction_params)
current_page = json.loads(r.text)
data = current_page['data']
if r.status_code == 200:
print("Connected to Up API")
if not data:
print("Nothing to Upload")
else:
for info in current_page['data']:
transaction_id = info['id']
tran_type = info['type']
description = info['attributes']['description']
message = info['attributes']['message']
roundup = info['attributes']['roundUp']
if roundup:
roundup_value = info['attributes']['roundUp']['amount']['value']
roundup_value_base = info['attributes']['roundUp']['amount']['valueInBaseUnits']
else:
roundup_value = 0
roundup_value_base = 0
amount_value = info['attributes']['amount']['value']
amount_value_base = info['attributes']['amount']['valueInBaseUnits']
created_at = info['attributes']['createdAt']
account_id = info['relationships']['account']['data']['id']
if info['relationships']['parentCategory']['data']:
parent_category = info['relationships']['parentCategory']['data']['id']
category = info['relationships']['category']['data']['id']
else:
parent_category = ''
category = ''
# print("id is " + str(transaction_id))
# print("tran_type is " + str(tran_type))
# print("description is " + str(description))
# print("message is " + str(message))
# print("roundUp is " + str(roundup))
# print("value is " + str(amount_value))
# print("valueInBaseUnits is " + str(amount_value_base))
upload_to_db(
transaction_id,
tran_type,
description,
message,
roundup,
roundup_value,
roundup_value_base,
amount_value,
amount_value_base,
created_at,
user_id,
account_id,
parent_category,
category,
cur,
con)
# Go to next page
tran_page_url = current_page['links']['next']
def get_categories(header, cur, con, transaction_id):
tran_page_url = "https://api.up.com.au/api/v1/categories/{}".format(transaction_id)
r = requests.get(
tran_page_url,
headers=header)
current_page = json.loads(r.text)
print(current_page)
data = current_page['data']
if r.status_code == 200:
print("Connected to Up API")
if not data:
print("Transaction not found")
else:
info = current_page['data']
category_name = info['attributes']['name']
return category_name
return False
def close_db(cur, con):
# close the cursor
cur.close()
# close the connection
con.close()
# main function
def main():
if sys.argv:
user_id = sys.argv[0]
if user_id == 'hamish' or user_id == 'nina':
cur, con = connect_to_db()
header = connect_to_up(user_id)
transaction_params = {'page[size]': 100}
extract_transactions(header, cur, con, transaction_params, user_id)
close_db(cur, con)
else:
print("user_id is invalid")
else:
print("No user_id supplied")
if __name__ == "__main__":
# main()
print("main is commented out")