diff --git a/weather_applet/config.sample b/weather_applet/config.sample new file mode 100644 index 0000000..b8d4154 --- /dev/null +++ b/weather_applet/config.sample @@ -0,0 +1,5 @@ +# replace with your values and remove .sample from filename +[api] +openweathermap_api_key = aaaa1111aaaa1111aaaa1111aaaa1111 +lat = 40.71 +lon = -74.00 \ No newline at end of file diff --git a/weather_applet/weather.py b/weather_applet/weather.py new file mode 100755 index 0000000..9045539 --- /dev/null +++ b/weather_applet/weather.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 + +import requests +from time import sleep +import configparser +import sys, os + + +iconlist = { + '01d': '', + '02d': '', + '03d': '', + '04d': '', + '09d': '', + '10d': '', + '11d': '', + '13d': '', + '50d': '🌫', + '01n': '', + '02n': '', + '03n': '', + '04n': '', + '09n': '', + '10n': '', + '11n': '', + '13n': '', + '50n': '🌫' +} + + +def get_config(config_path): + """ read out the .env file and return config values """ + # parse + config_parser = configparser.ConfigParser() + config_parser.read(config_path) + # return false on error + if config_parser.options('api') != ['openweathermap_api_key', 'lat', 'lon']: + print('config parse error') + return False + else: + api_key = config_parser.get('api', 'openweathermap_api_key') + lat = config_parser.get('api', 'lat') + lon = config_parser.get('api', 'lon') + return api_key, lat, lon + + +def get_data(api_key, lat, lon): + """ get celsius and icon_id based on lat and lon """ + url = "https://api.openweathermap.org/data/2.5/weather?&units=metric&appid=" \ + + api_key + "&lat=" + lat + "&lon=" + lon + # try up to 3 times + for i in range(1, 4): + try: + r = requests.get(url, timeout=5) + except: + sleep(int(i) * 30) + else: + break + # parse response + json = r.json() + celsius = round(json['main']['temp']) + celsius_pretty = str(celsius) + "°" + icon_ID = json['weather'][0]['icon'] + icon = iconlist.get(icon_ID) + # return + return celsius_pretty, icon + + +def main(): + """ main function to run """ + # get config file path relative to script file + config_path = os.path.dirname(sys.argv[0]) + '/config' + api_key, lat, lon = get_config(config_path) + # make the call + celsius_pretty, icon = get_data(api_key, lat, lon) + # print three lines for i3blocks + print(icon, celsius_pretty) + print(icon, celsius_pretty) + print() + + +# start from here +if __name__ == '__main__': + main() +