From f7d95d4df21bc442261723298f9889bd093feb97 Mon Sep 17 00:00:00 2001 From: Timm Szigat Date: Sun, 1 Feb 2015 04:08:45 +0100 Subject: add spaceapi module --- py3status/modules/spaceapi.py | 85 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 py3status/modules/spaceapi.py diff --git a/py3status/modules/spaceapi.py b/py3status/modules/spaceapi.py new file mode 100644 index 0000000..55846b8 --- /dev/null +++ b/py3status/modules/spaceapi.py @@ -0,0 +1,85 @@ +# -*- coding: utf-8 -*- +""" +This module shows if your favorite hackerspace is open or not + +Last modified: 2015-02-01 +Author: @timmszigat +License: WTFPL http://www.wtfpl.net/txt/copying/ +""" + +from time import time +import datetime +import json +import urllib.request +import codecs + +class Py3status: + """ + Configuration Parameters: + - cache_timeout: Set timeout between calls in seconds + - url: URL to SpaceAPI json file of your space + - open_text: text if space is open, strftime parmeters will be translated + - open_color: color if space is open + - closed_text: text if space is closed, strftime parameters will be translated + - closed_color: color if space is closed + """ + cache_timeout = 60 + url = 'http://status.chaospott.de/status.json' + open_text = 'open since %H:%M' + open_color = None + closed_text = 'closed since %H:%M' + closed_color = None + + def __init__(self): + pass + + def check(self, i3s_output_list, i3s_config): + + response = { + 'name': 'spaceapi', + 'cached_until': time() + self.cache_timeout + } + + try: + if not self.open_color: + self.open_color = i3s_config['color_good'] + + if not self.closed_color: + self.closed_color = '' + + json_file=urllib.request.urlopen(self.url) + + reader = codecs.getreader("utf-8") + data = json.load(reader(json_file)) + json_file.close() + + + if(data['state']['open'] == True): + response['full_text'] = self.open_text + response['short_text'] = '%H:%M' + if self.open_color: + response['color'] = self.open_color + else: + response['full_text'] = self.closed_text + response['short_test'] = '' + if self.closed_color: + response['color'] = self.closed_color + + + dt = datetime.datetime.fromtimestamp(data['state']['lastchange']) + response['full_text'] = dt.strftime(response['full_text']) + + except: + response['full_text'] = ''; + + + return response +if __name__ == "__main__": + """ + Test this module by calling it directly. + """ + from time import sleep + x = Py3status() + while True: + print(x.check([], {})) + sleep(1) -- cgit v1.3 From b8c154127083406349a126d6692d67e1757ef35a Mon Sep 17 00:00:00 2001 From: Timm Szigat Date: Sun, 1 Feb 2015 21:35:47 +0100 Subject: spaceapi source now with 20% more beauty --- py3status/modules/spaceapi.py | 73 ++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/py3status/modules/spaceapi.py b/py3status/modules/spaceapi.py index 55846b8..6f9dcd9 100644 --- a/py3status/modules/spaceapi.py +++ b/py3status/modules/spaceapi.py @@ -23,6 +23,8 @@ class Py3status: - closed_text: text if space is closed, strftime parameters will be translated - closed_color: color if space is closed """ + + # available configuration parameters cache_timeout = 60 url = 'http://status.chaospott.de/status.json' open_text = 'open since %H:%M' @@ -30,50 +32,51 @@ class Py3status: closed_text = 'closed since %H:%M' closed_color = None - def __init__(self): - pass - def check(self, i3s_output_list, i3s_config): response = { - 'name': 'spaceapi', - 'cached_until': time() + self.cache_timeout - } + 'name': 'spaceapi', + 'cached_until': time() + self.cache_timeout + } try: - if not self.open_color: - self.open_color = i3s_config['color_good'] - - if not self.closed_color: - self.closed_color = '' - - json_file=urllib.request.urlopen(self.url) - - reader = codecs.getreader("utf-8") - data = json.load(reader(json_file)) - json_file.close() - - - if(data['state']['open'] == True): - response['full_text'] = self.open_text - response['short_text'] = '%H:%M' - if self.open_color: - response['color'] = self.open_color - else: - response['full_text'] = self.closed_text - response['short_test'] = '' - if self.closed_color: - response['color'] = self.closed_color - - - dt = datetime.datetime.fromtimestamp(data['state']['lastchange']) - response['full_text'] = dt.strftime(response['full_text']) + # if color isn't set, set basic color schema + if not self.open_color: + self.open_color = i3s_config['color_good'] + + if not self.closed_color: + self.closed_color = '' + + + # grab json file + json_file=urllib.request.urlopen(self.url) + reader = codecs.getreader("utf-8") + data = json.load(reader(json_file)) + json_file.close() + + + if(data['state']['open'] == True): + response['full_text'] = self.open_text + response['short_text'] = '%H:%M' + if self.open_color: + response['color'] = self.open_color + else: + response['full_text'] = self.closed_text + response['short_text'] = '' + if self.closed_color: + response['color'] = self.closed_color + + # apply strftime to full and short text + dt = datetime.datetime.fromtimestamp(data['state']['lastchange']) + response['full_text'] = dt.strftime(response['full_text']) + response['short_text'] = dt.strftime(response['short_text']) except: - response['full_text'] = ''; + response['full_text'] = ''; return response + if __name__ == "__main__": """ Test this module by calling it directly. @@ -81,5 +84,5 @@ if __name__ == "__main__": from time import sleep x = Py3status() while True: - print(x.check([], {})) + print(x.check([], {'color_good': 'green'})) sleep(1) -- cgit v1.3 From 11997293f350f52a4b2c9ec18113be9af59a8a82 Mon Sep 17 00:00:00 2001 From: Timm Szigat Date: Mon, 2 Feb 2015 23:57:54 +0100 Subject: module spaceapi QA --- py3status/modules/spaceapi.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/py3status/modules/spaceapi.py b/py3status/modules/spaceapi.py index 6f9dcd9..8703fc9 100644 --- a/py3status/modules/spaceapi.py +++ b/py3status/modules/spaceapi.py @@ -7,35 +7,34 @@ Author: @timmszigat License: WTFPL http://www.wtfpl.net/txt/copying/ """ -from time import time +import codecs import datetime import json +from time import time import urllib.request -import codecs class Py3status: """ Configuration Parameters: - - cache_timeout: Set timeout between calls in seconds - - url: URL to SpaceAPI json file of your space - - open_text: text if space is open, strftime parmeters will be translated - - open_color: color if space is open - - closed_text: text if space is closed, strftime parameters will be translated - - closed_color: color if space is closed + - cache_timeout: Set timeout between calls in seconds + - closed_color: color if space is closed + - closed_text: text if space is closed, strftime parameters will be translated + - open_color: color if space is open + - open_text: text if space is open, strftime parmeters will be translated + - url: URL to SpaceAPI json file of your space """ # available configuration parameters cache_timeout = 60 - url = 'http://status.chaospott.de/status.json' - open_text = 'open since %H:%M' - open_color = None - closed_text = 'closed since %H:%M' closed_color = None + closed_text = 'closed since %H:%M' + open_color = None + open_text = 'open since %H:%M' + url = 'http://status.chaospott.de/status.json' def check(self, i3s_output_list, i3s_config): response = { - 'name': 'spaceapi', 'cached_until': time() + self.cache_timeout } @@ -47,14 +46,12 @@ class Py3status: if not self.closed_color: self.closed_color = '' - # grab json file json_file=urllib.request.urlopen(self.url) reader = codecs.getreader("utf-8") data = json.load(reader(json_file)) json_file.close() - if(data['state']['open'] == True): response['full_text'] = self.open_text response['short_text'] = '%H:%M' @@ -74,7 +71,6 @@ class Py3status: except: response['full_text'] = ''; - return response if __name__ == "__main__": -- cgit v1.3