Finalized the this month script, which will be sent weekly via text to me and necessary parties. The last month and new_month_check transactions are also cleaned up and working. The next tasks are to sunset the import of moneyed as reading the actual-py docs, I found that I can do transactions.get_amount() which will return a decimal formatted number instead of needing to change the strings, etc. Will also need to clean up the files so they are more organized for public release.

This commit is contained in:
norm
2025-11-11 09:47:47 -05:00
parent b1d6aab9e3
commit b70fcbb185
10 changed files with 119 additions and 152 deletions

View File

@ -13,9 +13,14 @@ import pprint
pp=pprint.PrettyPrinter(indent=4, sort_dicts=False)
load_dotenv()
THISMONTH = datetime.now()
# datestring= "2025-08-06 13:57:57"
# format = '%Y-%m-%d %H:%M:%S'
# THISMONTH = datetime.strptime(datestring, format)
MONTHMINUSONE = THISMONTH - relativedelta(months=1)
MONTHMINUSTWO = THISMONTH - relativedelta(months=2)
ONBUDGETACCOUNTS = os.getenv('ONBUDGETACCOUNTS')
LASTM_STRING = datetime.strftime(MONTHMINUSONE, '%B')
TWOM_STRING = datetime.strftime(MONTHMINUSTWO, '%B')
ONBUDGETACCOUNTS = list(os.getenv('ONBUDGETACCOUNTS'))
def compare_months(actual):
budget_one = main(actual, MONTHMINUSONE)
@ -23,7 +28,18 @@ def compare_months(actual):
x = find_percent_diff(budget_one, budget_two)
first_sort = dict(sorted(budget_one.items(), key=lambda item: item[1]['amount_spent']))
reduced_sort = dict(list(first_sort.items())[:5])
print(x)
text_list = []
for i, k in reduced_sort.items():
items = (i, format_money(k['amount_spent']))
text_list.append(items)
formatted = str(text_list)[1:-1]
replacements = [('\\(', ''), ('\\)',''), ("'", ''), ('"',''), ('(\\d)\\,\\s\\b', '\\1\\n'), ('\\b, (\\-)', ': \\1')]
for old, new in replacements:
formatted = re.sub(fr"{old}", fr"{new}", formatted)
x = x.replace('"','')
print(f"""**This is an automated message!**\nHere's your budget status. Here are your top 5 spending categories from last month:\n\n{formatted}\n\nAnd here are the top 5 categories with the biggest increase in spending between last month and the previous month:\n\n{x}""")
def find_percent_diff(lastmonth, twomonths):
@ -34,10 +50,23 @@ def find_percent_diff(lastmonth, twomonths):
absolute = v['amount_spent'] - x['amount_spent']
average = (v['amount_spent'] + x['amount_spent'])/2
diff = round(absolute/average, 2)
diffs.append(( k, diff) )
diffs.append((k, diff, (LASTM_STRING, v['amount_spent']), (TWOM_STRING, x['amount_spent'])))
sort = sorted(diffs, reverse=True, key=lambda item: item[1])
return sort
first = sort[:5]
second = []
for t in first:
t1 = str(f"{float(t[1])}%")
t2 = (t[2][0], format_money(t[2][1]))
t3 = (t[3][0], format_money(t[3][1]))
newlist = f"{t[0]}, {t1}, {t2}, {t3}"
second.append(newlist)
formatted = str(second)[1:-1]
replacements = [('\\(', ''), ('\\)',''), ("'", ''), ('", ','\\n'), ('(\\%), ','\\1 --> '), ('(\\d)\\,\\s\\b', '\\1 --> '), ('([a-z]),', '\\1:')]
for old, new in replacements:
formatted = re.sub(fr"{old}", fr"{new}", formatted)
return formatted
def simple_track(actual):