tg2/main.py

218 lines
6.9 KiB
Python
Raw Normal View History

2024-01-21 19:25:07 +01:00
import tgtg
2023-03-29 20:00:10 +02:00
from tgtg import TgtgClient
from tgtg.exceptions import TgtgAPIError
2023-03-29 20:00:10 +02:00
import os
import json
import telegram
import asyncio
import time
import datetime
2023-12-12 09:20:22 +01:00
import requests
import random
2023-03-29 20:00:10 +02:00
2024-01-23 00:25:15 +01:00
###################################
# UTILS #
###################################
2024-01-22 16:40:02 +01:00
2024-01-23 00:25:15 +01:00
def get_env(name, default=None, mandatory=False):
"""Get an environment variable, with a default value and type casting."""
2024-01-22 16:40:02 +01:00
content = os.environ.get(name, default)
2024-01-23 00:25:15 +01:00
if mandatory and content is None:
raise ValueError(f'Missing environment variable {name}')
2024-01-22 16:40:02 +01:00
if type(default) == bool:
return content.lower() in ('true', '1')
if type(default) == int:
return int(content)
if type(default) == float:
return float(content)
return content
2024-01-23 00:25:15 +01:00
def parse_duration(seconds):
2024-01-23 00:25:15 +01:00
"""Parse a duration in seconds to a human readable format."""
seconds = int(seconds)
minutes = seconds // 60
seconds = seconds % 60
hours = minutes // 60
minutes = minutes % 60
if hours > 0:
return f'{hours}h {minutes}m {seconds}s'
elif minutes > 0:
return f'{minutes}m {seconds}s'
else:
return f'{seconds}s'
2024-01-23 00:25:15 +01:00
def apply_randomness(value, randomness):
"""Apply randomness to a value."""
return round(value + randomness * value * (2 * random.random() - 1))
####################################
# CONFIG #
####################################
TGTG_EMAIL = get_env('TGTG_EMAIL', mandatory=True)
TELEGRAM_NOTIFICATIONS = get_env('TELEGRAM_NOTIFICATIONS', False)
TELEGRAM_TOKEN = get_env('TELEGRAM_TOKEN', mandatory=TELEGRAM_NOTIFICATIONS)
TELEGRAM_ID = get_env('TELEGRAM_ID', mandatory=TELEGRAM_NOTIFICATIONS)
MATRIX_NOTIFICATIONS = get_env('MATRIX_NOTIFICATIONS', False)
MATRIX_URL = get_env('MATRIX_URL', mandatory=MATRIX_NOTIFICATIONS)
MATRIX_BASIC_AUTH_USER = get_env('MATRIX_BASIC_AUTH_USER', mandatory=MATRIX_NOTIFICATIONS)
MATRIX_BASIC_AUTH_PASS = get_env('MATRIX_BASIC_AUTH_PASS', mandatory=MATRIX_NOTIFICATIONS)
INITIAL_WAITING_TIME = get_env('INITIAL_WAITING_TIME', 60)
WAITING_TIME_LIMIT = get_env('WAITING_TIME_LIMIT', 60 * 60 * 24)
WAITING_TIME_INCREASE = get_env('WAITING_TIME_FACTOR', 2)
RANDOMNESS = get_env('RANDOMNESS', 0.1)
INTERVAL = get_env('INTERVAL', 60)
REMOVAL_NOTIFICATIONS = get_env('REMOVAL_NOTIFICATIONS', False)
# Reduce frequency of polling to avoid rate limiting
tgtg.POLLING_WAIT_TIME = get_env('LOGIN_POLLING_WAIT_TIME', 30)
tgtg.MAX_POLLING_TRIES = get_env('LOGIN_MAX_POLLING_TRIES', 10)
TOKEN_PATH = get_env('TOKEN_PATH', '/data/token')
####################################
# MAIN #
####################################
waiting_time = INITIAL_WAITING_TIME
tgtgClient = None
telegram_bot = telegram.Bot(TELEGRAM_TOKEN) if TELEGRAM_NOTIFICATIONS else None
async def send_message(text):
if TELEGRAM_NOTIFICATIONS:
async with telegram_bot:
await telegram_bot.send_message(
chat_id=TELEGRAM_ID,
text='\n'.join(['Too good to Go'] + text)
)
if MATRIX_NOTIFICATIONS:
requests.post(
url=get_env('MATRIX_URL'),
headers={
'Content-Type': 'application/json',
},
auth=(MATRIX_BASIC_AUTH_USER, MATRIX_BASIC_AUTH_PASS),
json={
'title': 'Too Good to Go',
'list': text,
}
)
def catch_api_error(e, message):
global waiting_time
print('ERROR: ', e)
real_waiting_time = apply_randomness(waiting_time, RANDOMNESS)
print(f'{message}, retrying in {parse_duration(real_waiting_time)}')
time.sleep(real_waiting_time)
waiting_time = min(waiting_time * WAITING_TIME_INCREASE, WAITING_TIME_LIMIT)
2024-01-21 19:25:07 +01:00
def get_credentials():
2024-01-23 00:25:15 +01:00
while True:
try:
await send_message(['Open the link to login to tgtg'])
return tgtgClient.get_credentials()
except TgtgAPIError as e:
catch_api_error(e, 'tg² failed to get credentials')
2024-01-21 19:25:07 +01:00
2023-05-19 22:01:51 +02:00
def load_creds():
2024-01-23 00:25:15 +01:00
global tgtgClient, telegram_bot
2023-05-19 22:01:51 +02:00
2024-01-22 16:40:02 +01:00
if not os.path.exists(TOKEN_PATH):
2024-01-23 00:25:15 +01:00
tgtgClient = TgtgClient(email=TGTG_EMAIL)
2023-05-29 16:58:15 +02:00
print('Waiting for credentials ...')
2024-01-21 19:25:07 +01:00
credentials = get_credentials()
2024-01-22 16:40:02 +01:00
with open(TOKEN_PATH, 'w') as file:
2023-05-13 23:13:33 +02:00
file.write(str(credentials))
2023-05-29 16:58:15 +02:00
print('Credentials stored in file')
2023-03-29 20:00:10 +02:00
else:
2024-01-22 16:40:02 +01:00
with open(TOKEN_PATH, 'r') as file:
2023-05-13 23:13:33 +02:00
credentials = json.loads(file.read().replace('\'', '"'))
2023-05-29 16:58:15 +02:00
print('Credentials loaded from file')
2023-03-29 20:00:10 +02:00
2024-01-23 00:25:15 +01:00
tgtgClient = TgtgClient(**credentials)
2023-05-19 22:01:51 +02:00
async def main():
2023-12-19 09:22:17 +01:00
await send_message(['tg² telegram_bot is watching!'])
2023-03-29 20:00:10 +02:00
last = []
while True:
try:
2024-01-23 00:25:15 +01:00
items = tgtgClient.get_items()
texts = []
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"]
item_name = item["item"]["name"]
price = item["item"]["price_including_taxes"]["minor_units"]/(10**item["item"]["price_including_taxes"]["decimals"])
store_name = item["store"]["store_name"]
store_branch = item["store"]["branch"]
2023-03-29 20:00:10 +02:00
name = ', '.join(filter(bool, [item_name, store_name, store_branch]))
2023-03-29 20:00:10 +02:00
if not name:
name = "Panier anti-gaspi"
2023-03-29 20:00:10 +02:00
texts.append(f'{amount} x "{name}" ({price:.2f}€)')
2024-01-23 00:25:15 +01:00
elif REMOVAL_NOTIFICATIONS and 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_name = item["store"]["store_name"]
store_branch = item["store"]["branch"]
2023-03-29 20:00:10 +02:00
name = ', '.join(filter(bool, [item_name, store_name, store_branch]))
2023-03-29 20:00:10 +02:00
if not name:
name = "Panier anti-gaspi"
2023-12-12 09:20:22 +01:00
texts.append(f'no more "{name}"')
2023-12-12 09:20:22 +01:00
if len(texts) > 1:
2023-03-29 20:00:10 +02:00
print(f'\n{datetime.datetime.now()}: {len(texts)-1} new items available')
2023-03-29 20:00:10 +02:00
await send_message(texts)
else:
print('-', end='', flush=True)
last = next
2024-01-23 00:25:15 +01:00
real_interval = INTERVAL + RANDOMNESS * INTERVAL * (2 * random.random() - 1)
time.sleep(INTERVAL)
2023-03-29 20:00:10 +02:00
2024-01-23 00:25:15 +01:00
waiting_time = INITIAL_WAITING_TIME
2023-03-29 20:00:10 +02:00
except TgtgAPIError as e:
2024-01-23 00:25:15 +01:00
catch_api_error(e, 'tg² failed to get items')
2023-03-29 20:00:10 +02:00
if __name__ == '__main__':
2024-01-21 19:25:07 +01:00
if not check_env():
print('Missing environment variables')
exit(1)
2023-05-19 22:01:51 +02:00
load_creds()
2023-05-19 21:36:12 +02:00
asyncio.run(main())