You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

my_ip.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import GeoIP
  2. import subprocess
  3. from i3pystatus import IntervalModule, formatp
  4. class MyIP(IntervalModule):
  5. settings = (
  6. "format",
  7. "format_hide",
  8. "format_down",
  9. "timeout",
  10. "color",
  11. )
  12. interval = 15
  13. format = "{country_name} {country_code} {ip}"
  14. format_hide = format
  15. format_down = ""
  16. timeout = 5
  17. color = "#FFFFFF"
  18. on_leftclick = "switch_hide"
  19. on_rightclick = "run"
  20. def run(self):
  21. command = ["curl", "api.ipify.org"]
  22. try:
  23. p = subprocess.Popen(command, stdout=subprocess.PIPE,
  24. stderr=subprocess.DEVNULL)
  25. ip = p.communicate(timeout=self.timeout)[0].decode().strip()
  26. except Exception:
  27. return self.disable()
  28. gi = GeoIP.GeoIP(GeoIP.GEOIP_STANDARD)
  29. country_code = gi.country_code_by_addr(ip)
  30. country_name = gi.country_name_by_addr(ip)
  31. if not ip or not country_code:
  32. return self.disable()
  33. fdict = {
  34. "country_name": country_name,
  35. "country_code": country_code,
  36. "ip": ip
  37. }
  38. self.output = {
  39. "full_text": formatp(self.format, **fdict).strip(),
  40. "color": self.color
  41. }
  42. def disable(self):
  43. self.output = {
  44. "full_text": self.format_down,
  45. "color": self.color
  46. }
  47. def switch_hide(self):
  48. self.format, self.format_hide = self.format_hide, self.format
  49. self.run()