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
5.1 kB 145 lines
1import geoip2.database 2import fileinput 3import json 4import requests 5import os 6import random 7 8 9# https://open-meteo.com/en/docs 10def weather_code_representation(code: int) -> str: 11 if code == 0: 12 return 'clear sky' 13 if code == 1: 14 return 'mainly clear' 15 if code == 2: 16 return 'partly cloudy' 17 if code == 3: 18 return 'overcast' 19 if code == 45: 20 return 'fog' 21 if code == 48: 22 return 'depositing rime fog' 23 if code == 51: 24 return 'light drizzle' 25 if code == 53: 26 return 'moderate drizzle' 27 if code == 55: 28 return 'heavy drizzle' 29 if code == 56: 30 return 'light freezing drizzle' 31 if code == 57: 32 return 'heavy freezing drizzle' 33 if code == 61: 34 return 'light rain' 35 if code == 63: 36 return 'moderate rain' 37 if code == 65: 38 return 'heavy rain' 39 if code == 66: 40 return 'light freezing rain' 41 if code == 67: 42 return 'heavy freezing rain' 43 if code == 71: 44 return 'light snow' 45 if code == 73: 46 return 'moderate snow' 47 if code == 75: 48 return 'heavy snow' 49 if code == 77: 50 return 'light snow grains' 51 if code == 80: 52 return 'light rain showers' 53 if code == 81: 54 return 'moderate rain showers' 55 if code == 82: 56 return 'heavy rain showers' 57 if code == 85: 58 return 'light snow showers' 59 if code == 86: 60 return 'heavy snow showers' 61 if code == 95: 62 return 'thunderstorm' 63 if code == 96: 64 return 'thunderstorm with light hail' 65 if code == 99: 66 return 'thunderstorm with heavy hail' 67 return f'unknown weather code {code}' 68 69 70def downfall_representation(precipitation: float) -> str: 71 if precipitation > 0: 72 return f' the precipitation is {precipitation} millimeter' 73 else: 74 return '' 75 76geoip = geoip2.database.Reader(os.environ['MAXMIND_GEOIP_DB']) 77seen = {} 78output = [] 79ips = list(fileinput.input()) 80if len(ips) > 5: 81 output.append('weather report.') 82 for ip in fileinput.input(): 83 if "80.147.140.51" in ip: 84 output.append( 85 'Weather report for c-base.' 86 'temperature of -270 degrees, ' 87 ) 88 else: 89 try: 90 location = geoip.city(ip.strip()) 91 if location.city.geoname_id not in seen: 92 seen[location.city.geoname_id] = True 93 url = ( 94 f'https://api.open-meteo.com/v1/forecast' 95 f'?latitude={location.location.latitude}' 96 f'&longitude={location.location.longitude}' 97 f'&current=temperature_2m,weather_code' 98 ) 99 resp = requests.get(url) 100 weather = json.loads(resp.text) 101 output.append( 102 f'{location.city.name}, {location.country.name}. ' 103 f'{weather_code_representation(weather["current"]["weather_code"])}. ' 104 f'{weather["current"]["temperature_2m"]} degrees.' 105 ) 106 except: # noqa E722 107 pass 108 109else: 110 for ip in ips: 111 if "80.147.140.51" in ip: 112 output.append( 113 'Weather report for c-base, space. ' 114 'It is empty space outside ' 115 'with a temperature of -270 degrees, ' 116 'a lightspeed of 299792 kilometers per second ' 117 'and a humidity of Not a Number percent. ' 118 f'The probability of reincarnation is {random.randrange(0, 100)} percent. ' 119 ) 120 else: 121 try: 122 location = geoip.city(ip.strip()) 123 if location.city.geoname_id not in seen: 124 seen[location.city.geoname_id] = True 125 url = ( 126 f'https://api.open-meteo.com/v1/forecast' 127 f'?latitude={location.location.latitude}' 128 f'&longitude={location.location.longitude}' 129 '&current=temperature_2m,precipitation,weather_code,relative_humidity_2m,wind_speed_10m' 130 '&wind_speed_unit=ms' 131 ) 132 resp = requests.get(url) 133 weather = json.loads(resp.text) 134 output.append( 135 f'Weather report for {location.city.name}, {location.country.name}. ' 136 f'It is {weather_code_representation(weather["current"]["weather_code"])} outside' 137 f'{downfall_representation(weather["current"]["precipitation"])}, ' 138 f'with a temperature of {weather["current"]["temperature_2m"]} degrees, ' 139 f'a wind speed of {weather["current"]["wind_speed_10m"]} meters per second ' 140 f'and a humidity of {weather["current"]["relative_humidity_2m"]} percent. ' 141 ) 142 except: # noqa E722 143 pass 144 145print('\n'.join(output))