From 565d20554e763b0855528b6f8ef1d21571f8391e Mon Sep 17 00:00:00 2001 From: "LAPTOP-8G8AGL8G\\yotam" Date: Mon, 30 Sep 2024 22:19:27 +0300 Subject: [PATCH 1/3] Add skeleton for centralized config loading --- main.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 0db0db7..5cf0385 100644 --- a/main.py +++ b/main.py @@ -2,13 +2,35 @@ from dotenv import load_dotenv from splitwise import Splitwise, Expense, User, Comment from splitwise.user import ExpenseUser -from typing import Generator +from typing import Generator, TypedDict import os import requests time_now = datetime.now().astimezone() +class Config(TypedDict): + FIREFLY_URL: str + FIREFLY_TOKEN: str + FIREFLY_DRY_RUN: bool + FIREFLY_DEFAULT_CATEGORY: str + FIREFLY_DEFAULT_SPEND_ACCOUNT: str + FIREFLY_DEFAULT_TRXFR_ACCOUNT: str + SPLITWISE_TOKEN: str + SPLITWISE_DAYS: int + +def load_config() -> Config: + load_dotenv() + return { + "SPLITWISE_TOKEN": os.getenv("SPLITWISE_TOKEN"), + "FIREFLY_URL": os.getenv("FIREFLY_URL"), + "FIREFLY_TOKEN": os.getenv("FIREFLY_TOKEN"), + "FIREFLY_DEFAULT_CATEGORY": os.getenv("FIREFLY_DEFAULT_CATEGORY", "Uncategorized"), + "FIREFLY_DEFAULT_SPEND_ACCOUNT": os.getenv("FIREFLY_DEFAULT_SPEND_ACCOUNT"), + "FIREFLY_DEFAULT_TRXFR_ACCOUNT": os.getenv("FIREFLY_DEFAULT_TRXFR_ACCOUNT"), + "FIREFLY_DRY_RUN": bool(os.getenv("FIREFLY_DRY_RUN"), True), + "SPLITWISE_DAYS": int(os.getenv("SPLITWISE_DAYS", 1)), + }, def formatExpense(exp: Expense, myshare: ExpenseUser) -> str: """ From 74846d1c80af1bcb92ce582f9129a8da172c8cf3 Mon Sep 17 00:00:00 2001 From: "LAPTOP-8G8AGL8G\\yotam" Date: Mon, 30 Sep 2024 22:36:47 +0300 Subject: [PATCH 2/3] Use new Config class --- main.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 5cf0385..4997a17 100644 --- a/main.py +++ b/main.py @@ -7,8 +7,6 @@ import os import requests -time_now = datetime.now().astimezone() - class Config(TypedDict): FIREFLY_URL: str FIREFLY_TOKEN: str @@ -23,15 +21,18 @@ def load_config() -> Config: load_dotenv() return { "SPLITWISE_TOKEN": os.getenv("SPLITWISE_TOKEN"), - "FIREFLY_URL": os.getenv("FIREFLY_URL"), + "FIREFLY_URL": os.getenv("FIREFLY_URL", "http://firefly:8080"), "FIREFLY_TOKEN": os.getenv("FIREFLY_TOKEN"), "FIREFLY_DEFAULT_CATEGORY": os.getenv("FIREFLY_DEFAULT_CATEGORY", "Uncategorized"), - "FIREFLY_DEFAULT_SPEND_ACCOUNT": os.getenv("FIREFLY_DEFAULT_SPEND_ACCOUNT"), - "FIREFLY_DEFAULT_TRXFR_ACCOUNT": os.getenv("FIREFLY_DEFAULT_TRXFR_ACCOUNT"), + "FIREFLY_DEFAULT_SPEND_ACCOUNT": os.getenv("FIREFLY_DEFAULT_SPEND_ACCOUNT", "Amex"), + "FIREFLY_DEFAULT_TRXFR_ACCOUNT": os.getenv("FIREFLY_DEFAULT_TRXFR_ACCOUNT", "Chase Checking"), "FIREFLY_DRY_RUN": bool(os.getenv("FIREFLY_DRY_RUN"), True), "SPLITWISE_DAYS": int(os.getenv("SPLITWISE_DAYS", 1)), }, +time_now = datetime.now().astimezone() +conf = load_config() + def formatExpense(exp: Expense, myshare: ExpenseUser) -> str: """ Format expense for logging. @@ -160,15 +161,15 @@ def callApi(path, method="POST", params={}, body={}, fail=True): :param fail: Whether to raise an exception on failure :return: The response object """ - baseUrl = os.getenv("FIREFLY_URL", "http://firefly:8080") - token = os.getenv("FIREFLY_TOKEN") + baseUrl = conf["FIREFLY_URL"] + token = conf["FIREFLY_TOKEN"] headers = { "Authorization": f"Bearer {token}", # https://github.com/firefly-iii/firefly-iii/issues/6829 "Accept": "application/json", } - if method != "GET" and os.getenv("FIREFLY_DRY_RUN"): + if method != "GET" and conf["FIREFLY_DRY_RUN"]: print(f"Skipping {method} call due to dry run.") res = requests.Response() res.status_code, res._content = 200, b"{}" @@ -318,8 +319,7 @@ def getExpenseTransactionBody(exp: Expense, myshare: ExpenseUser, data: list[str if len(data) > 0 and data[0]: category = data[0] else: - category = os.getenv("FIREFLY_DEFAULT_CATEGORY", - exp.getCategory().getName()) + category = conf["FIREFLY_DEFAULT_CATEGORY"] or exp.getCategory().getName() data = data[1:] if len(data) > 0 and data[0]: @@ -333,10 +333,9 @@ def getExpenseTransactionBody(exp: Expense, myshare: ExpenseUser, data: list[str source = data[0] else: if myshare.getPaidShare() != "0.0": - source = os.getenv("FIREFLY_DEFAULT_SPEND_ACCOUNT", "Amex") + source = conf["FIREFLY_DEFAULT_SPEND_ACCOUNT"] else: - source = os.getenv( - "FIREFLY_DEFAULT_TRXFR_ACCOUNT", "Chase Checking") + source = conf["FIREFLY_DEFAULT_TRXFR_ACCOUNT"] data = data[1:] notes = "" @@ -367,11 +366,11 @@ def getExpenseTransactionBody(exp: Expense, myshare: ExpenseUser, data: list[str Main function. Get Splitwise expenses after a date and process them - update or add transactions on Firefly. """ load_dotenv() - past_day = time_now - timedelta(days=int(os.getenv("SPLITWISE_DAYS", 1))) + past_day = time_now - timedelta(days=conf["SPLITWISE_DAYS"]) txns = getTransactionsAfter(past_day) - sw = Splitwise("", "", api_key=os.getenv("SPLITWISE_TOKEN")) + sw = Splitwise("", "", api_key=conf["SPLITWISE_TOKEN"]) currentUser = sw.getCurrentUser() print(f"User: {currentUser.getFirstName()}") print(f"From: {past_day}") From 20882cf41bead227e967757ad172c6175d60a1ae Mon Sep 17 00:00:00 2001 From: "LAPTOP-8G8AGL8G\\yotam" Date: Mon, 30 Sep 2024 22:45:57 +0300 Subject: [PATCH 3/3] Minor bugfixes --- main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 4997a17..3e941ed 100644 --- a/main.py +++ b/main.py @@ -26,9 +26,9 @@ def load_config() -> Config: "FIREFLY_DEFAULT_CATEGORY": os.getenv("FIREFLY_DEFAULT_CATEGORY", "Uncategorized"), "FIREFLY_DEFAULT_SPEND_ACCOUNT": os.getenv("FIREFLY_DEFAULT_SPEND_ACCOUNT", "Amex"), "FIREFLY_DEFAULT_TRXFR_ACCOUNT": os.getenv("FIREFLY_DEFAULT_TRXFR_ACCOUNT", "Chase Checking"), - "FIREFLY_DRY_RUN": bool(os.getenv("FIREFLY_DRY_RUN"), True), + "FIREFLY_DRY_RUN": bool(os.getenv("FIREFLY_DRY_RUN", True)), "SPLITWISE_DAYS": int(os.getenv("SPLITWISE_DAYS", 1)), - }, + } time_now = datetime.now().astimezone() conf = load_config() @@ -365,7 +365,6 @@ def getExpenseTransactionBody(exp: Expense, myshare: ExpenseUser, data: list[str """ Main function. Get Splitwise expenses after a date and process them - update or add transactions on Firefly. """ - load_dotenv() past_day = time_now - timedelta(days=conf["SPLITWISE_DAYS"]) txns = getTransactionsAfter(past_day)