use scheduler and avoid abusing ntfy.sh

This commit is contained in:
J. Fernando Sánchez 2023-12-12 09:13:43 +01:00
parent 84f8e4b299
commit c490af0732

View File

@ -4,7 +4,7 @@ import json
import time import time
import requests import requests
import random import random
#import schedule import schedule
from collections import defaultdict from collections import defaultdict
previous = defaultdict(int) previous = defaultdict(int)
@ -38,8 +38,9 @@ def check(item):
def notify(data, url=NTFY_CHANNEL): def notify(data, url=NTFY_CHANNEL):
requests.post(url, print(f"Sending {data} to {url}")
data=data.encode(encoding='utf-8')) requests.post(url,
data=data.encode(encoding='utf-8'))
def check_watchlist(): def check_watchlist():
@ -48,12 +49,17 @@ def check_watchlist():
item = client.get_item(id) item = client.get_item(id)
total += check(item) total += check(item)
time.sleep(random.randint(2, 10)) time.sleep(random.randint(2, 10))
def notify_all():
total = sum(previous.values())
notify(f"List checked. Total available: {total}", url=NTFY_CHANNEL_DEBUG) notify(f"List checked. Total available: {total}", url=NTFY_CHANNEL_DEBUG)
#schedule.every(1).minutes.do(check_watchlist) schedule.every(1).minutes.do(check_watchlist)
schedule.every(60).minutes.do(notify_all)
while True: while True:
#schedule.run_pending() schedule.run_pending()
check_watchlist() #check_watchlist()
time.sleep(30) time.sleep(30)