From f591633bda1153e1493ddc323658de1771cf5379 Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 7 Feb 2021 16:09:24 +0700 Subject: [PATCH] added unit to config file to diff between metric or imperial --- README.md | 1 + weather_applet/config.sample | 3 ++- weather_applet/weather.py | 21 +++++++++++---------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 188550a..ffca800 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ Create a file called *config* in the same directory as the *weather.py* script a data from *config.sample*: * Replace the dummy *openweathermap_api_key* with your key. * Add *lat* and *lon* values with the latitude and longitude values from your location. +* Add unit of measurement: *metric* or *imperial* to get results in C° or F°. ### Install: None standard Python dependencies: diff --git a/weather_applet/config.sample b/weather_applet/config.sample index b8d4154..1cb2fb6 100644 --- a/weather_applet/config.sample +++ b/weather_applet/config.sample @@ -2,4 +2,5 @@ [api] openweathermap_api_key = aaaa1111aaaa1111aaaa1111aaaa1111 lat = 40.71 -lon = -74.00 \ No newline at end of file +lon = -74.00 +unit = metric \ No newline at end of file diff --git a/weather_applet/weather.py b/weather_applet/weather.py index e6b6f2a..078c4f1 100755 --- a/weather_applet/weather.py +++ b/weather_applet/weather.py @@ -39,20 +39,21 @@ def get_config(config_path): config_parser = configparser.ConfigParser() config_parser.read(config_path) # return false on error - if config_parser.options('api') == ['openweathermap_api_key', 'lat', 'lon']: + if config_parser.options('api') == ['openweathermap_api_key', 'lat', 'lon', 'unit']: api_key = config_parser.get('api', 'openweathermap_api_key') lat = config_parser.get('api', 'lat') lon = config_parser.get('api', 'lon') + unit = config_parser.get('api', 'unit') else: print('config parse error') return False - return api_key, lat, lon + return api_key, lat, lon, unit -def get_data(api_key, lat, lon): +def get_data(api_key, lat, lon, unit): """ 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 + url = "https://api.openweathermap.org/data/2.5/weather?&units=" + unit \ + + "&appid=" + api_key + "&lat=" + lat + "&lon=" + lon # try up to 3 times for i in range(1, 4): try: @@ -71,11 +72,11 @@ def get_data(api_key, lat, lon): return celsius_pretty, icon -def get_forecast(api_key, lat, lon): +def get_forecast(api_key, lat, lon, unit): """ get next three days forecast """ # get data url = "https://api.openweathermap.org/data/2.5/onecall?lat=" + lat + "&lon=" + lon \ - + "&units=metric&exclude=current,minutely,hourly,alerts&appid=" + api_key + + "&units=" + unit + "&exclude=current,minutely,hourly,alerts&appid=" + api_key response = requests.get(url, timeout=5) json = response.json() notify_list = [] @@ -101,7 +102,7 @@ def get_forecast(api_key, lat, lon): def main(): """ main function to run """ # make the call - celsius_pretty, icon = get_data(api_key, lat, lon) + celsius_pretty, icon = get_data(api_key, lat, lon, unit) # print three lines for i3blocks print(icon, celsius_pretty) print(icon, celsius_pretty) @@ -112,11 +113,11 @@ def main(): if __name__ == '__main__': # 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) + api_key, lat, lon, unit = get_config(config_path) # check for button clicked env = os.environ.copy() button = env.get('BLOCK_BUTTON', False) if button == '1': - get_forecast(api_key, lat, lon) + get_forecast(api_key, lat, lon, unit) # regular main()