Skip to content

Commit

Permalink
feat(cloud): add notifications for upcoming botb battles
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtnvgr committed Nov 16, 2024
1 parent be48430 commit e442e78
Showing 2 changed files with 55 additions and 1 deletion.
44 changes: 44 additions & 0 deletions hosts/cloud/botb_battles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Simple script to notify me about new BotB battles

from datetime import datetime, timedelta
import requests, time

NTFY_TOPIC = "botb_upcoming_battles144"

def get_adjusted_time(x):
x = datetime.fromisoformat(x)
x += timedelta(hours=15) # Adjust to UTC+7
return 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())

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"]

for battle in battles:
if battle["id"] in sent_battles_ids:
continue

title = battle["title"]
start_time = get_adjusted_time(battle["start"]).strftime("%H:%M %d.%m")

ntfy_send(f"{title} ({start_time})")

sent_battles.append(battle)
sent_battles_ids.add(battle["id"])

time.sleep(3600) # 1h

# Collect garbage battles
for battle in sent_battles:
if get_adjusted_time(battle["start"]) > datetime.now():
sent_battles.remove(battle)
sent_battles_ids.remove(battle["id"])
12 changes: 11 additions & 1 deletion hosts/cloud/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ ... }: {
{ pkgs, ... }: {
modules.server = {
enable = true;

@@ -49,5 +49,15 @@
};
};

systemd.user.services.botb-battles = {
enable = true;
after = [ "network.target" ];
wantedBy = [ "default.target" ];
serviceConfig = {
Type = "simple";
ExecStart = ''${pkgs.python3} ${./botb_battles.py}'';
};
};

system.stateVersion = "24.05";
}

0 comments on commit e442e78

Please sign in to comment.