tg2/main.py

73 lines
2.2 KiB
Python
Raw Normal View History

2023-03-29 20:00:10 +02:00
import config
from tgtg import TgtgClient
import os
import json
import telegram
import asyncio
import time
import datetime
async def main():
if not os.path.exists('token'):
client = TgtgClient(email=config.email)
credentials = client.get_credentials()
with open('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:
with open('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-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"]
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"
texts.append(f' - {amount} item(s) of "{name}" ({price:.2f}€) available at "{store}"')
2023-05-13 23:13:33 +02:00
elif item["item"]["item_id"] in last:
2023-03-29 20:00:10 +02:00
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"
texts.append(f' - No more "{name}" ({price:.2f}€) available at "{store}"')
if len(texts) > 1:
bot = telegram.Bot(config.telegram_token)
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))
else:
print('-', end='', flush=True)
last = next
time.sleep(60)
if __name__ == '__main__':
asyncio.run(main())