added unit to config file to diff between metric or imperial
This commit is contained in:
parent
7a43294f7f
commit
f591633bda
|
@ -63,6 +63,7 @@ Create a file called *config* in the same directory as the *weather.py* script a
|
||||||
data from *config.sample*:
|
data from *config.sample*:
|
||||||
* Replace the dummy *openweathermap_api_key* with your key.
|
* Replace the dummy *openweathermap_api_key* with your key.
|
||||||
* Add *lat* and *lon* values with the latitude and longitude values from your location.
|
* 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:
|
### Install:
|
||||||
None standard Python dependencies:
|
None standard Python dependencies:
|
||||||
|
|
|
@ -2,4 +2,5 @@
|
||||||
[api]
|
[api]
|
||||||
openweathermap_api_key = aaaa1111aaaa1111aaaa1111aaaa1111
|
openweathermap_api_key = aaaa1111aaaa1111aaaa1111aaaa1111
|
||||||
lat = 40.71
|
lat = 40.71
|
||||||
lon = -74.00
|
lon = -74.00
|
||||||
|
unit = metric
|
|
@ -39,20 +39,21 @@ def get_config(config_path):
|
||||||
config_parser = configparser.ConfigParser()
|
config_parser = configparser.ConfigParser()
|
||||||
config_parser.read(config_path)
|
config_parser.read(config_path)
|
||||||
# return false on error
|
# 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')
|
api_key = config_parser.get('api', 'openweathermap_api_key')
|
||||||
lat = config_parser.get('api', 'lat')
|
lat = config_parser.get('api', 'lat')
|
||||||
lon = config_parser.get('api', 'lon')
|
lon = config_parser.get('api', 'lon')
|
||||||
|
unit = config_parser.get('api', 'unit')
|
||||||
else:
|
else:
|
||||||
print('config parse error')
|
print('config parse error')
|
||||||
return False
|
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 """
|
""" get celsius and icon_id based on lat and lon """
|
||||||
url = "https://api.openweathermap.org/data/2.5/weather?&units=metric&appid=" \
|
url = "https://api.openweathermap.org/data/2.5/weather?&units=" + unit \
|
||||||
+ api_key + "&lat=" + lat + "&lon=" + lon
|
+ "&appid=" + api_key + "&lat=" + lat + "&lon=" + lon
|
||||||
# try up to 3 times
|
# try up to 3 times
|
||||||
for i in range(1, 4):
|
for i in range(1, 4):
|
||||||
try:
|
try:
|
||||||
|
@ -71,11 +72,11 @@ def get_data(api_key, lat, lon):
|
||||||
return celsius_pretty, icon
|
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 next three days forecast """
|
||||||
# get data
|
# get data
|
||||||
url = "https://api.openweathermap.org/data/2.5/onecall?lat=" + lat + "&lon=" + lon \
|
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)
|
response = requests.get(url, timeout=5)
|
||||||
json = response.json()
|
json = response.json()
|
||||||
notify_list = []
|
notify_list = []
|
||||||
|
@ -101,7 +102,7 @@ def get_forecast(api_key, lat, lon):
|
||||||
def main():
|
def main():
|
||||||
""" main function to run """
|
""" main function to run """
|
||||||
# make the call
|
# 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 three lines for i3blocks
|
||||||
print(icon, celsius_pretty)
|
print(icon, celsius_pretty)
|
||||||
print(icon, celsius_pretty)
|
print(icon, celsius_pretty)
|
||||||
|
@ -112,11 +113,11 @@ def main():
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# get config file path relative to script file
|
# get config file path relative to script file
|
||||||
config_path = os.path.dirname(sys.argv[0]) + '/config'
|
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
|
# check for button clicked
|
||||||
env = os.environ.copy()
|
env = os.environ.copy()
|
||||||
button = env.get('BLOCK_BUTTON', False)
|
button = env.get('BLOCK_BUTTON', False)
|
||||||
if button == '1':
|
if button == '1':
|
||||||
get_forecast(api_key, lat, lon)
|
get_forecast(api_key, lat, lon, unit)
|
||||||
# regular
|
# regular
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue