forked from davidschlachter/firefly-iii-email-summary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monthly-report.py
executable file
·145 lines (141 loc) · 6.69 KB
/
monthly-report.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
#!/usr/bin/env python3
import yaml
import sys
import traceback
import datetime
import requests
import re
import bs4
import ssl
import smtplib
import os
from email.message import EmailMessage
from email.headerregistry import Address
from email.utils import make_msgid
def main():
#
# Make the working path the same path as where our source is located
os.chdir(os.path.dirname(sys.argv[0]))
#
# Load configuration
with open('config.yaml', 'r') as configFile:
try:
config = yaml.safe_load(configFile)
except:
traceback.print_exc()
print("ERROR: could not load config.yaml")
sys.exit(1)
#
# Determine the applicable date range: the previous month
today = datetime.date.today()
endDate = today.replace(day=1) - datetime.timedelta(days=1)
startDate = endDate.replace(day=1)
monthName = startDate.strftime("%B")
#
# Set us up for API requests
HEADERS = {'Authorization': 'Bearer {}'.format(config['accesstoken'])}
with requests.Session() as s:
s.headers.update(HEADERS)
#
# Get all the categories
url = config['firefly-url'] + '/api/v1/categories'
categories = s.get(url).json()
#
# Get the spent and earned totals for each category
totals = []
for category in categories['data']:
url = config['firefly-url'] + '/api/v1/categories/' + category['id'] + '?start=' + startDate.strftime('%Y-%m-%d') + '&end=' + endDate.strftime('%Y-%m-%d')
r = s.get(url).json()
categoryName = r['data']['attributes']['name']
try:
categorySpent = r['data']['attributes']['spent'][0]['sum']
except (KeyError, IndexError):
categorySpent = 0
try:
categoryEarned = r['data']['attributes']['earned'][0]['sum']
except (KeyError, IndexError):
categoryEarned = 0
categoryTotal = float(categoryEarned) + float(categorySpent)
totals.append( {'name': categoryName, 'spent': categorySpent, 'earned': categoryEarned, 'total': categoryTotal} )
#
# Get general information
monthSummary = s.get(config['firefly-url'] + '/api/v1/summary/basic' + '?start=' + startDate.strftime('%Y-%m-%d') + '&end=' + endDate.strftime('%Y-%m-%d')).json()
yearToDateSummary = s.get(config['firefly-url'] + '/api/v1/summary/basic' + '?start=' + startDate.strftime('%Y') + '-01-01' + '&end=' + endDate.strftime('%Y-%m-%d')).json()
currency = config.get('currency', None)
if currency:
currencyName = currency
else:
for key in monthSummary:
if re.match(r'spent-in-.*', key):
currencyName = key.replace("spent-in-", "")
spentThisMonth = float(monthSummary['spent-in-'+currencyName]['monetary_value'])
earnedThisMonth = float(monthSummary['earned-in-'+currencyName]['monetary_value'])
netChangeThisMonth = float(monthSummary['balance-in-'+currencyName]['monetary_value'])
spentThisYear = float(yearToDateSummary['spent-in-'+currencyName]['monetary_value'])
earnedThisYear = float(yearToDateSummary['earned-in-'+currencyName]['monetary_value'])
netChangeThisYear = float(yearToDateSummary['balance-in-'+currencyName]['monetary_value'])
netWorth = float(yearToDateSummary['net-worth-in-'+currencyName]['monetary_value'])
#
# Set up the categories table
categoriesTableBody = '<table><tr><th>Category</th><th style="text-align: right;">Total</th></tr>'
#categoriesTableBody = '<table><tr><th>Category</th><th>Spent</th><th>Earned</th><th>Total</th></tr>'
for category in totals:
categoriesTableBody += '<tr><td style="padding-right: 1em;">'+category['name']+'</td><td style="text-align: right;">'+str(round(float(category['total']))).replace("-", "−")+'</td></tr>'
#categoriesTableBody += '<tr><td>'+category['name']+'</td><td>'+str(round(float(category['spent'])))+'</td><td>'+str(round(float(category['earned'])))+'</td><td>'+str(round(float(category['total'])))+'</td></tr>'
categoriesTableBody += '</table>'
#
# Set up the general information table
generalTableBody = '<table>'
generalTableBody += '<tr><td>Spent this month:</td><td style="text-align: right;">' + str(round(spentThisMonth)).replace("-", "−") + '</td></tr>'
generalTableBody += '<tr><td>Earned this month:</td><td style="text-align: right;">' + str(round(earnedThisMonth)).replace("-", "−") + '</td></tr>'
generalTableBody += '<tr style="border-bottom: 1px solid black"><td>Net change this month:</td><td style="text-align: right;">' + str(round(netChangeThisMonth)).replace("-", "−") + '</td></tr>'
generalTableBody += '<tr><td>Spent so far this year:</td><td style="text-align: right;">' + str(round(spentThisYear)).replace("-", "−") + '</td></tr>'
generalTableBody += '<tr><td>Earned so far this year:</td><td style="text-align: right;">' + str(round(earnedThisYear)).replace("-", "−") + '</td></tr>'
generalTableBody += '<tr style="border-bottom: 1px solid black"><td style="padding-right: 1em;">Net change so far this year:</td><td style="text-align: right;">' + str(round(netChangeThisYear)).replace("-", "−") + '</td></tr>'
generalTableBody += '<tr><td>Current net worth:</td><td style="text-align: right;">' + str(round(netWorth)).replace("-", "−") + '</td></tr>'
generalTableBody +='</table>'
#
# Assemble the email
msg = EmailMessage()
msg['Subject'] = "Firefly III: Monthly report"
msg['From'] = "monthly-report <" + config['email']['from'] + ">"
msg['To'] = ( tuple(config['email']['to']) )
htmlBody = """
<html>
<head>
<style>table{{border-collapse: collapse; border-top: 1px solid black; border-bottom: 1px solid black;}} th {{border-bottom: 1px solid black; padding: 0.33em 1em 0.33em 1em;}} td{{padding: .1em;}} tr:nth-child(even) {{background: #EEE}} tr:nth-child(odd) {{background: #FFF}}</style>
</head>
<body>
<p>Monthly report for {monthName} {year}:</p>
{categoriesTableBody}
<p>General information:</p>
{generalTableBody}
</body>
</html>
""".format( monthName=monthName, year=startDate.strftime("%Y"), categoriesTableBody=categoriesTableBody, generalTableBody=generalTableBody )
msg.set_content(bs4.BeautifulSoup(htmlBody, "html.parser").get_text()) # just html to text
msg.add_alternative(htmlBody, subtype='html')
#
# Set up the SSL context for SMTP if necessary
context = ssl.create_default_context()
#
# Send off the message
with smtplib.SMTP_SSL(host=config['smtp']['server'], port=config['smtp']['port']) as s:
if config['smtp']['starttls']:
s.ehlo()
try:
s.starttls(context=context)
except:
traceback.print_exc()
print("ERROR: could not connect to SMTP server with STARTTLS")
sys.exit(2)
if config['smtp']['authentication']:
try:
s.login(user=config['smtp']['user'], password=config['smtp']['password'])
except:
traceback.print_exc()
print("ERROR: could not authenticate with SMTP server.")
sys.exit(3)
s.send_message(msg)
if __name__ == "__main__":
main()