-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarling_to_sheets.py
120 lines (103 loc) · 4.18 KB
/
starling_to_sheets.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
import os
import gspread
import variables
from starling import Starling
def update_transaction_sheet(account, transactions, workbook):
print(f"Accessing sheet for {account.lower()} account...")
try:
worksheet = workbook.worksheet(f"starling-{account.lower()}")
except gspread.exceptions.WorksheetNotFound:
worksheet = workbook.add_worksheet(f"starling-{account.lower()}", 0, 0)
print(f"Formatting transaction data...")
data = [[
"Currency",
"Amount",
"Source Currency",
"Source Amount",
"Direction",
"Transaction Time",
"Source",
"Status",
"Counter Party Type",
"Counter Party Name",
"Reference",
"Country",
"Spending Category",
"Has Attachment",
"Has Receipt"
]]
for transaction in transactions:
data.append([
transaction.get('amount', "").get('currency', ""),
transaction.get('amount', "").get('minorUnits', 0) / 100,
transaction.get('sourceAmount', "").get('currency', ""),
transaction.get('sourceAmount', "").get('minorUnits', 0) / 100,
transaction.get('direction', ""),
transaction.get('transactionTime', ""),
transaction.get('source', ""),
transaction.get('status', ""),
transaction.get('counterPartyType', ""),
transaction.get('counterPartyName', ""),
transaction.get('reference', ""),
transaction.get('country', ""),
transaction.get('spendingCategory', ""),
transaction.get('hasAttachment', ""),
transaction.get('hasReceipt', ""),
])
print(f"Adding transactions to sheet...")
worksheet.update(range_name='A1',
values=data)
def update_saving_spaces_sheet(account, spaces, workbook):
print(f"Accessing saving spaces sheet for {account.lower()} account...")
try:
worksheet = workbook.worksheet(f"starling-spaces-{account.lower()}")
except gspread.exceptions.WorksheetNotFound:
worksheet = workbook.add_worksheet(f"starling-spaces-{account.lower()}", 0, 0)
print(f"Formatting saving spaces data...")
data = [[
"Space",
"Target Currency",
"Target",
"Total Saved Currency",
"Total Saved",
"Saved Percentage",
"State"
]]
for space in spaces['savingsGoalList']:
space_data = [
space.get('name', "")
]
if space.get('target'):
space_data.append(space.get('target', "").get('currency', ""))
space_data.append(space.get('target', "").get('minorUnits', 0) / 100)
else:
space_data.append("")
space_data.append("")
data.append(space_data + [
space.get('totalSaved', "").get('currency', ""),
space.get('totalSaved', "").get('minorUnits', 0) / 100,
space.get('savedPercentage', ""),
space.get('state', "")
])
print(f"Adding saving space data to sheet...")
worksheet.update(range_name='A1',
values=data)
if __name__ == '__main__':
print(f"Connecting to Google Sheets...")
finance_workbook = gspread.service_account(filename="service_account.json").open("Finance")
for starling_account in variables.accounts:
print(f"Checking for {starling_account.lower()} access token...")
access_token = f'{starling_account}_ACCESS_TOKEN'
if access_token not in os.environ:
print("Not found, skipping...")
continue
print(f"Getting data from {starling_account.lower()} account...")
starling = Starling(
os.getenv(access_token),
sandbox=False)
main_account = starling.get_accounts()[0]['accountUid']
account_transactions = starling.get_transaction_feed(main_account)
account_spaces = starling.get_saving_spaces(main_account)
update_transaction_sheet(starling_account, account_transactions, finance_workbook)
update_saving_spaces_sheet(starling_account, account_spaces, finance_workbook)
print(f"Successfully finished updating {starling_account.lower()} account!")