diff --git a/config.example.py b/config.example.py deleted file mode 100644 index 8105c17..0000000 --- a/config.example.py +++ /dev/null @@ -1,3 +0,0 @@ -email = '' -telegram_token = '' -telegram_id = '' \ No newline at end of file diff --git a/dockerfile b/dockerfile index d928f3f..6c5e118 100644 --- a/dockerfile +++ b/dockerfile @@ -1,5 +1,7 @@ FROM python:3 +VOLUME /data + WORKDIR /tg2 COPY requirements.txt . @@ -8,6 +10,5 @@ RUN pip install --no-cache-dir pip && \ pip install --no-cache-dir -r requirements.txt COPY main.py . -COPY config.py . CMD ["python", "main.py"] diff --git a/main.py b/main.py index d8a3474..c0fc9f5 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,3 @@ -import config - from tgtg import TgtgClient import os import json @@ -9,22 +7,37 @@ import time import datetime -async def main(): - if not os.path.exists('token'): - client = TgtgClient(email=config.email) +client = None +bot = None + + +def check_env(): + return not (os.environ.get('TGTG_EMAIL') is None or os.environ.get('TELEGRAM_TOKEN') is None or os.environ.get('TELEGRAM_ID') is None) + + +def load_creds(): + global client, bot + + if not os.path.exists('/data/token'): + client = TgtgClient(email=os.environ.get('TGTG_EMAIL')) credentials = client.get_credentials() - with open('token', 'w') as file: + with open('/data/token', 'w') as file: file.write(str(credentials)) else: - with open('token', 'r') as file: + with open('/data/token', 'r') as file: credentials = json.loads(file.read().replace('\'', '"')) - client = TgtgClient(**credentials) - bot = telegram.Bot(config.telegram_token) + bot = telegram.Bot(os.environ['TELEGRAM_TOKEN']) + +async def send_message(text): async with bot: - await bot.send_message(chat_id=config.telegram_id, text='tg² bot is watching!') + await bot.send_message(chat_id=os.environ.get('TELEGRAM_ID'), text=text) + + +async def main(): + await send_message('tg² bot is watching!') last = [] @@ -40,32 +53,31 @@ async def main(): next.append(item["item"]["item_id"]) if item["item"]["item_id"] not in last: amount = item["items_available"] - name = item["item"]["name"] + item_name = item["item"]["name"] price = item["item"]["price_including_taxes"]["minor_units"]/(10**item["item"]["price_including_taxes"]["decimals"]) - store = item["store"]["store_name"] + ' (' + item["store"]["branch"] + ')' + store_name = item["store"]["store_name"] + store_branch = item["store"]["branch"] - if not name: - name = "Panier anti-gaspi" + name = ', '.join(filter(bool, [item_name, store_name, store_branch])) - texts.append(f' - {amount} item(s) of "{name}" ({price:.2f}€) available at "{store}"') - elif item["item"]["item_id"] in last: - amount = item["items_available"] - name = item["item"]["name"] - price = item["item"]["price_including_taxes"]["minor_units"]/(10**item["item"]["price_including_taxes"]["decimals"]) - store = item["store"]["store_name"] + texts.append(f'{amount} x "{name}" ({price:.2f}€)') + # elif item["item"]["item_id"] in last: + # amount = item["items_available"] + # name = item["item"]["name"] + # price = item["item"]["price_including_taxes"]["minor_units"]/(10**item["item"]["price_including_taxes"]["decimals"]) + # store = item["store"]["store_name"] - if not name: - name = "Panier anti-gaspi" + # if not name: + # name = "Panier anti-gaspi" - texts.append(f' - No more "{name}" ({price:.2f}€) available at "{store}"') + # texts.append(f' - No more "{name}" ({price:.2f}€) available at "{store}"') if len(texts) > 1: print(f'\n{datetime.datetime.now()}: {len(texts)-1} new items available') - async with bot: - await bot.send_message(chat_id=config.telegram_id, text='\n'.join(texts)) + await send_message('\n'.join(texts)) else: print('-', end='', flush=True) @@ -73,4 +85,6 @@ async def main(): time.sleep(60) if __name__ == '__main__': + check_env() + load_creds() asyncio.run(main())