diff --git a/main.py b/main.py new file mode 100644 index 0000000..29ce1aa --- /dev/null +++ b/main.py @@ -0,0 +1,65 @@ +import aiohttp +import asyncio +import datetime +import os +import time + +from dotenv import load_dotenv + +from bring_api import Bring +from bring_api.exceptions import BringException + +load_dotenv() + +class BringPlugin: + SLEEP_INTERVAL = 15 * 60 # 15 minutes + + def __init__(self): + self.email = os.getenv('EMAIL') + self.password = os.getenv('PASSWORD') + self.webhook_url = os.getenv('WEBHOOK_URL') + + async def grabItems(self): + itemObjs = (await self.bring.get_list(self.list)).items.purchase + items = [item.itemId for item in itemObjs] + print(f"Items fetched: {items}") + return items + + async def sendItemsToTerminal(self, session, items): + try: + await session.post( + self.webhook_url, + json={'merge_variables': {'items': items}}, + headers={'Content-Type': 'application/json'}, + raise_for_status=True) + + current_timestamp = datetime.datetime.now().isoformat() + print(f"Items sent successfully to TRMNL at {current_timestamp}") + except Exception as e: + print(f"Exception occurred during sending items to TRMNL: {e}") + + async def run(self): + async with aiohttp.ClientSession() as session: + self.bring = Bring(session, self.email, self.password) + await self.bring.login() + + while True: + self.list = (await self.bring.load_lists()).lists[0].listUuid + items = await self.grabItems() + await self.sendItemsToTerminal(session, items) + time.sleep(self.SLEEP_INTERVAL) + + +if __name__ == "__main__": + bring_plugin = BringPlugin() + loop = asyncio.get_event_loop() + + while True: + try: + loop.run_until_complete(bring_plugin.run()) + except BringException as e: + print(f"Bring exception occured: {e}") + print(f"Retrying the service") + except Exception as e: + print(f"Unknowne exception occured: {e}") + raise \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9df64f6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,16 @@ +aiohappyeyeballs==2.4.6 +aiohttp==3.11.12 +aiosignal==1.3.2 +attrs==25.1.0 +bring-api==1.0.2 +frozenlist==1.5.0 +idna==3.10 +mashumaro==3.15 +multidict==6.1.0 +orjson==3.10.15 +pip3-autoremove==1.2.2 +propcache==0.2.1 +python-dotenv==1.0.1 +setuptools==75.8.0 +typing_extensions==4.12.2 +yarl==1.18.3