From 969d2f35cd0a64517f4ee31f84a8c74cdd3d7b87 Mon Sep 17 00:00:00 2001 From: Tobias Manske Date: Fri, 5 Oct 2018 17:25:52 +0200 Subject: [PATCH 1/2] [module/spaceapi] Add Space API module SpaceAPI is a API for hackerspaces. Hackerspaces announce their state and location using a predefined json framework. Users can use this data to make sure the space is opened before they decide to go there. The module was created because the author felt the need to know if his hometown's space was open or not. Signed-off-by: Tobias Manske --- bumblebee/modules/spaceapi.py | 75 +++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 bumblebee/modules/spaceapi.py diff --git a/bumblebee/modules/spaceapi.py b/bumblebee/modules/spaceapi.py new file mode 100644 index 0000000..60037a8 --- /dev/null +++ b/bumblebee/modules/spaceapi.py @@ -0,0 +1,75 @@ +# pylint: disable=C0111,R0903 + +"""Displays the state of a spaceapi endpoint + +Requires the following libraries: + * urllib + * json + * time + +Parameters: + * spaceapi.url: String representation of the api endpoint + * spaceapi.name: String overwriting the space name + * spaceapi.prefix: Prefix for the space string + * spaceapi.interval: time between updates +""" + +import bumblebee.input +import bumblebee.output +import bumblebee.engine + +import urllib.request +import json +import time + + +class Module(bumblebee.engine.Module): + def __init__(self, engine, config): + super(Module, self).__init__( + engine, config, bumblebee.output.Widget(full_text=self.getState) + ) + self._state = False + self._error = False + self._url = self.parameter("url", + default="http://club.entropia.de/spaceapi") + self._name = self.parameter("name", default="") + self._lastQuery = 0 + self._sleeptime = self.parameter("interval", default=300) + + def getState(self, widget): + string = self.parameter("prefix", default="") + string += self._name + ": " + if self._error: + string += "ERROR" + elif self._state: + string += "Open" + else: + string += "Closed" + return string + + def state(self, widget): + if self._error: + return ["critical"] + elif self._state: + return ["warning"] + else: + return [] + + def update(self, widgets): + unixtime = time.mktime(time.gmtime()) + # Only query again after interval has passed + if self._lastQuery + self._sleeptime < int(unixtime): + self._lastQuery = int(unixtime) + try: + with urllib.request.urlopen(self._url) as u: + data = json.loads(u.read().decode()) + self._state = data["state"]["open"] + self._name = self.parameter("name", default=data["space"]) + self._error = False + except Exception: + # Displays ERROR status + self._error = True + + +# Author: Tobias Manske +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 From 095b27436e8ff0be5e5fe618abc9e13f89adc7cc Mon Sep 17 00:00:00 2001 From: Tobias Manske Date: Fri, 5 Oct 2018 17:54:43 +0200 Subject: [PATCH 2/2] [modules/spaceapi] convert from urllib2 to requests better python2 support Signed-off-by: Tobias Manske --- bumblebee/modules/spaceapi.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bumblebee/modules/spaceapi.py b/bumblebee/modules/spaceapi.py index 60037a8..f9beb38 100644 --- a/bumblebee/modules/spaceapi.py +++ b/bumblebee/modules/spaceapi.py @@ -3,7 +3,7 @@ """Displays the state of a spaceapi endpoint Requires the following libraries: - * urllib + * requests * json * time @@ -18,7 +18,7 @@ import bumblebee.input import bumblebee.output import bumblebee.engine -import urllib.request +import requests import json import time @@ -61,8 +61,9 @@ class Module(bumblebee.engine.Module): if self._lastQuery + self._sleeptime < int(unixtime): self._lastQuery = int(unixtime) try: - with urllib.request.urlopen(self._url) as u: - data = json.loads(u.read().decode()) + + with requests.get(self._url) as u: + data = u.json() self._state = data["state"]["open"] self._name = self.parameter("name", default=data["space"]) self._error = False