summaryrefslogtreecommitdiffstats
path: root/lass/2configs/radio/weather_for_ips.py
diff options
context:
space:
mode:
authormakefu <github@syntax-fehler.de>2022-05-27 00:35:31 +0200
committermakefu <github@syntax-fehler.de>2022-05-27 00:35:31 +0200
commit0b84135ffd69750974fd5ad7721b64ccd772a4a6 (patch)
tree5093b06989d9cba87328c6b27261e2e1b9b9ef01 /lass/2configs/radio/weather_for_ips.py
parent9ef120c4ed528cb60a9348c8474c2e23912d4960 (diff)
parente92a58fe0f4edbf047ff4dd89866ae0991d37350 (diff)
Merge remote-tracking branch 'lass/master'
Diffstat (limited to 'lass/2configs/radio/weather_for_ips.py')
-rw-r--r--lass/2configs/radio/weather_for_ips.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/lass/2configs/radio/weather_for_ips.py b/lass/2configs/radio/weather_for_ips.py
new file mode 100644
index 000000000..8d9a2e7bc
--- /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))