picowidget/open_meteo.py

63 lines
3.5 KiB
Python
Raw Normal View History

2024-11-19 20:19:14 +00:00
#Copyright 2024 Frederick Boniface
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import urequests
import config
def get_weather():
URL = f"https://api.open-meteo.com/v1/forecast?latitude={config.LATLONG[0]}&longitude={config.LATLONG[1]}&current=temperature_2m&daily=weather_code,temperature_2m_max,temperature_2m_min,sunrise,sunset,wind_speed_10m_max,wind_gusts_10m_max&timezone=Europe%2FLondon&forecast_days=1"
res = urequests.get(URL)
return parse_weather(res.json())
def parse_weather(weather):
output = {
"current_temp": weather['current']['temperature_2m'],
"max": weather['daily']['temperature_2m_max'][0],
"min": weather['daily']['temperature_2m_min'][0],
"sunrise": weather['daily']['sunrise'][0],
"sunset": weather['daily']['sunset'][0],
"icon": get_weather_icon(weather['daily']['weather_code'][0]),
}
return output
def get_weather_icon(code):
with open (weather_icon_paths[code], "rb") as file:
raw_bytes = file.read()
return raw_bytes
weather_icon_paths = {
0: "/ico/weather-sunny.bin", #Clear Sky
1: "/ico/weather-sunny.bin", #Mainly Clear
2: "/ico/weather-partly-cloudy.bin", #Partly Cloudy
3: "/ico/weather-cloudy.bin", #Overcast
45: "/ico/weather-fog.bin", #Fog
48: "/ico/weather-fog.bin", #Depositing Rime Fog
51: "/ico/weather-partly-rainy.bin", #Light Drizzle
53: "/ico/weather-partly-rainy.bin", #Moderate Drizzle
55: "/ico/weather-partly-rainy.bin", #Dense Drizzle
56: "/ico/weather-snowy-rainy.bin", #Light Freezing Drizzle
57: "/ico/weather-snowy-rainy.bin", #Dense Freezing Drizzle
61: "/ico/weather-rainy.bin", #Light Rain
63: "/ico/weather-rainy.bin", #Moderate Rain
65: "/ico/weather-pouring.bin", #Heavy Rain
66: "/ico/weather-hail.bin", #Light Freezing Rain
67: "/ico/weather-hail.bin", #Heavy Freezing Rain
71: "/ico/weather-snowy.bin", #Slight Snowfall
73: "/ico/weather-snowy.bin", #Moderate Snowfall
75: "/ico/weather-snowy-heavy.bin", #Heavy Snowfall
77: "/ico/weather-snowy-heavy.bin", #Snow Grains
80: "/ico/weather-partly-rainy.bin", #Slight Rain Showers
81: "/ico/weather-partly-rainy.bin", #Moderate Rain Showers
82: "/ico/weather-pouring.bin", #Violent Rain Showers
85: "/ico/weather-partly-snowy.bin", #Slight Snow Showers
86: "/ico/weather-partly-snowy.bin", #Heavy Snow Showers
95: "/ico/weather-partly-lightning.bin", #Slight or Moderate Thunderstorm
96: "/ico/weather-lightning.bin", #Thunderstorm with Slight Hail
99: "/ico/weather-lightning-rainy.bin", #Thunderstorm with Heavy Hail
}