From 5befc5a6f260e2283863212b39749d5ed05afb63 Mon Sep 17 00:00:00 2001 From: lassulus Date: Thu, 19 May 2022 19:16:09 +0200 Subject: l radio news: use openweather api --- lass/2configs/radio/weather_for_ips.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lass/2configs/radio/weather_for_ips.py (limited to 'lass/2configs/radio/weather_for_ips.py') diff --git a/lass/2configs/radio/weather_for_ips.py b/lass/2configs/radio/weather_for_ips.py new file mode 100644 index 00000000..8d9a2e7b --- /dev/null +++ b/lass/2configs/radio/weather_for_ips.py @@ -0,0 +1,33 @@ +import geoip2.database +import fileinput +import json +import requests +import os + + +geoip = geoip2.database.Reader(os.environ['MAXMIND_GEOIP_DB']) +seen = {} +output = [] +for ip in fileinput.input(): + location = geoip.city(ip.strip()) + if location.city.geoname_id not in seen: + seen[location.city.geoname_id] = True + weather_api_key = os.environ['OPENWEATHER_API_KEY'] + url = ( + f'https://api.openweathermap.org/data/2.5/onecall' + f'?lat={location.location.latitude}' + f'&lon={location.location.longitude}' + f'&appid={weather_api_key}' + f'&units=metric' + ) + resp = requests.get(url) + weather = json.loads(resp.text)['current'] + output.append( + f'Weather report for {location.city.name}' + f', {location.country.name}. ' + f'Currently it is {weather["weather"][0]["description"]} outside ' + f'with a temperature of {weather["temp"]} degrees ' + f'and a windspeed of {weather["wind_speed"]} meter per second. ' + ) + +print('\n'.join(output)) -- cgit v1.2.3