scheduled-plugin-pretalx-br.../service

117 lines
3.4 KiB
Text
Raw Normal View History

#!/usr/bin/python
2023-09-30 11:52:06 +00:00
import os
import sys
import time
import traceback
from datetime import datetime, timedelta
import pytz
from requests import get
from hosted import config, node
config.restart_on_update()
2023-09-30 11:52:06 +00:00
SEND_PREFIX = os.environ["NODE"].replace("root/", "root/plugin/")
def log(msg):
sys.stderr.write("[pretalx] {}\n".format(msg))
def idle(seconds, event_start, event_tz):
end = time.time() + seconds
log("sleeping for {} seconds".format(seconds))
while time.time() < end:
send_data = {"day": "??", "time": int(time.time())}
if event_start is not None:
event_now = datetime.now(event_tz)
utc_now = datetime.now(pytz.utc)
utc_offset = (event_now - utc_now).total_seconds()
day_zero = event_start.replace(
hour=0, minute=0, second=0, tzinfo=event_tz
) - timedelta(days=1)
day_info = event_now - day_zero
log("Day0: {}".format(day_zero.isoformat()))
log("NOW: {}".format(event_now.isoformat()))
send_data["day"] = day_info.days
for k, v in send_data.items():
2023-09-30 11:52:06 +00:00
node.send_raw("{}/{}:{}".format(SEND_PREFIX, k, v))
time.sleep(1)
def main():
event_info = None
event_start = None
schedule = None
event_tz = pytz.utc
while True:
event_url = config["event_url"]
if not event_url.endswith("/"):
event_url = event_url + "/"
if "example.com" in event_url:
log("default event url, waiting for config update")
# sleep forever, service will get restarted if the config
# changes.
time.sleep(99999999)
log("event url: {}".format(event_url))
try:
r = get(
event_url + "p/broadcast-tools/event.json",
)
r.raise_for_status()
except Exception as e:
log("updating event info failed: {}".format(repr(e)))
# Only print the error message. If we have fetched the event
# info json blob atleast once, we have all the information
# we need.
else:
event_info = r.json()
node.write_json("event.json", event_info)
log("updated event info json")
if event_info is not None:
event_start = datetime.strptime(event_info["start"], "%Y-%m-%d")
event_tz = pytz.timezone(event_info["timezone"])
try:
r = get(
event_url + "p/broadcast-tools/schedule.json",
)
r.raise_for_status()
except Exception as e:
log("updating schedule failed: {}".format(repr(e)))
# Only print the error message. If we have fetched the schedule
# info json blob atleast once, we have all the information
# we need.
else:
schedule = r.json()
for talk in schedule["talks"]:
talk["start_str"] = (
datetime.fromtimestamp(talk["start_ts"])
.replace(tzinfo=pytz.utc)
.astimezone(event_tz)
.strftime("%H:%M")
)
node.write_json("schedule.json", schedule)
log("updated schedule json")
idle(30, event_start, event_tz)
if __name__ == "__main__":
try:
main()
except:
traceback.print_exc()
time.sleep(30)