#!/usr/bin/env python import logging from datetime import datetime, timedelta from time import sleep from pytz import utc from requests import get from hosted import device logging.basicConfig(level=logging.INFO) log = logging.getLogger("afra-status") last_open = datetime.now(utc) while True: try: r = get("https://spaceapi.afra-berlin.de/v1/status.json") r.raise_for_status() status = r.json() except Exception as e: log.exception("error while fetching status") status = { "state": { "open": None, }, } try: if status["state"]["open"]: last_open = datetime.now(utc) except KeyError: log.exception("open status was not in state json") if datetime.now(utc) - last_open > timedelta(minutes=2): device.screen(on=False) else: device.screen(on=True) sleep(30)