Skip to content

Commit

Permalink
chore(cloud/botb-battles): improve logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtnvgr committed Dec 2, 2024
1 parent 20b40ae commit 88281e8
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions hosts/cloud/botb_battles.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/env python

# Simple script to notify me about new BotB battles

from requests.exceptions import RequestException, HTTPError
from datetime import datetime, timedelta
import requests, time

Expand All @@ -12,16 +15,23 @@ def get_adjusted_time(x):

def ntfy_send(text):
print(f"Sent: \"{text}\"")
resp = requests.post(f"https://ntfy.sh/{NTFY_TOPIC}", data = text.encode(encoding="utf-8"))
if resp.status_code != 200:
print(resp.json())

while True:
try:
requests.post(f"https://ntfy.sh/{NTFY_TOPIC}", data = text.encode(encoding="utf-8"))
break
except HTTPError as e:
print(e.response.json())
except RequestException as e:
print(f"Error: {e}")
time.sleep(600) # 10m

sent_battles = []
sent_battles_ids = set()

while True:
battles = requests.get("https://battleofthebits.com/api/v1/battle/current").json()
battles = [x for x in battles if x["period"] == "warmup"]
battles = [x for x in battles if x.get("period") == "warmup"]

for battle in battles:
if battle["id"] in sent_battles_ids:
Expand All @@ -39,6 +49,6 @@ def ntfy_send(text):

# Collect garbage battles
for battle in sent_battles:
if get_adjusted_time(battle["start"]) > datetime.now():
if datetime.now() > get_adjusted_time(battle["start"]):
sent_battles.remove(battle)
sent_battles_ids.remove(battle["id"])

0 comments on commit 88281e8

Please sign in to comment.