Initial commit

This commit is contained in:
Brieuc Dubois 2023-03-29 20:00:10 +02:00
commit 4ebd98b920
4 changed files with 84 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
config.py
token
/__pycache__/

4
config.example.py Normal file
View File

@ -0,0 +1,4 @@
email = ''
telegram_token = ''
telegram_id = ''

74
main.py Normal file
View File

@ -0,0 +1,74 @@
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:
file.write(credentials)
else:
with open('token', 'r') as file:
credentials = file.read()
data = json.loads(credentials.replace('\'', '"'))
client = TgtgClient(**data)
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}"')
elif 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"
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())

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
# Tested versions. Upgrade at your own risks
tgtg==0.15.0
python-telegram-bot==20.2