l radio: add c-base weather
This commit is contained in:
parent
32966b38ae
commit
4555a8858d
@ -3,32 +3,43 @@ import fileinput
|
|||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
import os
|
import os
|
||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
geoip = geoip2.database.Reader(os.environ['MAXMIND_GEOIP_DB'])
|
geoip = geoip2.database.Reader(os.environ['MAXMIND_GEOIP_DB'])
|
||||||
seen = {}
|
seen = {}
|
||||||
output = []
|
output = []
|
||||||
for ip in fileinput.input():
|
for ip in fileinput.input():
|
||||||
location = geoip.city(ip.strip())
|
if "80.147.140.51" in ip:
|
||||||
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)
|
|
||||||
output.append(
|
output.append(
|
||||||
f'Weather report for {location.city.name}, {location.country.name}. '
|
'Weather report for c-base, space.'
|
||||||
f'It is {weather["current"]["weather"][0]["description"]} outside '
|
'It is empty space outside '
|
||||||
f'with a temperature of {weather["current"]["temp"]:.1f} degrees, '
|
'with a temperature of -270 degrees, '
|
||||||
f'a wind speed of {weather["current"]["wind_speed"]:.1f} meters per second '
|
'a lightspeed of 299792 kilometers per second '
|
||||||
f'and a humidity of {weather["current"]["humidity"]} percent. '
|
'and a humidity of Not a Number percent. '
|
||||||
f'The probability of precipitation is {weather["hourly"][0]["pop"] * 100:.0f} percent. '
|
f'The probability of reincarnation is {random.randrange(0, 100)} percent.'
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
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)
|
||||||
|
output.append(
|
||||||
|
f'Weather report for {location.city.name}, {location.country.name}. '
|
||||||
|
f'It is {weather["current"]["weather"][0]["description"]} outside '
|
||||||
|
f'with a temperature of {weather["current"]["temp"]:.1f} degrees, '
|
||||||
|
f'a wind speed of {weather["current"]["wind_speed"]:.1f} meters per second '
|
||||||
|
f'and a humidity of {weather["current"]["humidity"]} percent. '
|
||||||
|
f'The probability of precipitation is {weather["hourly"][0]["pop"] * 100:.0f} percent. '
|
||||||
|
)
|
||||||
|
|
||||||
print('\n'.join(output))
|
print('\n'.join(output))
|
||||||
|
Loading…
Reference in New Issue
Block a user