Compare commits
9 commits
trunk
...
budgetxmpp
Author | SHA1 | Date | |
---|---|---|---|
ee2a29467d | |||
330966369d | |||
12fd8e8885 | |||
74b69f3398 | |||
b46400aa3b | |||
04cdcec83e | |||
64c624e0a4 | |||
a9873ada47 | |||
ebcbe48317 |
5 changed files with 113 additions and 9 deletions
39
budgetmail.py
Normal file
39
budgetmail.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
import calendar
|
||||
import datetime
|
||||
|
||||
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():
|
||||
for budget in _collect_budget_data():
|
||||
print("Budget: " + budget.get('name'))
|
||||
print(" Limit: " + budget.get('limit'))
|
||||
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')))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
59
budgetxmpp.py
Normal file
59
budgetxmpp.py
Normal file
|
@ -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()
|
|
@ -3,6 +3,7 @@ import os
|
|||
import calendar
|
||||
import datetime
|
||||
|
||||
import pytz
|
||||
import requests
|
||||
import dateutil.parser
|
||||
|
||||
|
@ -12,9 +13,10 @@ host = os.environ.get('FIREFLY_API_HOST')
|
|||
|
||||
|
||||
def _get_budgets():
|
||||
budgets_json = requests.get(
|
||||
budgets_req = requests.get(
|
||||
host + "/api/v1/budgets", headers=header
|
||||
).json()
|
||||
)
|
||||
budgets_json = budgets_req.json()
|
||||
budgets = budgets_json.get('data')
|
||||
while "next" in budgets_json.get('links'):
|
||||
budgets_json = requests.get(
|
||||
|
@ -35,7 +37,7 @@ def _get_current_limit(budget_id):
|
|||
).json()
|
||||
budgets.extend(budget_json.get('data'))
|
||||
|
||||
today = datetime.datetime.combine(datetime.date.today(), datetime.datetime.min.time())
|
||||
today = datetime.datetime.combine(datetime.date.today(), datetime.datetime.min.time()).replace(tzinfo=pytz.UTC)
|
||||
return [
|
||||
budget for budget in budgets
|
||||
if today > dateutil.parser.parse(budget.get('attributes').get('start')) and today <= dateutil.parser.parse(budget.get('attributes').get('end'))
|
||||
|
@ -44,11 +46,13 @@ def _get_current_limit(budget_id):
|
|||
|
||||
def _get_current_transactions(budget_id):
|
||||
limit_id = _get_current_limit(budget_id).get('id')
|
||||
transactions_json = requests.get(
|
||||
host + "/api/v1/budgets/limits/{}/transactions".format(
|
||||
transactions_req = requests.get(
|
||||
host + "/api/v1/budgets/{}/limits/{}/transactions".format(
|
||||
budget_id,
|
||||
limit_id
|
||||
), headers=header
|
||||
).json()
|
||||
)
|
||||
transactions_json = transactions_req.json()
|
||||
transactions = transactions_json.get('data')
|
||||
while 'next' in transactions_json:
|
||||
transactions_json = requests.get(
|
||||
|
|
1
requirements-xmpp.txt
Normal file
1
requirements-xmpp.txt
Normal file
|
@ -0,0 +1 @@
|
|||
xmpppy==0.6.1
|
|
@ -1,3 +1,4 @@
|
|||
Flask==1.1.2
|
||||
python-dateutil==2.8.1
|
||||
requests==2.24.0
|
||||
Flask==2.1.2
|
||||
python-dateutil==2.8.2
|
||||
requests==2.28.1
|
||||
pytz==2022.1
|
||||
|
|
Loading…
Reference in a new issue