From 6be90d58306dd06e0fad00ef99ea2f175c64f2db Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Wed, 29 Sep 2021 07:55:47 +0200 Subject: [PATCH] polybar-weather: switch to MaxMind for location In the future, I can get a key from here: https://support.maxmind.com/getting-started-with-geolite2/. The issue with ip-api.com is that the city is often wrong. --- bin/polybar-weather | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/bin/polybar-weather b/bin/polybar-weather index 79c0dee..2f0932b 100755 --- a/bin/polybar-weather +++ b/bin/polybar-weather @@ -16,13 +16,21 @@ logger = logging.getLogger("polybar-weather") def get_location(): """Return current location as latitude/longitude tuple.""" - logger.debug("query ip-api.com for location") - r = requests.get("http://ip-api.com/json") + logger.debug("query MaxMind for location") + r = requests.get( + "https://www.maxmind.com/geoip/v2.1/city/me", + headers={"referer": "https://www.maxmind.com/en/locate-my-ip-address"}, + ) r.raise_for_status() data = r.json() logger.debug("current location data: %s", data) - logger.info(f'current location: {data["city"]}, {data["country"]}') - return ((data["lat"], data["lon"]), data["city"]) + logger.info( + f'current location: {data["city"]["names"]["en"]}, {data["country"]["names"]["en"]}' + ) + return ( + (data["location"]["latitude"], data["location"]["longitude"]), + data["city"]["names"]["en"], + ) def get_weather(apikey, latitude, longitude):