summaryrefslogtreecommitdiffstats
path: root/py3status/modules/online_status.py
blob: 8176d2163cabfbf354ee854cdfc4f6ca14511bff (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
# -*- coding: utf-8 -*-
"""
Display if a connection to the internet is established.

@author obb
"""

from time import time
try:
    # python3
    from urllib.request import urlopen
except:
    from urllib2 import urlopen


class Py3status:
    """
    Configuration parameters:
        - cache_timeout : how often to run the check
        - format_offline : what to display when offline
        - format_online : what to display when online
        - timeout : how long before deciding we're offline
        - url : connect to this url to check the connection status
    """
    # available configuration parameters
    cache_timeout = 10
    format_offline = '■'
    format_online = '●'
    timeout = 2
    url = 'http://www.google.com'

    def _connection_present(self):
        try:
            urlopen(self.url, timeout=self.timeout)
        except:
            return False
        else:
            return True

    def online_status(self, i3s_output_list, i3s_config):
        response = {
            'cached_until': time() + self.cache_timeout
        }

        connected = self._connection_present()
        if connected:
            response['full_text'] = self.format_online
            response['color'] = i3s_config['color_good']
        else:
            response['full_text'] = self.format_offline
            response['color'] = i3s_config['color_bad']

        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.online_status([], config))
        sleep(1)