commit 84f8e4b2990193dffab272dc7d2d2a2c1f30156e Author: J. Fernando Sánchez Date: Sun Dec 10 20:15:22 2023 +0100 first working demo diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d98f9d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.* diff --git a/checker.py b/checker.py new file mode 100644 index 0000000..ca5ad40 --- /dev/null +++ b/checker.py @@ -0,0 +1,59 @@ +from tgtg import TgtgClient +from pprint import pprint +import json +import time +import requests +import random +#import schedule +from collections import defaultdict + +previous = defaultdict(int) + +creds = {} + + +with open('.credentials.json', 'r') as f: + creds = json.load(f) + +with open('.config.json', 'r') as f: + config = json.load(f) + NTFY_CHANNEL = config["ntfy_channel"] + watchlist = config["watchlist"] + +NTFY_CHANNEL_DEBUG = NTFY_CHANNEL + "-debug" + + +client = TgtgClient(access_token=creds["access_token"], refresh_token=creds["refresh_token"], user_id=creds["user_id"], cookie=creds["cookie"]) + + +def check(item): + #pprint(item) + available = int(item["items_available"]) + id = item["store"]["store_id"] + print(f"{id:<10} - { item['display_name']}: { item['items_available']} available") + if available != previous[id]: + previous[id] = available + notify(f"{ available } disponibles para {item['display_name'] }") + return available + + +def notify(data, url=NTFY_CHANNEL): + requests.post(url, + data=data.encode(encoding='utf-8')) + + +def check_watchlist(): + total = 0 + for id in watchlist: + item = client.get_item(id) + total += check(item) + time.sleep(random.randint(2, 10)) + notify(f"List checked. Total available: {total}", url=NTFY_CHANNEL_DEBUG) + + +#schedule.every(1).minutes.do(check_watchlist) + +while True: + #schedule.run_pending() + check_watchlist() + time.sleep(30) diff --git a/login.py b/login.py new file mode 100644 index 0000000..93cc747 --- /dev/null +++ b/login.py @@ -0,0 +1,7 @@ +from tgtg import TgtgClient + +client = TgtgClient(email="tgtg@sinpapel.es") +credentials = client.get_credentials() +with open('.credentials.json', 'w') as f: + print(credentials) + json.dump(credentials, f) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..72ad264 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +tgtg==0.16.1 +requests==2.31.0 +schedule==1.2.1