This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

superconfig / 2configs / services / radio / weather_for_ips.py
1.9 kB 48 lines
1import geoip2.database 2import fileinput 3import json 4import requests 5import os 6import random 7 8 9geoip = geoip2.database.Reader(os.environ['MAXMIND_GEOIP_DB']) 10seen = {} 11output = [] 12for ip in fileinput.input(): 13 if "80.147.140.51" in ip: 14 output.append( 15 'Weather report for c-base, space. ' 16 'It is empty space outside ' 17 'with a temperature of -270 degrees, ' 18 'a lightspeed of 299792 kilometers per second ' 19 'and a humidity of Not a Number percent. ' 20 f'The probability of reincarnation is {random.randrange(0, 100)} percent. ' 21 ) 22 else: 23 try: 24 location = geoip.city(ip.strip()) 25 if location.city.geoname_id not in seen: 26 seen[location.city.geoname_id] = True 27 weather_api_key = os.environ['OPENWEATHER_API_KEY'] 28 url = ( 29 f'https://api.openweathermap.org/data/2.5/onecall' 30 f'?lat={location.location.latitude}' 31 f'&lon={location.location.longitude}' 32 f'&appid={weather_api_key}' 33 f'&units=metric' 34 ) 35 resp = requests.get(url) 36 weather = json.loads(resp.text) 37 output.append( 38 f'Weather report for {location.city.name}, {location.country.name}. ' 39 f'It is {weather["current"]["weather"][0]["description"]} outside ' 40 f'with a temperature of {weather["current"]["temp"]:.1f} degrees, ' 41 f'a wind speed of {weather["current"]["wind_speed"]:.1f} meters per second ' 42 f'and a humidity of {weather["current"]["humidity"]} percent. ' 43 f'The probability of precipitation is {weather["hourly"][0]["pop"] * 100:.0f} percent. ' 44 ) 45 except: # noqa E722 46 pass 47 48print('\n'.join(output))