firefly-exporter/budgetmail.py

40 lines
1.4 KiB
Python
Raw Normal View History

2020-10-26 09:42:45 +00:00
import calendar
import datetime
import firefly.budgets
def _get_remaining_days():
2020-10-31 14:06:48 +00:00
today = datetime.datetime.combine(datetime.date.today(), datetime.datetime.min.time())
2020-10-26 09:42:45 +00:00
endofmonth = today.replace(day=calendar.monthrange(today.year, today.month)[1])
2020-10-31 14:06:48 +00:00
return (endofmonth - today).days + 1
2020-10-26 09:42:45 +00:00
2020-10-31 14:06:48 +00:00
def _collect_budget_data():
2020-10-26 09:42:45 +00:00
budgets = []
for budget in firefly.budgets._get_budgets():
budgets.append({
"id": budget.get('id'),
"name": budget.get('attributes').get('name'),
"limit": firefly.budgets._get_current_limit(budget.get('id')).get('attributes').get('amount'),
2020-10-31 14:06:48 +00:00
"spent": firefly.budgets._get_current_spent_amount(budget.get('id')),
2020-10-26 09:42:45 +00:00
})
2020-10-31 14:06:48 +00:00
currbudget = budgets[-1]
currbudget['remaining'] = (float(currbudget.get('limit')) - float(currbudget.get('spent')))
remaining_per_day = currbudget.get('remaining') / _get_remaining_days()
currbudget['remaining_per_day'] = 0 if remaining_per_day < 0 else remaining_per_day
return budgets
def main():
for budget in _collect_budget_data():
2020-10-26 09:42:45 +00:00
print("Budget: " + budget.get('name'))
print(" Limit: " + budget.get('limit'))
2020-10-31 14:06:48 +00:00
print(" Spent: {:.2f}".format(budget.get('spent')))
print(" Remaining: {:.2f}".format(budget.get('remaining')))
print(" Remaining per day: {:.2f}".format(budget.get('remaining_per_day')))
2020-10-26 09:42:45 +00:00
if __name__ == "__main__":
main()