-
Notifications
You must be signed in to change notification settings - Fork 2
/
ynabemail.py
120 lines (91 loc) · 4.25 KB
/
ynabemail.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
#!/usr/bin/python3.5
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from pynYNAB.Client import nYnabClient
from pynYNAB.connection import nYnabConnection
from pynYNAB.schema.budget import Payee, Transaction
import datetime
import pickle
import os.path
import settings
from collections import defaultdict
def sendemail(from_addr, to_addr_list, cc_addr_list,
subject, message,
login, password,
smtpserver='smtp.gmail.com:587'):
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = ','.join(to_addr_list)
htmlmessage = '<html><head></head><body>'+message+'</body></html>'
part1 = MIMEText(message, 'plain')
part2 = MIMEText(htmlmessage, 'html')
msg.attach(part1)
msg.attach(part2)
server = smtplib.SMTP(smtpserver)
server.starttls()
server.login(login,password)
problems = server.sendmail(from_addr, to_addr_list, msg.as_string())
server.quit()
return problems
class Bal():
balance = 0
def main():
ynabUser = settings.ynab_user
ynabPassword = settings.ynab_password
ynabBudgetName = settings.ynab_budgetname
print('Getting YNAB info')
connection = nYnabConnection(ynabUser, ynabPassword)
connection.init_session()
client = nYnabClient(nynabconnection=connection, budgetname=ynabBudgetName)
cats = {}
subs = {}
balances = defaultdict(Bal)
if os.path.isfile('balances.p'):
old_balances = pickle.load( open( "balances.p", "rb" ) )
if type(old_balances) is not defaultdict:
old_balances = defaultdict(Bal, old_balances)
else:
old_balances = defaultdict(Bal)
#Creates hiarichy structure of category/subcategory and only those that have the keyword in YNAB subcategory notes section
for cat in client.budget.be_master_categories:
cats[cat.name]=cat
subs[cat.name+'_subs'] = {}
for subcat in client.budget.be_subcategories:
if subcat.entities_master_category_id == cat.id:
subs[cat.name+'_subs'][subcat.name] = subcat
#Gets current month budget calculations
for b in client.budget.be_monthly_subcategory_budget_calculations:
if b.entities_monthly_subcategory_budget_id[4:11]==(datetime.datetime.now().strftime('%Y-%m')):
balances[b.entities_monthly_subcategory_budget_id[12:]]=b
#print(b.entities_monthly_subcategory_budget_id[12:]+': ' + str(b.balance))
#Displays the balance for each subcategory in the subs dict
bal_str = '<p>'
for cat in cats:
if 'Internal' not in cat:
if len(subs[cat+'_subs'])>0:
bal_str += '<b>'+cat+'</b> <br>'
for scat in subs[cat+"_subs"]:
#print(cat + ' - ' + scat)
bal_str += ' '+ scat + ': ' + str(balances[subs[cat+"_subs"][scat].id].balance)
bal_diff = balances[subs[cat+"_subs"][scat].id].balance - old_balances[subs[cat+"_subs"][scat].id].balance
bal_diff = round(bal_diff,2)
if bal_diff:
if bal_diff > 0:
#Balance goes up
bal_str += " <span style='color:green'>$" + str(bal_diff) + " ↑</span>"
else:
#Balance went down
bal_str += " <span style='color:red'>$" + str(abs(bal_diff)) + " ↓</span>"
bal_str += '<br>'
print('Sending Email')
sendemail(settings.from_address, settings.to_list, '',
'YNAB Balances for ' + datetime.datetime.now().strftime('%x'), bal_str, settings.gmail_user, settings.gmail_password, 'smtp.gmail.com:587')
print('Saving balances')
pickle.dump( balances, open( "balances.p", "wb" ) )
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
quit()