Initial commit. Lots of messy functions but eventually each file will do different things for apple shortcuts to then send me a text.
This commit is contained in:
128
scrubbed_main.py
Normal file
128
scrubbed_main.py
Normal file
@ -0,0 +1,128 @@
|
||||
"""
|
||||
The output of this script should be in a format that can be sent as a text or added to a note.
|
||||
In Apple Shortcuts, do the following:
|
||||
* Create new shortcut
|
||||
* Add Shell Script action
|
||||
* In the text box put:
|
||||
source ~/Documents/tmp/.actualpy/bin/activate
|
||||
python ~/Documents/tmp/main.py
|
||||
|
||||
Select the following options:
|
||||
Shell: Bash
|
||||
Input: Input
|
||||
Pass Input: to stdin
|
||||
Run as Admin: unchecked
|
||||
|
||||
Then add the other actions that you need. In my case, I did "Send {Shell Script Result} to {Contact}"
|
||||
"""
|
||||
|
||||
|
||||
from actual import Actual
|
||||
from actual.queries import get_budgets, get_categories, get_category, get_transactions
|
||||
from dotenv import load_dotenv
|
||||
from datetime import datetime
|
||||
from moneyed import Money, USD
|
||||
from moneyed.l10n import format_money
|
||||
import json
|
||||
import re
|
||||
import pprint
|
||||
|
||||
pp=pprint.PrettyPrinter(indent=4)
|
||||
load_dotenv()
|
||||
TODAY = datetime.now()
|
||||
MONTH_DAY = TODAY.strftime("%Y%m")
|
||||
MONTHSTR = TODAY.strftime("%B")
|
||||
MONTHDAYDATE = datetime.strptime(MONTH_DAY, "%Y%m")
|
||||
|
||||
def main():
|
||||
main_dict = {}
|
||||
main_list = []
|
||||
with Actual(
|
||||
base_url=os.getenv('BASEURL')
|
||||
password=os.getenv('PASSWORD')
|
||||
encryption_password=None, # Optional: Password for the file encryption. Will not use it if set to None.
|
||||
file=os.getenv('FILE')
|
||||
) as actual:
|
||||
budget = get_budgets(actual.session)
|
||||
for b in budget:
|
||||
if b.month == int(MONTH_DAY):
|
||||
if b.amount > 0:
|
||||
cats = get_categories(actual.session)
|
||||
for cat in cats:
|
||||
if cat.id == b.category_id:
|
||||
# if cat.name == "Kid's Activities":
|
||||
formatted_amount = str(b.amount)[:-2] + "." + str(b.amount)[-2:]
|
||||
budgeted_amount = Money(formatted_amount, USD)
|
||||
main_dict[cat.name] = {"budgeted_amount": budgeted_amount, "amount_spent": "", "amount_left": ""}
|
||||
# main_dict = {"category": cat.name, "month": MONTHSTR, "budgeted_amount": budgeted_amount, "amount_spent": "", "amount_left": ""}
|
||||
category_transcations(sesh=actual.session, main_list=main_dict)
|
||||
|
||||
|
||||
def category_transcations(sesh, main_list):
|
||||
for keys, vals in main_list.items():
|
||||
this_month_trans = get_transactions(sesh, category=keys, start_date=MONTHDAYDATE)
|
||||
curr_sum = Money(0, USD)
|
||||
for ta in this_month_trans:
|
||||
tamount = str(ta.amount)[:-2] + "." + str(ta.amount)[-2:]
|
||||
total_sum = Money(tamount, USD)
|
||||
curr_sum += total_sum
|
||||
# print(f"curr_sum: {curr_sum} PLUS total_sum: {total_sum}")
|
||||
amount_list = curr_sum + vals["budgeted_amount"]
|
||||
vals["budgeted_amount"] = vals['budgeted_amount']
|
||||
vals["amount_spent"] = curr_sum
|
||||
vals["amount_left"] = amount_list
|
||||
|
||||
# sorted_items = sorted(main_list.items(), key=lambda item: item[1]['amount_left'])
|
||||
# sorted_nested_dict = dict(sorted_items)
|
||||
# pp.pprint(sorted_nested_dict)
|
||||
# pp.pprint(sorted_key_val)
|
||||
# sorted_data = sorted(main_list, key=lambda x: main_list[x]['amount_left'])
|
||||
# pp.pprint(sorted_data)
|
||||
# sort_budgets_for_notification(sorted_data)
|
||||
sort_budgets_for_notification(main_list)
|
||||
|
||||
def sort_budgets_for_notification(sorted_data):
|
||||
negative_budget = {}
|
||||
no_budget_left = {}
|
||||
some_budget_left = {}
|
||||
final_tuple = ()
|
||||
for categories, values in sorted_data.items():
|
||||
# for budget_type, amount in values.items():
|
||||
intmoney = values['amount_left'].amount
|
||||
if intmoney == 0:
|
||||
# no_budget_left.append((categories, values) )
|
||||
no_budget_left[categories] = values
|
||||
elif intmoney < 0:
|
||||
# negative_budget.append((categories, values))
|
||||
negative_budget[categories] = values
|
||||
else:
|
||||
# some_budget_left.append(( categories, values) )
|
||||
some_budget_left[categories] = values
|
||||
|
||||
final_tuple = (format_dicts(some_budget_left), format_dicts(negative_budget), format_dicts(no_budget_left))
|
||||
print(f"""**This is an automated message!** Here's your spending status so far for this month.\nFirst, here are the categories where you've overspent:\n{final_tuple[1]}.\n\nHere is where you still have some budget left:\n{final_tuple[0]}.\n\nAnd finally, here is where you're exactly where you need to be. $0 left.\n {final_tuple[2]}""")
|
||||
|
||||
|
||||
def format_dicts(budgets_sorted):
|
||||
x = str(budgets_sorted)
|
||||
# y = re.search(r"(\'\w*\':)|(\'\w{1,9}\s\w{1,9})|(\d*.\d{2})", x).group()
|
||||
y = (x
|
||||
.replace("{'", "")
|
||||
.replace("}", "")
|
||||
.replace("Money('", "")
|
||||
.replace("'", "")
|
||||
.replace(", USD", "")
|
||||
.replace(")","")
|
||||
.replace("(","")
|
||||
.replace('"', '')
|
||||
)
|
||||
z = re.sub(r"((([A-Z][a-z]*( | & )){1,2}[A-Z][a-z]*:)|([A-Z][a-z]*)|529 Contrib)", '\n\\1', y)
|
||||
t = re.sub(r", \n", '\n', z)
|
||||
h = re.sub(r'([a-z]*_[a-z]*)',lambda x: x[1].replace('_', ' ').capitalize(), t)
|
||||
p = re.sub(r'(\d{1,5}.\d{2})', '$\\1', h)
|
||||
o = re.sub(r'(\d{4})', lambda x: x[1][:1]+','+x[1][1:], p)
|
||||
return o
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user