#!/usr/bin/env python3 # You need to somehow make the variables OWM_LAT, OWM_LON, OWM_APIKEY # available in the environment before using this. import requests import sys import os from block import * WEATHER_URL = "https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&units=metric&appid={apikey}" class Weather(Block): def execute(self): url = WEATHER_URL.format( lat=os.environ['OWM_LAT'], lon=os.environ['OWM_LON'], apikey=os.environ['OWM_APIKEY'], ) weather = requests.get(url).json() self.icon_from_id(weather['weather'][0]['icon']) return '{temp:.1f}°C {wind_bft} {windspeed:.1f}m/s {wind_icon}'.format( temp=weather['main']['temp'], wind_bft=self.wind_bft(weather['wind']['speed']), wind_icon=self.wind_icon(weather['wind']['deg']), windspeed=weather['wind']['speed'], ) def icon_from_id(self, icon_id): # https://openweathermap.org/weather-conditions # http://nerdfonts.com/?set=nf-weather-#cheat-sheet if icon_id.startswith('01'): # clear sky if icon_id[2] == 'd': self.set_icon('\ue30d') # nf-weather-day_sunny self.set_icon_color('yellow') else: self.set_icon('\ue32b') # nf-weather-night_clear self.set_icon_color('gray') elif icon_id.startswith('02'): # few clouds if icon_id[2] == 'd': self.set_icon('\ue30c') # nf-weather-day_sunny_overcast else: self.set_icon('\ue37b') # nf-weather-night_partly_cloudy elif icon_id.startswith('03') or icon_id.startswith('04'): # scattered clouds / broken clouds if icon_id[2] == 'd': self.set_icon('\ue302') # nf-weather-day_cloudy else: self.set_icon('\ue32e') # nf-weather-night_cloudy elif icon_id.startswith('09'): # shower rain if icon_id[2] == 'd': self.set_icon('\ue309') # nf-weather-day_showers else: self.set_icon('\ue334') # nf-weather-night_showers elif icon_id.startswith('10'): # rain if icon_id[2] == 'd': self.set_icon('\ue308') # nf-weather-day_rain else: self.set_icon('\ue333') # nf-weather-night_rain elif icon_id.startswith('11'): # thunderstorm if icon_id[2] == 'd': self.set_icon('\ue30f') # nf-weather-day_thunderstorm else: self.set_icon('\ue338') # nf-weather-night_thunderstorm elif icon_id.startswith('13'): # snow if icon_id[2] == 'd': self.set_icon('\ue30a') # nf-weather-day_snow else: self.set_icon('\ue335') # nf-weather-night_snow elif icon_id.startswith('50'): # mist if icon_id[2] == 'd': self.set_icon('\ue303') # nf-weather-day_fog else: self.set_icon('\ue346') # nf-weather-night_fog def wind_bft(self, speed): scale = [ [0.0, '\ue3b9', 'white'], # 0 BFT [0.5, '\ue3b0', 'white'], # 1 BFT [1.6, '\ue3b1', 'white'], # 2 BFT [3.4, '\ue3b2', 'white'], # 3 BFT [5.5, '\ue3b3', 'white'], # 4 BFT [8.0, '\ue3b4', 'white'], # 5 BFT [10.8, '\ue3b5', 'yellow'], # 6 BFT [13.9, '\ue3b6', 'yellow'], # 7 BFT [17.2, '\ue3b7', 'yellow'], # 8 BFT [20.8, '\ue3b8', 'red'], # 9 BFT [24.5, '\ue3b9', 'red'], # 10 BFT [28.5, '\ue3ba', 'red'], # 11 BFT [32.7, '\ue3bb', 'red'], # 12 BFT ] selected = None for scale_item in scale: if scale_item[0] <= speed: selected = scale_item return self.color_text(selected[1], selected[2]) def wind_icon(self, direction): icons = [ '\ue353', # nf-weather-direction_up '\ue352', # nf-weather-direction_up_right '\ue349', # nf-weather-direction_right '\ue380', # nf-weather-direction_down_right '\ue340', # nf-weather-direction_down '\ue33f', # nf-weather-direction_down_left '\ue344', # nf-weather-direction_left '\ue37f', # nf-weather-direction_up_left '\ue353', # nf-weather-direction_up ] return icons[round(direction / 45)] def main(): block = Weather(None) block.render() if __name__ == '__main__': main()