summaryrefslogtreecommitdiffstats
path: root/py3status/modules/spaceapi.py
blob: c4eac582814a116a64705555a0f7282b4fe9328c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
"""
Display if your favorite hackerspace is open or not.

@author timmszigat
@license WTFPL <http://www.wtfpl.net/txt/copying/>
"""

import codecs
import datetime
import json
from time import time
import urllib.request


class Py3status:
    """
    Configuration Parameters:
        - 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
    closed_color = None
    closed_text = 'closed'
    open_color = None
    open_text = 'open'
    time_text = ' since %H:%M'
    url = 'http://status.chaospott.de/status.json'

    def check(self, i3s_output_list, i3s_config):

        response = {
            'cached_until': time() + self.cache_timeout
            }

        try:
            # 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'] is True):
                if self.open_color:
                    response['color'] = self.open_color
                if 'lastchange' in data['state'].keys():
                    response['full_text'] = self.open_text + self.time_text
                    response['short_text'] = '%H:%M'
                else:
                    response['full_text'] = self.open_text
                    response['short_text'] = self.open_text

            else:
                if self.closed_color:
                    response['color'] = self.closed_color
                if 'lastchange' in data['state'].keys():
                    response['full_text'] = self.closed_text + self.time_text
                    response['short_text'] = ''
                else:
                    response['full_text'] = self.closed_text
                    response['short_text'] = self.closed_text

            if 'lastchange' in data['state'].keys():
                # 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'] = ''

        return response

if __name__ == "__main__":
    """
    Test this module by calling it directly.
    """
    from time import sleep
    x = Py3status()
    config = {
        'color_good': '#00FF00',
        'color_bad': '#FF0000',
    }
    while True:
        print(x.check([], config))
        sleep(1)