tg2/main.py

91 lines
2.7 KiB
Python
Raw Normal View History

2023-03-29 20:00:10 +02:00
from tgtg import TgtgClient
import os
import json
import telegram
import asyncio
import time
import datetime
2023-05-19 22:01:51 +02:00
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'))
2023-03-29 20:00:10 +02:00
credentials = client.get_credentials()
2023-05-19 22:01:51 +02:00
with open('/data/token', 'w') as file:
2023-05-13 23:13:33 +02:00
file.write(str(credentials))
2023-03-29 20:00:10 +02:00
else:
2023-05-19 22:01:51 +02:00
with open('/data/token', 'r') as file:
2023-05-13 23:13:33 +02:00
credentials = json.loads(file.read().replace('\'', '"'))
2023-03-29 20:00:10 +02:00
2023-05-13 23:13:33 +02:00
client = TgtgClient(**credentials)
2023-05-19 22:01:51 +02:00
bot = telegram.Bot(os.environ['TELEGRAM_TOKEN'])
2023-05-19 21:36:12 +02:00
2023-05-19 22:01:51 +02:00
async def send_message(text):
2023-05-19 21:36:12 +02:00
async with bot:
2023-05-19 22:01:51 +02:00
await bot.send_message(chat_id=os.environ.get('TELEGRAM_ID'), text=text)
async def main():
await send_message('tg² bot is watching!')
2023-03-29 20:00:10 +02:00
last = []
while True:
items = client.get_items()
texts = ['Too good to go']
next = []
for item in items:
if item['items_available'] > 0:
next.append(item["item"]["item_id"])
if item["item"]["item_id"] not in last:
amount = item["items_available"]
2023-05-19 22:01:51 +02:00
item_name = item["item"]["name"]
2023-03-29 20:00:10 +02:00
price = item["item"]["price_including_taxes"]["minor_units"]/(10**item["item"]["price_including_taxes"]["decimals"])
2023-05-19 22:01:51 +02:00
store_name = item["store"]["store_name"]
store_branch = item["store"]["branch"]
2023-03-29 20:00:10 +02:00
2023-05-19 22:01:51 +02:00
name = ', '.join(filter(bool, [item_name, store_name, store_branch]))
2023-03-29 20:00:10 +02:00
2023-05-19 22:01:51 +02:00
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"]
2023-03-29 20:00:10 +02:00
2023-05-19 22:01:51 +02:00
# if not name:
# name = "Panier anti-gaspi"
2023-03-29 20:00:10 +02:00
2023-05-19 22:01:51 +02:00
# texts.append(f' - No more "{name}" ({price:.2f}€) available at "{store}"')
2023-03-29 20:00:10 +02:00
if len(texts) > 1:
print(f'\n{datetime.datetime.now()}: {len(texts)-1} new items available')
2023-05-19 22:01:51 +02:00
await send_message('\n'.join(texts))
2023-03-29 20:00:10 +02:00
else:
print('-', end='', flush=True)
last = next
time.sleep(60)
if __name__ == '__main__':
2023-05-19 22:01:51 +02:00
check_env()
load_creds()
2023-05-19 21:36:12 +02:00
asyncio.run(main())