<commit_before># -*- coding: utf-8 -*-
# FIXME closed_color, open_color params
"""
Display if your favorite hackerspace is open or not.

Configuration parameters:
    cache_timeout: Set timeout between calls in seconds (default 60)
    closed_color: color if space is closed (default None)
    closed_text: text if space is closed, strftime parameters
        will be translated (default 'closed')
    open_color: color if space is open (default None)
    open_text: text if space is open, strftime parmeters will be translated
        (default 'open')
    time_text: format used for time display (default ' since %H:%M')
    url: URL to SpaceAPI json file of your space
        (default 'http://status.chaospott.de/status.json')

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

import codecs
import datetime
import json
import urllib.request


class Py3status:
    """
    """
    # 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):

        response = {
            'cached_until': self.py3.time_in(self.cache_timeout),
            }

        try:
            # if color isn't set, set basic color schema
            if not self.open_color:
                self.open_color = self.py3.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__":
    """
    Run module in test mode.
    """
    from py3status.module_test import module_test
    module_test(Py3status)<commit_msg>Go to a space's website onclick<commit_after># -*- coding: utf-8 -*-
# FIXME closed_color, open_color params
"""
Display if your favorite hackerspace is open or not.

Configuration parameters:
    button_url: Button that when clicked opens the URL sent in the space's API.
        Setting to None disables. (default 3)
    cache_timeout: Set timeout between calls in seconds (default 60)
    closed_color: color if space is closed (default None)
    closed_text: text if space is closed, strftime parameters
        will be translated (default 'closed')
    open_color: color if space is open (default None)
    open_text: text if space is open, strftime parmeters will be translated
        (default 'open')
    time_text: format used for time display (default ' since %H:%M')
    url: URL to SpaceAPI json file of your space
        (default 'http://status.chaospott.de/status.json')

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

import codecs
import datetime
import json
import urllib.request
import shlex
import subprocess


class Py3status:
    """
    """
    # available configuration parameters
    button_url = 3
    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):

        response = {
            'cached_until': self.py3.time_in(self.cache_timeout),
            }

        try:
            # if color isn't set, set basic color schema
            if not self.open_color:
                self.open_color = self.py3.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()
            self._url = data.get('url')

            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

    def on_click(self, event):
        button = event['button']
        if self._url and self.button_url == button:
            cmd = 'xdg-open {}'.format(self._url)
            subprocess.call(shlex.split(cmd))
            self.py3.prevent_refresh()


if __name__ == "__main__":
    """
    Run module in test mode.
    """
    from py3status.module_test import module_test
    module_test(Py3status)
{'fraction_non_alphanumeric': 0.1044434919274182, 'fraction_numerical': 0.0035719388484069152, 'mean_word_length': 1.971137521222411, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1855571', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1878, 'n_words': 504}