[modules] Use central location libary instead of individual

This commit is contained in:
tobi-wan-kenobi 2020-04-15 13:26:08 +02:00
parent 74c25ba2ee
commit 8bb47c14aa
4 changed files with 15 additions and 43 deletions

View file

@ -30,6 +30,7 @@ import core.widget
import core.decorators import core.decorators
import util.format import util.format
import util.location
SYMBOL = { SYMBOL = {
'GBP': u'£', 'EUR': u'', 'USD': u'$', 'JPY': u'¥', 'KRW': u'' 'GBP': u'£', 'EUR': u'', 'USD': u'$', 'JPY': u'¥', 'KRW': u''
@ -38,12 +39,6 @@ DEFAULT_DEST = 'USD,EUR,auto'
DEFAULT_SRC = 'GBP' DEFAULT_SRC = 'GBP'
API_URL = 'https://markets.ft.com/data/currencies/ajax/conversion?baseCurrency={}&comparison={}' API_URL = 'https://markets.ft.com/data/currencies/ajax/conversion?baseCurrency={}&comparison={}'
LOCATION_URL = 'https://ipvigilante.com/'
def get_local_country():
r = requests.get(LOCATION_URL)
location = r.json()
return location['data']['country_name']
def load_country_to_currency(): def load_country_to_currency():
return [ return [
@ -1079,7 +1074,7 @@ class Module(core.module.Module):
def find_local_currency(self): def find_local_currency(self):
"""Use geolocation lookup to find local currency""" """Use geolocation lookup to find local currency"""
try: try:
country = get_local_country() country = util.location.country()
currency_map = load_country_to_currency() currency_map = load_country_to_currency()
return currency_map.get(country, DEFAULT_SRC) return currency_map.get(country, DEFAULT_SRC)
except: except:

View file

@ -1,33 +1,17 @@
"""Displays public IP address """Displays public IP address
Requires the following python packages:
* requests
Parameters:
* publicip.region: us-central (default), us-east, us-west, uk, de, pl, nl
* publicip.service: web address that returns plaintext ip address (ex. 'http://l2.io/ip')
""" """
from requests import get
import core.module import core.module
import core.widget import core.widget
import core.decorators import core.decorators
import util.location
class Module(core.module.Module): class Module(core.module.Module):
@core.decorators.every(minutes=60) @core.decorators.every(minutes=60)
def __init__(self, config): def __init__(self, config):
super().__init__(config, core.widget.Widget(self.public_ip)) super().__init__(config, core.widget.Widget(self.public_ip))
self.__avail__regions = {'us-east':'http://checkip.amazonaws.com',
'us-central':'http://checkip.amazonaws.com',
'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'}
self.__region = self.parameter('region', 'us-central')
self.__service = self.parameter('service', '')
self.__ip = '' self.__ip = ''
@ -36,11 +20,7 @@ class Module(core.module.Module):
def update(self): def update(self):
try: try:
if self.__service: self.__ip = util.location.public_ip()
self.address = self.__service
else:
self.address = self.__avail__regions[self.__region]
self.__ip = get(self.address).text.rstrip()
except Exception: except Exception:
self.__ip = 'n/a' self.__ip = 'n/a'

View file

@ -7,7 +7,7 @@ Requires the following python packages:
* requests * requests
Parameters: Parameters:
* weather.location: Set location, defaults to 'auto' for getting location from http://ipinfo.io * weather.location: Set location, defaults to 'auto' for getting location automatically from a web service
If set to a comma-separated list, left-click and right-click can be used to rotate the locations. If set to a comma-separated list, left-click and right-click can be used to rotate the locations.
Locations should be city names or city ids. Locations should be city names or city ids.
* weather.unit: metric (default), kelvin, imperial * weather.unit: metric (default), kelvin, imperial
@ -21,6 +21,7 @@ import core.widget
import core.input import core.input
import util.format import util.format
import util.location
import re import re
@ -101,10 +102,7 @@ class Module(core.module.Module):
weather_url = 'http://api.openweathermap.org/data/2.5/weather?appid={}'.format(self.__apikey) weather_url = 'http://api.openweathermap.org/data/2.5/weather?appid={}'.format(self.__apikey)
weather_url = '{}&units={}'.format(weather_url, self.__unit) weather_url = '{}&units={}'.format(weather_url, self.__unit)
if self.__location[self.__index] == 'auto': if self.__location[self.__index] == 'auto':
# TODO: make that a general "provider" coord = util.location.coordinates()
location_url = 'http://ipinfo.io/json'
location = requests.get(location_url).json()
coord = location['loc'].split(',')
weather_url = '{url}&lat={lat}&lon={lon}'.format(url=weather_url, lat=coord[0], lon=coord[1]) weather_url = '{url}&lat={lat}&lon={lon}'.format(url=weather_url, lat=coord[0], lon=coord[1])
elif self.__location[self.__index].isdigit(): elif self.__location[self.__index].isdigit():
weather_url = '{url}&id={id}'.format(url=weather_url, id=self.__location[self.__index]) weather_url = '{url}&id={id}'.format(url=weather_url, id=self.__location[self.__index])

View file

@ -7,7 +7,7 @@ Requires the following executable:
Parameters: Parameters:
* redshift.location : location provider, either of 'auto' (default), 'geoclue2', * redshift.location : location provider, either of 'auto' (default), 'geoclue2',
'ipinfo' (requires the requests package), or 'manual' 'ipinfo' or 'manual'
'auto' uses whatever redshift is configured to do 'auto' uses whatever redshift is configured to do
* redshift.lat : latitude if location is set to 'manual' * redshift.lat : latitude if location is set to 'manual'
* redshift.lon : longitude if location is set to 'manual' * redshift.lon : longitude if location is set to 'manual'
@ -15,7 +15,6 @@ Parameters:
import re import re
import threading import threading
import requests
import core.module import core.module
import core.widget import core.widget
@ -23,6 +22,7 @@ import core.input
import core.decorators import core.decorators
import util.cli import util.cli
import util.location
def get_redshift_value(module): def get_redshift_value(module):
widget = module.widget() widget = module.widget()
@ -75,14 +75,13 @@ class Module(core.module.Module):
if self.parameter('location', '') == 'ipinfo': if self.parameter('location', '') == 'ipinfo':
# override lon/lat with ipinfo # override lon/lat with ipinfo
try: try:
location_url = 'http://ipinfo.io/json' location = util.location.coordinates()
location = requests.get(location_url).json() self.set('lat', location[0])
self.parameter('lat', location['loc'].split(',')[0]) self.set('lon', location[1])
self.parameter('lon', location['loc'].split(',')[1]) self.set('location', 'manual')
self.parameter('location', 'manual')
except Exception: except Exception:
# Fall back to geoclue2. # Fall back to geoclue2.
self.parameter('location', 'geoclue2') self.set('location', 'geoclue2')
self._text = '' self._text = ''