bumblebee-status/modules/contrib/publicip.py

48 lines
1.5 KiB
Python
Raw Normal View History

2020-04-11 09:15:29 +02:00
"""Displays public IP address
Requires the following python packages:
* requests
Parameters:
* publicip.region: us-central (default), us-east, us-west, uk, de, pl, nl
2020-04-11 09:15:51 +02:00
* publicip.service: web address that returns plaintext ip address (ex. 'http://l2.io/ip')
2020-04-11 09:15:29 +02:00
"""
2020-04-11 09:20:19 +02:00
from requests import get
import core.module
import core.widget
import core.decorators
class Module(core.module.Module):
@core.decorators.every(minutes=60)
def __init__(self, config):
super().__init__(config, core.widget.Widget(self.public_ip))
self.__avail__regions = {'us-east':'http://checkip.amazonaws.com',
'us-central':'http://checkip.amazonaws.com',
2020-04-11 09:15:51 +02:00
'us-west':'http://ipv4bot.whatismyipaddress.com',
'pl':'http://ip.42.pl/raw',
'de':'http://myexternalip.com/raw',
'nl':'http://tnx.nl/ip',
'uk':'http://ident.me'}
2020-04-11 09:20:19 +02:00
self.__region = self.parameter('region', 'us-central')
self.__service = self.parameter('service', '')
self.__ip = ''
2020-04-11 09:15:29 +02:00
def public_ip(self, widget):
2020-04-11 09:20:19 +02:00
return self.__ip
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:
2020-04-11 09:20:19 +02:00
if self.__service:
self.address = self.__service
2020-04-11 09:15:29 +02:00
else:
2020-04-11 09:20:19 +02:00
self.address = self.__avail__regions[self.__region]
self.__ip = get(self.address).text.rstrip()
2020-04-11 09:15:29 +02:00
except Exception:
2020-04-11 09:20:19 +02:00
self.__ip = 'n/a'
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4