external_ip: add require internet
This commit is contained in:
parent
1e078c249b
commit
a7db805db7
@ -1,4 +1,5 @@
|
|||||||
from i3pystatus import IntervalModule, formatp
|
from i3pystatus import IntervalModule, formatp
|
||||||
|
from i3pystatus.core.util import internet, require
|
||||||
|
|
||||||
import GeoIP
|
import GeoIP
|
||||||
import urllib.request
|
import urllib.request
|
||||||
@ -16,6 +17,7 @@ class ExternalIP(IntervalModule):
|
|||||||
* {country_code} the country code of the country from the IP (eg. 'US')
|
* {country_code} the country code of the country from the IP (eg. 'US')
|
||||||
* {ip} the ip
|
* {ip} the ip
|
||||||
"""
|
"""
|
||||||
|
interval = 15
|
||||||
|
|
||||||
settings = (
|
settings = (
|
||||||
"format",
|
"format",
|
||||||
@ -28,7 +30,6 @@ class ExternalIP(IntervalModule):
|
|||||||
("timeout", "timeout in seconds when the http request is taking too much time"),
|
("timeout", "timeout in seconds when the http request is taking too much time"),
|
||||||
)
|
)
|
||||||
|
|
||||||
interval = 15
|
|
||||||
format = "{country_name} {country_code} {ip}"
|
format = "{country_name} {country_code} {ip}"
|
||||||
format_hide = "{country_code}"
|
format_hide = "{country_code}"
|
||||||
format_down = "Timeout"
|
format_down = "Timeout"
|
||||||
@ -42,19 +43,25 @@ class ExternalIP(IntervalModule):
|
|||||||
on_leftclick = "switch_hide"
|
on_leftclick = "switch_hide"
|
||||||
on_rightclick = "run"
|
on_rightclick = "run"
|
||||||
|
|
||||||
def run(self):
|
@require(internet)
|
||||||
|
def get_external_ip(self):
|
||||||
try:
|
try:
|
||||||
request = urllib.request.urlopen(self.ip_website,
|
request = urllib.request.urlopen(self.ip_website,
|
||||||
timeout=self.timeout)
|
timeout=self.timeout)
|
||||||
ip = request.read().decode().strip()
|
return request.read().decode().strip()
|
||||||
except Exception:
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
ip = self.get_external_ip()
|
||||||
|
if not ip:
|
||||||
return self.disable()
|
return self.disable()
|
||||||
|
|
||||||
gi = GeoIP.GeoIP(GeoIP.GEOIP_STANDARD)
|
gi = GeoIP.GeoIP(GeoIP.GEOIP_STANDARD)
|
||||||
country_code = gi.country_code_by_addr(ip)
|
country_code = gi.country_code_by_addr(ip)
|
||||||
country_name = gi.country_name_by_addr(ip)
|
country_name = gi.country_name_by_addr(ip)
|
||||||
|
|
||||||
if not ip or not country_code:
|
if not country_code:
|
||||||
return self.disable() # fail here in the case of a bad IP
|
return self.disable() # fail here in the case of a bad IP
|
||||||
|
|
||||||
fdict = {
|
fdict = {
|
||||||
|
Loading…
Reference in New Issue
Block a user