blob: c44d7eae5b6002a190d51e60fd42cde5a7d4ec89 (
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
|
# -*- coding: utf-8 -*-
"""
Display your public/external IP address and toggle to online status on click.
Configuration parameters:
- cache_timeout : how often we refresh this module in seconds (default 30s)
- format: the only placeholder available is {ip} (default '{ip}')
- format_offline : what to display when offline
- format_online : what to display when online
- hide_when_offline: hide the module output when offline (default False)
- mode: default mode to display is 'ip' or 'status' (click to toggle)
- timeout : how long before deciding we're offline
@author ultrabug
"""
from time import time
try:
# python3
from urllib.request import urlopen
except:
from urllib2 import urlopen
class Py3status:
"""
"""
# available configuration parameters
cache_timeout = 30
format = '{ip}'
format_offline = '■'
format_online = '●'
hide_when_offline = False
mode = 'ip'
timeout = 5
def on_click(self, i3s_output_list, i3s_config, event):
"""
Toggle between display modes 'ip' and 'status'
"""
if self.mode == 'ip':
self.mode = 'status'
else:
self.mode = 'ip'
def _get_my_ip(self):
"""
"""
try:
ip = urlopen('http://ipecho.net/plain',
timeout=self.timeout).read()
ip = ip.decode('utf-8')
except Exception:
ip = None
return ip
def whatismyip(self, i3s_output_list, i3s_config):
"""
"""
ip = self._get_my_ip()
response = {'cached_until': time() + self.cache_timeout}
if ip is None and self.hide_when_offline:
response['full_text'] = ''
elif ip is not None:
if self.mode == 'ip':
response['full_text'] = self.format.format(ip=ip)
else:
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_bad': '#FF0000',
'color_degraded': '#FFFF00',
'color_good': '#00FF00'
}
while True:
print(x.whatismyip([], config))
sleep(1)
|