import datetime from abc import ABCMeta, abstractmethod from collections import namedtuple import requests Status = namedtuple("Status", ['doorstate', 'timestamp', 'text']) class Source(metaclass=ABCMeta): @abstractmethod def get_status(self): pass def is_recent(self, status, **kwargs): return status.timestamp + datetime.timedelta(days=1) < datetime.datetime.today() class IsFsWIAIopen(Source): url = "https://isfswiaiopen.wiai.de?json" def __init__(self, texts=None): self.texts = texts def _parse_time(self, string): return datetime.datetime.strptime(string, "%Y-%m-%d %H:%M:%S") def _get_text(self, state): return self.texts[state] if self.texts else "" def get_status(self): status = requests.get(self.url).json() return Status( doorstate=str(status['doorstate']), timestamp=self._parse_time(status['timestamp']), text=self._get_text(str(status['doorstate'])))