-
Notifications
You must be signed in to change notification settings - Fork 0
/
finances.py
96 lines (78 loc) · 3.01 KB
/
finances.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
import mintapi
import csv
import re
import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from lib.functions import *
import pprint
######################################################
#############GLOBAL VARIABLES ########################
SPREADSHEET_NAME = "TRANSACTIONS"
SHEET_NAME = "transactions"
LIB_PATH = "./lib/"
SCOPE = ["https://spreadsheets.google.com/feeds"
,'https://www.googleapis.com/auth/spreadsheets'
,"https://www.googleapis.com/auth/drive.file"
,"https://www.googleapis.com/auth/drive"]
COLS = ['Date', 'Original Description', 'Amount', 'Transaction Type']
CREDS = ServiceAccountCredentials \
.from_json_keyfile_name('{}client_secret.json'.format(LIB_PATH), SCOPE)
CLIENT = gspread.authorize(CREDS)
print("Creating google sheets api connection")
SPREADSHEET = CLIENT.open(SPREADSHEET_NAME)
SHEET = SPREADSHEET.worksheet(SHEET_NAME)
pp = pprint.PrettyPrinter()
######################################################
def mintParams():
'''
concats username and password params
with default mintparams
'''
mintparams = {
'mfa_method':'sms'
,'headless': True
, 'mfa_input_callback': None
, 'session_path': None
, 'imap_account': None
, 'imap_password': None
, 'imap_server': None
, 'imap_folder':'INBOX'
, 'wait_for_sync': False
, 'wait_for_sync_timeout': 300
}
upw = eval(openFile(path=LIB_PATH, file='password.pw'))
mintparams.update(upw)
return mintparams
def tempTransactions():
df = pd.read_csv('.temp_transactions.csv', delimiter=",")[:-1]
df['uuid'] = df[COLS].apply(lambda row: create_sudo_id_field(row), axis=1)
return df
def sheetsTransactions():
df = pd.DataFrame(SHEET.get_all_records())
df['uuid'] = df[COLS].apply(lambda row: create_sudo_id_field(row), axis=1)
return df
def joinDFs(mintdf, sheetsdf):
sheetsdf = sheetsdf[['uuid', 'category']].rename(columns={'uuid': 'sheets_uuid'})
joindf = mintdf.merge(sheetsdf, how='left', left_on='uuid', right_on='sheets_uuid')
joindf = joindf[joindf['sheets_uuid'].isnull()].drop(columns='sheets_uuid')
#pp.pprint(joindf)
return joindf
def insertNewData(mintdf, sheetsdf, row_num):
rows_list = joinDFs(mintdf, sheetsdf).values.tolist()
vals = [[replace_nans(r) for r in row] for row in rows_list]
SPREADSHEET.values_update('transactions!A{}'.format(row_num)
, params={'valueInputOption': 'RAW'}
, body={'values': vals}
)
def main():
print("Creating mint api connection")
mint = mintapi.Mint(**mintParams())
parse_and_write_transactions(mint.get_transactions_csv())
mintdf, sheetsdf = tempTransactions(), sheetsTransactions()
#pp.pprint(mintdf)
#pp.pprint(sheetsdf)
insertNewData(mintdf, sheetsdf, len(sheetsdf) + 2)
update_col_format(SPREADSHEET, SHEET)
if __name__ == "__main__":
main()