2023-12-10 19:15:22 +00:00
|
|
|
from tgtg import TgtgClient
|
|
|
|
from pprint import pprint
|
|
|
|
import json
|
|
|
|
import time
|
|
|
|
import requests
|
|
|
|
import random
|
2023-12-12 08:13:43 +00:00
|
|
|
import schedule
|
2023-12-10 19:15:22 +00:00
|
|
|
from collections import defaultdict
|
2024-01-13 11:10:03 +00:00
|
|
|
from .config import CREDFILE, config
|
2023-12-10 19:15:22 +00:00
|
|
|
|
|
|
|
previous = defaultdict(int)
|
|
|
|
|
|
|
|
creds = {}
|
|
|
|
|
|
|
|
with open('.credentials.json', 'r') as f:
|
|
|
|
creds = json.load(f)
|
|
|
|
|
2024-01-13 11:10:03 +00:00
|
|
|
NTFY_CHANNEL = config["ntfy_channel"]
|
2023-12-10 19:15:22 +00:00
|
|
|
|
|
|
|
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):
|
2023-12-12 08:13:43 +00:00
|
|
|
print(f"Sending {data} to {url}")
|
|
|
|
requests.post(url,
|
|
|
|
data=data.encode(encoding='utf-8'))
|
2023-12-10 19:15:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_watchlist():
|
2024-01-13 11:10:03 +00:00
|
|
|
watchlist = client.get_items(favorites_only=True)
|
2023-12-10 19:15:22 +00:00
|
|
|
total = 0
|
2024-01-13 11:10:03 +00:00
|
|
|
for item in watchlist:
|
|
|
|
#item = client.get_item(id)
|
2023-12-10 19:15:22 +00:00
|
|
|
total += check(item)
|
|
|
|
time.sleep(random.randint(2, 10))
|
2023-12-12 08:13:43 +00:00
|
|
|
|
|
|
|
def notify_all():
|
|
|
|
total = sum(previous.values())
|
2023-12-10 19:15:22 +00:00
|
|
|
notify(f"List checked. Total available: {total}", url=NTFY_CHANNEL_DEBUG)
|
|
|
|
|
|
|
|
|
2023-12-12 08:13:43 +00:00
|
|
|
schedule.every(1).minutes.do(check_watchlist)
|
|
|
|
schedule.every(60).minutes.do(notify_all)
|
|
|
|
|
2023-12-10 19:15:22 +00:00
|
|
|
|
|
|
|
while True:
|
2023-12-12 08:13:43 +00:00
|
|
|
schedule.run_pending()
|
|
|
|
#check_watchlist()
|
2023-12-10 19:15:22 +00:00
|
|
|
time.sleep(30)
|