added unit to config file to diff between metric or imperial

This commit is contained in:
simon 2021-02-07 16:09:24 +07:00
parent 7a43294f7f
commit f591633bda
3 changed files with 14 additions and 11 deletions

View File

@ -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:

View File

@ -2,4 +2,5 @@
[api]
openweathermap_api_key = aaaa1111aaaa1111aaaa1111aaaa1111
lat = 40.71
lon = -74.00
lon = -74.00
unit = metric

View File

@ -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()