Randomness in interval and increasing waiting time to prevent API ban
This commit is contained in:
parent
a2eff32362
commit
24df340049
103
main.py
103
main.py
|
@ -1,4 +1,5 @@
|
|||
from tgtg import TgtgClient
|
||||
from tgtg.exceptions import TgtgAPIError
|
||||
import os
|
||||
import json
|
||||
import telegram
|
||||
|
@ -6,7 +7,16 @@ import asyncio
|
|||
import time
|
||||
import datetime
|
||||
import requests
|
||||
import random
|
||||
|
||||
initial_waiting_time = 60
|
||||
waiting_time = initial_waiting_time
|
||||
waiting_time_limit = 60 * 60 * 24
|
||||
waiting_time_increase = 2
|
||||
|
||||
randomness = 0.1
|
||||
|
||||
interval = 60
|
||||
|
||||
client = None
|
||||
telegram_bot = None
|
||||
|
@ -16,6 +26,21 @@ if removal_notification is None:
|
|||
removal_notification = False
|
||||
|
||||
|
||||
def parse_duration(seconds):
|
||||
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'
|
||||
|
||||
|
||||
def check_env():
|
||||
if os.environ.get('TGTG_EMAIL') is None:
|
||||
return False
|
||||
|
@ -74,53 +99,65 @@ async def main():
|
|||
last = []
|
||||
|
||||
while True:
|
||||
items = client.get_items()
|
||||
try:
|
||||
items = client.get_items()
|
||||
|
||||
texts = []
|
||||
texts = []
|
||||
|
||||
next = []
|
||||
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"]
|
||||
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"]
|
||||
|
||||
name = ', '.join(filter(bool, [item_name, store_name, store_branch]))
|
||||
name = ', '.join(filter(bool, [item_name, store_name, store_branch]))
|
||||
|
||||
if not name:
|
||||
name = "Panier anti-gaspi"
|
||||
if not name:
|
||||
name = "Panier anti-gaspi"
|
||||
|
||||
texts.append(f'{amount} x "{name}" ({price:.2f}€)')
|
||||
elif removal_notification 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"]
|
||||
texts.append(f'{amount} x "{name}" ({price:.2f}€)')
|
||||
elif removal_notification 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"]
|
||||
|
||||
name = ', '.join(filter(bool, [item_name, store_name, store_branch]))
|
||||
name = ', '.join(filter(bool, [item_name, store_name, store_branch]))
|
||||
|
||||
if not name:
|
||||
name = "Panier anti-gaspi"
|
||||
if not name:
|
||||
name = "Panier anti-gaspi"
|
||||
|
||||
texts.append(f'no more "{name}"')
|
||||
texts.append(f'no more "{name}"')
|
||||
|
||||
|
||||
if len(texts) > 1:
|
||||
if len(texts) > 1:
|
||||
|
||||
print(f'\n{datetime.datetime.now()}: {len(texts)-1} new items available')
|
||||
print(f'\n{datetime.datetime.now()}: {len(texts)-1} new items available')
|
||||
|
||||
await send_message(texts)
|
||||
else:
|
||||
print('-', end='', flush=True)
|
||||
await send_message(texts)
|
||||
else:
|
||||
print('-', end='', flush=True)
|
||||
|
||||
last = next
|
||||
time.sleep(60)
|
||||
last = next
|
||||
time.sleep(interval + randomness * interval * (2 * random.random() - 1))
|
||||
|
||||
waiting_time = initial_waiting_time
|
||||
|
||||
except TgtgAPIError as e:
|
||||
print(e)
|
||||
real_waiting_time = round(waiting_time + randomness * waiting_time * (2 * random.random() - 1))
|
||||
await send_message([f'tg² failed to fetch data, retrying in {parse_duration(real_waiting_time)}'])
|
||||
time.sleep(real_waiting_time)
|
||||
|
||||
if waiting_time < waiting_time_limit:
|
||||
waiting_time *= waiting_time_increase
|
||||
|
||||
if __name__ == '__main__':
|
||||
print('Check environ:', check_env())
|
||||
|
|
Loading…
Reference in New Issue