diff --git a/budgetxmpp.py b/budgetxmpp.py new file mode 100644 index 0000000..4d1a529 --- /dev/null +++ b/budgetxmpp.py @@ -0,0 +1,59 @@ +import calendar +import datetime +import xmpp +import time +import os + +import firefly.budgets + + +def _get_remaining_days(): + today = datetime.datetime.combine(datetime.date.today(), datetime.datetime.min.time()) + endofmonth = today.replace(day=calendar.monthrange(today.year, today.month)[1]) + return (endofmonth - today).days + 1 + + +def _collect_budget_data(): + 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'), + "spent": firefly.budgets._get_current_spent_amount(budget.get('id')), + }) + 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(): + jid = xmpp.protocol.JID(os.environ.get("FIREFLY_JABBER_ID")) + connection = xmpp.Client(server=jid.getDomain()) + connection.connect() + connection.auth( + user=jid.getNode(), + password=os.environ.get("FIREFLY_JABBER_PASSWORD"), + resource=jid.getResource(), + ) + + for budget in _collect_budget_data(): + message = "Budget: " + budget.get('name') + message += "\n Limit: " + budget.get('limit') + message += "\n Spent: {:.2f}".format(budget.get('spent')) + message += "\n Remaining: {:.2f}".format(budget.get('remaining')) + message += "\n Remaining per day: {:.2f}".format(budget.get('remaining_per_day')) + + connection.send( + xmpp.protocol.Message( + to=os.environ.get('FIREFLY_JABBER_RECEIVER'), + body=message, + ), + ) + time.sleep(1) + + +if __name__ == "__main__": + main() diff --git a/requirements-xmpp.txt b/requirements-xmpp.txt new file mode 100644 index 0000000..c376d82 --- /dev/null +++ b/requirements-xmpp.txt @@ -0,0 +1 @@ +xmpppy==0.6.1