diff --git a/.env.example b/.env.example index 042e84c..2a49010 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,4 @@ EMAIL=email PASSWORD=password -WEBHOOK_URL=trmnl webhook url \ No newline at end of file +#LIST_UUID=optional list uuid +WEBHOOK_URL=trmnl webhook url diff --git a/README.md b/README.md index 60e3d06..5ee6447 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,8 @@ You can either run the backend service through docker or run the script directly #### Docker -Pull the docker image and pass the email, password and webhook url as env variables. +Pull the docker image and pass the email, password and webhook url as env variables.
+You can also provide an optional list UUID (see 3. below). ```bash docker pull ghcr.io/yshrdbrn/trmnl-bring-plugin:main @@ -47,15 +48,44 @@ docker run -e 'EMAIL'='' -e 'PASSWORD'='' -e 'WEBHOOK_URL'='' + -e 'LIST_UUID'='' # optional 'ghcr.io/yshrdbrn/trmnl-bring-plugin:main' ``` #### Run the script directly -Clone the repo and rename `.env.example` to `.env` and put in your email, password and webhook url there. Then run the script: +Clone the repo and rename `.env.example` to `.env` and put in your email, password, webhook url, and optionally list UUID there. Then run the script: ```bash python3 -m venv venv pip install -r requirements.txt python ./main.py ``` + +### 3. Optional: Determine your list UUID and provide it to the plugin + +You can manage several shopping lists on Bring with the same account.
+If you want to choose a specific list, you need to determine its UUID. Otherwise the plugin just uses the first list it encounters (which might not be the first list you see in the app or web page). + +```bash +docker run + -e 'EMAIL'='' + -e 'PASSWORD'='' + 'ghcr.io/yshrdbrn/trmnl-bring-plugin:main' + python -u ./fetch_lists.py +``` + +Or run it without docker (EMAIL and PASSWORD must be set in .env and dependencies installed as in 2. above): + +```bash +python ./fetch_lists.py +``` + +You should get a list of lists like this: + +| **Name** | **UUID** | +| --------- | ------------------------------- | +| Groceries | 12345678-90ab-cdef-0123456789ab | +| Stuff | abcdef01-2345-6789-0123456789ab | + +Copy the UUID of the list you would like to display, and set it as `LIST_UUID` either via `-e` in docker or in the `.env` file. Restart your plugin service to apply the change. diff --git a/fetch_lists.py b/fetch_lists.py new file mode 100644 index 0000000..5545218 --- /dev/null +++ b/fetch_lists.py @@ -0,0 +1,36 @@ +import asyncio +import os +import aiohttp +from bring_api import Bring +from dotenv import load_dotenv + +load_dotenv() + +class Color: + BOLD = '\033[1m' + END = '\033[0m' + +async def main(): + email = os.getenv("EMAIL") + password = os.getenv("PASSWORD") + + async with aiohttp.ClientSession() as session: + bring = Bring(session, email, password) + await bring.login() + + lists = (await bring.load_lists()).lists + + if len(lists) <= 0: + print("No lists found on account") + return + + name_col_width = max(len("Name"), max(len(l.name) for l in lists)) + 2 # padding + uuid_col_width = max(len("UUID"), max(len(l.listUuid) for l in lists)) + + print(f"{Color.BOLD}{'Name'.ljust(name_col_width)} {'UUID'.ljust(uuid_col_width)}{Color.END}") + for l in lists: + print(f"{l.name.ljust(name_col_width)} {l.listUuid.ljust(uuid_col_width)}") + +if __name__ == "__main__": + asyncio.run(main()) + diff --git a/main.py b/main.py index 0c2b677..6bdaee6 100644 --- a/main.py +++ b/main.py @@ -47,6 +47,7 @@ class BringPlugin: def __init__(self): self.email = os.getenv("EMAIL") self.password = os.getenv("PASSWORD") + self.list_uuid = os.getenv("LIST_UUID") self.webhook_url = os.getenv("WEBHOOK_URL") self.bring = None self.existing_list = None @@ -95,7 +96,16 @@ class BringPlugin: if self.existing_list: new_list = copy.deepcopy(self.existing_list) else: - bring_api_list = (await self.bring.load_lists()).lists[0] + all_api_lists = (await self.bring.load_lists()).lists + if self.list_uuid is None: + if len(all_api_lists) > 1: + print(f"LIST_UUID not specified, will use first list.") + bring_api_list = all_api_lists[0] + else: + bring_api_list = next((l for l in all_api_lists if l.listUuid == self.list_uuid), None) + if bring_api_list is None: + print(f"No list with UUID \"{self.list_uuid}\" found, will use first list instead.") + bring_api_list = all_api_lists[0] new_list = BringList(name=bring_api_list.name, uuid=bring_api_list.listUuid) await self.grab_items(new_list)