changed weather api

This commit is contained in:
Justin 2023-05-23 16:09:20 +08:00 committed by GitHub
parent b01ab720c1
commit d38296ec2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -661,74 +661,79 @@ def updateNews(api_key, logf):
def updateWeather(api_key, logf): def updateWeather(api_key, logf):
max_cities = 30
try: try:
gn = geocoders.GeoNames(username='fintic') gn = geocoders.GeoNames(username='fintic')
weather_codes = {
0: ['Clear', 'Clear sky'],
1: ['Clouds', 'few clouds'], 2: ['Clouds', 'scattered clouds'], 3: ['Clouds', 'overcast clouds'],
45: ['Fog', 'Fog'], 48: ['Fog', 'depositing rime fog'],
51: ['Drizzle', 'Light'], 53: ['Drizzle', 'moderate'], 55: ['Drizzle', 'dense'], 56: ['Drizzle', 'light'], 57: ['Drizzle', 'dense'],
61: ['Rain', 'light rain'], 63: ['Rain', 'moderate rain'], 65: ['Rain', 'very heavy rain'],
66: ['Rain', 'freezing rain'], 67: ['Rain', 'freezing rain'],
71: ['Snow', 'slight'], 73: ['Snow', 'moderate'], 75: ['Snow', 'heavy'], 77: ['Snow', 'Snow grains'], 85: ['Snow', 'slight'], 86: ['Snow', 'heavy'],
80: ['Rain', 'light intensity shower rain'], 81: ['Rain', 'shower rain'], 82: ['Rain', 'heavy intensity shower rain'],
95: ['Thunderstorm', 'Slight or moderate'], 96: ['Thunderstorm', 'slight hail'], 99: ['Thunderstorm', 'heavy hail']
}
f = open('csv/daily_weather.json', 'r') f = open('csv/daily_weather.json', 'r')
all_daily_settings = json.load(f) all_daily_settings = json.load(f)
f.close() f.close()
f = open('csv/current_weather.json', 'r') f = open('csv/current_weather.json', 'r')
all_current_settings = json.load(f) all_current_settings = json.load(f)
f.close() f.close()
current_locations = list(all_current_settings['locations'].keys()) current_locations = list(all_current_settings['locations'].keys())
daily_locations = list(all_daily_settings['locations'].keys()) daily_locations = list(all_daily_settings['locations'].keys())
all_locations = list(set(current_locations + daily_locations)) all_locations = list(set(current_locations + daily_locations))
current_weathers = {} current_weathers = {}
daily_weathers = {} daily_weathers = {}
for location in all_locations: for location in all_locations:
loc = gn.geocode(location) loc = gn.geocode(location)
current_weather = {} current_weather = {}
lat = loc.latitude lat = loc.latitude
lon = loc.longitude lon = loc.longitude
url = 'https://api.openweathermap.org/data/2.5/onecall?lat={}&units=metric&lon={}&appid={}'.format(lat, lon, api_key) url = 'https://api.open-meteo.com/v1/forecast?latitude={}&longitude={}&hourly=apparent_temperature,temperature_2m,relativehumidity_2m,precipitation_probability,weathercode,cloudcover,visibility,windspeed_10m,winddirection_10m,uv_index,is_day&daily=weathercode,temperature_2m_max,temperature_2m_min&current_weather=true&timezone=UTC'.format(lat, lon)
r = requests.get(url) r = requests.get(url).json()
weather = r.json()['current'] times = r['hourly']['time']
hour_now = datetime.now(pytz.utc).strftime('%Y-%m-%dT%H:00')
current_weather['main_weather'] = weather['weather'][0]['main'] index_pos = times.index(hour_now)
current_weather['description'] = weather['weather'][0]['description']
current_weather['temp'] = weather['temp']
current_weather['min_temp'] = r.json()['daily'][0]['temp']['min']
current_weather['max_temp'] = r.json()['daily'][0]['temp']['max']
current_weather['feels_like'] = weather['feels_like']
current_weather['humidity'] = weather['humidity']
current_weather['clouds'] = weather['clouds']
current_weather['wind_speed'] = weather['wind_speed']
current_weather['wind_direction'] = weather['wind_deg']
current_weather['visibility'] = weather['visibility']
current_weather['uv'] = weather['uvi']
current_weather['rain_chance'] = r.json()['hourly'][0]['pop']
main_weather_code = r['hourly']['weathercode'][index_pos]
current_weather['main_weather'] = weather_codes[main_weather_code][0]
current_weather['description'] = weather_codes[main_weather_code][1]
current_weather['temp'] = r['hourly']['temperature_2m'][index_pos]
current_weather['min_temp'] = r['daily']['temperature_2m_min'][0]
current_weather['max_temp'] = r['daily']['temperature_2m_max'][0]
current_weather['feels_like'] = r['hourly']['apparent_temperature'][index_pos]
current_weather['humidity'] = r['hourly']['relativehumidity_2m'][index_pos]
current_weather['clouds'] = r['hourly']['cloudcover'][index_pos]
current_weather['visibility'] = r['hourly']['visibility'][index_pos]
current_weather['uv'] = r['hourly']['uv_index'][index_pos]
current_weather['rain_chance'] = r['hourly']['precipitation_probability'][index_pos]
current_weather['wind_speed'] = r['hourly']['windspeed_10m'][index_pos]
current_weather['wind_direction'] = r['hourly']['winddirection_10m'][index_pos]
current_weather['is_day'] = r['hourly']['is_day'][index_pos]
if location in current_locations: if location in current_locations:
current_weathers[location] = current_weather current_weathers[location] = current_weather
daily_weather = [] daily_weather = []
daily = r.json()['daily'] daily = r['daily']
for day in daily: for i in range(0,7):
dct = {} dct = {}
dct['main_weather'] = day['weather'][0]['main'] daily_weather_code = daily['weathercode'][i]
dct['description'] = day['weather'][0]['description'] dct['main_weather'] = weather_codes[daily_weather_code][0]
dct['min_temp'] = day['temp']['min'] dct['description'] = weather_codes[daily_weather_code][1]
dct['max_temp'] = day['temp']['max'] dct['min_temp'] = daily['temperature_2m_min'][i]
dct['max_temp'] = daily['temperature_2m_max'][i]
daily_weather.append(dct) daily_weather.append(dct)
#add relevant urrent information to first day in daily # add relevant urrent information to first day in daily
daily_weather[0]['temp'] = weather['temp'] daily_weather[0]['temp'] = current_weather['temp']
daily_weather[0]['rain_chance'] = current_weather['rain_chance'] daily_weather[0]['rain_chance'] = current_weather['rain_chance']
daily_weather[0]['humidity'] = current_weather['humidity'] daily_weather[0]['humidity'] = current_weather['humidity']
daily_weather[0]['wind_speed'] = current_weather['wind_speed'] daily_weather[0]['wind_speed'] = current_weather['wind_speed']
@ -741,16 +746,14 @@ def updateWeather(api_key, logf):
if location in daily_locations: if location in daily_locations:
daily_weathers[location] = daily_weather daily_weathers[location] = daily_weather
all_current_settings['locations'] = current_weathers all_current_settings['locations'] = current_weathers
all_daily_settings['locations'] = daily_weathers all_daily_settings['locations'] = daily_weathers
f = open( "csv/current_weather.json", 'w+' ) f = open("csv/current_weather.json", 'w+')
json.dump( all_current_settings, f) json.dump(all_current_settings, f)
f.close() f.close()
f = open( "csv/daily_weather.json", 'w+' ) f = open("csv/daily_weather.json", 'w+')
json.dump( all_daily_settings, f) json.dump(all_daily_settings, f)
f.close() f.close()
except: except: