-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip.py
More file actions
executable file
·69 lines (52 loc) · 1.77 KB
/
Copy pathip.py
File metadata and controls
executable file
·69 lines (52 loc) · 1.77 KB
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
#!/usr/bin/python
import os
import sys
from gi.repository import Gtk, GObject
from gi.repository import AppIndicator3 as appindicator
import requests
from collections import namedtuple
# Set constants
Resources = namedtuple('Resources', ("icon", "url", "period"))
resources = Resources(
os.path.join(os.path.dirname(__file__), "images/flags"),
'http://www.telize.com/geoip',
10000)
class IPIndicator:
def __init__(self):
self.ind = appindicator.Indicator.new("ip-indicator", "", appindicator.IndicatorCategory.APPLICATION_STATUS)
self.ind.set_status(appindicator.IndicatorStatus.ACTIVE)
self.ind.set_menu(self._setup_menu())
self.ind.set_icon_theme_path(resources.icon)
def _setup_menu(self):
menu = Gtk.Menu()
quit = Gtk.MenuItem('Quit')
quit.connect("activate", self._quit)
quit.show()
menu.append(quit)
return menu
@property
def ip_geo_info(self):
try:
resp = requests.get(resources.url, timeout=10)
resp.raise_for_status()
resp = resp.json()
result = '{cty}:{sep}{ip}'.format(
cty=resp.get('city', 'Tunnel used'),
sep=' '*5,
**resp), resp.get('country_code', 'index').lower()
except Exception as e:
result = 'No connection with geoip service.', 'index'
return result
def _refresh(self):
ip, flag = self.ip_geo_info
self.ind.set_icon(flag)
self.ind.set_label(ip, '')
GObject.timeout_add(resources.period, self._refresh)
def main(self):
self._refresh()
Gtk.main()
def _quit(self, widget):
sys.exit(0)
if __name__ == "__main__":
indicator = IPIndicator()
indicator.main()