2022-07-05 20:05:25 +02:00
|
|
|
|
"""
|
2022-07-06 12:51:19 +02:00
|
|
|
|
Displays zero or more of:
|
|
|
|
|
* Public IP address
|
|
|
|
|
* Country Name
|
|
|
|
|
* Country Code
|
|
|
|
|
* City Name
|
|
|
|
|
* Geographic Coordinates\
|
|
|
|
|
|
2022-07-05 20:05:25 +02:00
|
|
|
|
Maximum refresh interval should be 5 minutes to avoid free SLA breach from providers
|
|
|
|
|
Note: 1 request/5 minutes is 8640 requests/month
|
|
|
|
|
Provider information contained in core.location
|
|
|
|
|
|
2022-07-06 12:51:19 +02:00
|
|
|
|
Left mouse click on the widget forces immediate update
|
2022-07-05 20:05:25 +02:00
|
|
|
|
|
2022-07-06 12:51:19 +02:00
|
|
|
|
Parameters:
|
|
|
|
|
publicip.format: Format string (defaults to ‘{ip} ({country_code})’)
|
2022-07-05 20:05:25 +02:00
|
|
|
|
|
2022-07-06 12:51:19 +02:00
|
|
|
|
Available format strings:
|
|
|
|
|
ip
|
|
|
|
|
country_name
|
|
|
|
|
country_code
|
|
|
|
|
city_name
|
|
|
|
|
coordinates
|
2022-07-05 20:05:25 +02:00
|
|
|
|
|
2022-07-06 12:51:19 +02:00
|
|
|
|
Examples:
|
|
|
|
|
bumblebee-status -m publicip -p publicip.format="{ip} ({country_code})"
|
|
|
|
|
bumblebee-status -m publicip -p publicip.format="{ip} which is in {city_name}"
|
|
|
|
|
bumblebee-status -m publicip -p publicip.format="Your packets are right here: {coordinates}"
|
2020-04-11 09:15:29 +02:00
|
|
|
|
"""
|
|
|
|
|
|
2020-04-11 09:20:19 +02:00
|
|
|
|
import core.module
|
|
|
|
|
import core.widget
|
|
|
|
|
import core.decorators
|
2022-07-05 20:05:25 +02:00
|
|
|
|
import core.input
|
|
|
|
|
import util.format
|
2020-04-15 13:26:08 +02:00
|
|
|
|
import util.location
|
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
|
2020-04-11 09:20:19 +02:00
|
|
|
|
class Module(core.module.Module):
|
2022-07-06 14:37:29 +02:00
|
|
|
|
@core.decorators.every(minutes=60)
|
2020-04-26 16:39:24 +02:00
|
|
|
|
def __init__(self, config, theme):
|
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.public_ip))
|
2020-04-11 09:20:19 +02:00
|
|
|
|
|
2022-07-06 12:51:19 +02:00
|
|
|
|
# Immediate update (override default) when left click on widget
|
2022-07-05 20:05:25 +02:00
|
|
|
|
core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.__click_update)
|
|
|
|
|
|
2022-07-06 12:51:19 +02:00
|
|
|
|
self.__ip = "" # Public IP address
|
|
|
|
|
self.__country_name = "" # Country name associated with public IP address
|
|
|
|
|
self.__country_code = "" # Country code associated with public IP address
|
|
|
|
|
self.__city_name = "" # City name associated with public IP address
|
2022-07-06 14:37:29 +02:00
|
|
|
|
self.__coordinates = "" # Coordinates assoicated with public IP address
|
2022-07-06 12:51:19 +02:00
|
|
|
|
|
|
|
|
|
# By default show: <ip> (<2 letter country code>)
|
|
|
|
|
self._format = self.parameter("format", "{ip} ({country_code})")
|
2022-07-05 20:05:25 +02:00
|
|
|
|
|
|
|
|
|
def __click_update(self, event):
|
|
|
|
|
util.location.reset()
|
2020-04-11 09:15:29 +02:00
|
|
|
|
|
|
|
|
|
def public_ip(self, widget):
|
2022-07-06 12:51:19 +02:00
|
|
|
|
if not self.__ip:
|
|
|
|
|
return "Error fetching IP"
|
2022-07-05 20:05:25 +02:00
|
|
|
|
else:
|
2022-07-06 12:51:19 +02:00
|
|
|
|
return self._format.format(
|
|
|
|
|
ip=self.__ip,
|
|
|
|
|
country_name=self.__country_name,
|
|
|
|
|
country_code=self.__country_code,
|
|
|
|
|
city_name=self.__city_name,
|
|
|
|
|
coordinates=self.__coordinates,
|
|
|
|
|
)
|
2020-04-11 09:15:29 +02:00
|
|
|
|
|
2020-04-11 09:20:19 +02:00
|
|
|
|
def update(self):
|
2020-04-11 09:15:29 +02:00
|
|
|
|
try:
|
2022-07-06 14:37:29 +02:00
|
|
|
|
__info = util.location.location_info()
|
|
|
|
|
self.__ip = __info["public_ip"]
|
|
|
|
|
self.__country_name = __info["country"]
|
|
|
|
|
self.__country_code = __info["country_code"]
|
|
|
|
|
self.__city_name = __info["city_name"]
|
|
|
|
|
__lat = __info["latitude"]
|
|
|
|
|
__lon = __info["longitude"]
|
2022-07-06 12:51:19 +02:00
|
|
|
|
__lat = "{:.2f}".format(__lat)
|
|
|
|
|
__lon = "{:.2f}".format(__lon)
|
|
|
|
|
__output = __lat + "°N" + "," + " " + __lon + "°E"
|
|
|
|
|
self.__coordinates = __output
|
2020-04-11 09:15:29 +02:00
|
|
|
|
except Exception:
|
2022-07-06 12:51:19 +02:00
|
|
|
|
pass
|
2022-07-05 20:05:25 +02:00
|
|
|
|
|
2020-04-11 09:20:19 +02:00
|
|
|
|
|
2022-07-05 20:05:25 +02:00
|
|
|
|
# vim: tabstop=7 expandtab shiftwidth=4 softtabstop=4
|