import GeoIP import subprocess from i3pystatus import IntervalModule, formatp class MyIP(IntervalModule): settings = ( "format", "format_hide", "format_down", "timeout", "color", ) interval = 15 format = "{country_name} {country_code} {ip}" format_hide = format format_down = "" timeout = 5 color = "#FFFFFF" on_leftclick = "switch_hide" on_rightclick = "run" def run(self): command = ["curl", "api.ipify.org"] try: p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) ip = p.communicate(timeout=self.timeout)[0].decode().strip() except Exception: return self.disable() gi = GeoIP.GeoIP(GeoIP.GEOIP_STANDARD) country_code = gi.country_code_by_addr(ip) country_name = gi.country_name_by_addr(ip) if not ip or not country_code: return self.disable() fdict = { "country_name": country_name, "country_code": country_code, "ip": ip } self.output = { "full_text": formatp(self.format, **fdict).strip(), "color": self.color } def disable(self): self.output = { "full_text": self.format_down, "color": self.color } def switch_hide(self): self.format, self.format_hide = self.format_hide, self.format self.run()