This repository has no description
0

Configure Feed

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

machines.radio: add shorter weather report for many listeners

+68 -35
+68 -35
2configs/services/radio/weather_for_ips.py
··· 9 9 geoip = geoip2.database.Reader(os.environ['MAXMIND_GEOIP_DB']) 10 10 seen = {} 11 11 output = [] 12 - for 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 12 + ips = list(fileinput.input()) 13 + if len(ips) > 5: 14 + output.append('weather report.') 15 + for ip in fileinput.input(): 16 + if "80.147.140.51" in ip: 17 + output.append( 18 + 'Weather report for c-base.' 19 + 'temperature of -270 degrees, ' 20 + ) 21 + else: 22 + try: 23 + location = geoip.city(ip.strip()) 24 + if location.city.geoname_id not in seen: 25 + seen[location.city.geoname_id] = True 26 + weather_api_key = os.environ['OPENWEATHER_API_KEY'] 27 + url = ( 28 + f'https://api.openweathermap.org/data/2.5/onecall' 29 + f'?lat={location.location.latitude}' 30 + f'&lon={location.location.longitude}' 31 + f'&appid={weather_api_key}' 32 + f'&units=metric' 33 + ) 34 + resp = requests.get(url) 35 + weather = json.loads(resp.text) 36 + output.append( 37 + f'{location.city.name}, {location.country.name}. ' 38 + f'{weather["current"]["weather"][0]["description"]}. ' 39 + f'{weather["current"]["temp"]:.1f} degrees.' 40 + ) 41 + except: # noqa E722 42 + pass 43 + 44 + else: 45 + for ip in fileinput.input(): 46 + if "80.147.140.51" in ip: 47 + output.append( 48 + 'Weather report for c-base, space. ' 49 + 'It is empty space outside ' 50 + 'with a temperature of -270 degrees, ' 51 + 'a lightspeed of 299792 kilometers per second ' 52 + 'and a humidity of Not a Number percent. ' 53 + f'The probability of reincarnation is {random.randrange(0, 100)} percent. ' 54 + ) 55 + else: 56 + try: 57 + location = geoip.city(ip.strip()) 58 + if location.city.geoname_id not in seen: 59 + seen[location.city.geoname_id] = True 60 + weather_api_key = os.environ['OPENWEATHER_API_KEY'] 61 + url = ( 62 + f'https://api.openweathermap.org/data/2.5/onecall' 63 + f'?lat={location.location.latitude}' 64 + f'&lon={location.location.longitude}' 65 + f'&appid={weather_api_key}' 66 + f'&units=metric' 67 + ) 68 + resp = requests.get(url) 69 + weather = json.loads(resp.text) 70 + output.append( 71 + f'Weather report for {location.city.name}, {location.country.name}. ' 72 + f'It is {weather["current"]["weather"][0]["description"]} outside ' 73 + f'with a temperature of {weather["current"]["temp"]:.1f} degrees, ' 74 + f'a wind speed of {weather["current"]["wind_speed"]:.1f} meters per second ' 75 + f'and a humidity of {weather["current"]["humidity"]} percent. ' 76 + f'The probability of precipitation is {weather["hourly"][0]["pop"] * 100:.0f} percent. ' 77 + ) 78 + except: # noqa E722 79 + pass 47 80 48 81 print('\n'.join(output))