-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarling_to_sheets.py
More file actions
175 lines (154 loc) · 5.89 KB
/
Copy pathstarling_to_sheets.py
File metadata and controls
175 lines (154 loc) · 5.89 KB
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
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.clear()
worksheet.freeze(rows=1)
worksheet.update(range_name='A1',
values=data)
worksheet.format(ranges="1:1", format={
"textFormat": {
"bold": True
},
"horizontalAlignment": "CENTER",
})
worksheet.format(ranges="B:B", format={
"numberFormat": {
"type": "CURRENCY",
}
})
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', 0) / 100,
space.get('state', "")
])
print(f"Adding saving space data to sheet...")
worksheet.clear()
worksheet.freeze(rows=1)
worksheet.update(range_name='A1',
values=data)
worksheet.format(ranges="1:1", format={
"textFormat": {
"bold": True
},
"horizontalAlignment": "CENTER",
})
worksheet.format(ranges=["C:C", "E:E"], format={
"numberFormat": {
"type": "CURRENCY",
}
})
worksheet.format(ranges="F:F", format={
"numberFormat": {
"type": "PERCENT",
}
})
def update_balance_cells(account, balance, workbook):
print(f"Writing balance data 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)
effective_balance = balance['effectiveBalance']['minorUnits'] / 100
total_effective_balance = balance['totalEffectiveBalance']['minorUnits'] / 100
worksheet.update(range_name='P1:Q2', values=[
["Effective Balance", effective_balance],
["Total Effective Balance", total_effective_balance],
])
worksheet.format(ranges="Q1:Q2", format={
"numberFormat": {
"type": "CURRENCY",
}
})
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)
account = starling.get_accounts()[0]
main_account = account['accountUid']
account_transactions = starling.get_transaction_feed(main_account, account['defaultCategory'])
account_spaces = starling.get_saving_spaces(main_account)
account_balance = starling.get_balance(main_account)
update_transaction_sheet(starling_account, account_transactions, finance_workbook)
update_saving_spaces_sheet(starling_account, account_spaces, finance_workbook)
update_balance_cells(starling_account, account_balance, finance_workbook)
print(f"Successfully finished updating {starling_account.lower()} account!")