doorbot/sources.py

35 lines
929 B
Python

import datetime
import requests
from collections import namedtuple
Status = namedtuple("Status", ['doorstate', 'timestamp', 'text'])
class Source:
def get_status(self):
raise NotImplementedError()
def is_recent(self, status, **kwargs):
raise NotImplementedError()
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'])))
def is_recent(self, status, **kwargs):
return status.timestamp + datetime.timedelta(days=1) < datetime.datetime.today()