commit b441bf0f3b190bb41f914d6fe67d0fbf353ee692
parent b1258795ece044976eedf99c54e797c8a1176471
Author: Daniel Moch <daniel@danielmoch.com>
Date: Mon, 9 Apr 2018 05:36:48 -0400
Only print UTF-8 characters if LANG indicates support
Diffstat:
1 file changed, 36 insertions(+), 14 deletions(-)
diff --git a/.local/lib/forecastio/weather.py b/.local/lib/forecastio/weather.py
@@ -13,7 +13,8 @@ def generate_forecast():
longitude = None
try:
- # I keep my api key for forecast.io in a separate text file so I don't accidentally publish it anywhere.
+ # I keep my api key for forecast.io in a separate text file so
+ # I don't accidentally publish it anywhere.
with open('forecastio_api.txt', 'r') as apifile:
api_key = apifile.read()
api_key = api_key.strip()
@@ -26,19 +27,24 @@ def generate_forecast():
zipcode = None
if zipcode is None:
- # Resolving location from IP is always going to be a best guess, but it's better than hardcoding location data
- latitude, longitude = requests.get('https://ipinfo.io').json()['loc'].split(',')
+ # Resolving location from IP is always going to be a best
+ # guess, but it's better than hardcoding location data
+ latitude, longitude = requests.get('https://ipinfo.io'). \
+ json()['loc'].split(',')
else:
try:
with open('zipcodeapi_api.txt', 'r') as zipcodeapifile:
zipcodeapi_key = zipcodeapifile.read()
zipcodeapi_key = zipcodeapi_key.strip()
except FileNotFoundError:
- sys.stdout.writelines(datetime.now().strftime("%m/%d/%Y %I:%M:%S:") + " Could not find zipcodeapi key\n")
+ sys.stdout.writelines(
+ datetime.now().strftime("%m/%d/%Y %I:%M:%S:") +
+ " Could not find zipcodeapi key\n")
exit()
- location = requests.get('https://www.zipcodeapi.com/rest/' + zipcodeapi_key + \
- '/info.json/' + zipcode + '/degrees').json()
+ location = requests.get('https://www.zipcodeapi.com/rest/' \
+ + zipcodeapi_key + '/info.json/' + zipcode + \
+ '/degrees').json()
latitude = location['lat']
longitude = location['lng']
@@ -48,22 +54,38 @@ def generate_forecast():
current_temp = forecast.currently()
day = forecast.daily()
- temp = str(current_temp.temperature) + "\u00b0 F"
+ if 'LANG' in os.environ and \
+ os.environ['LANG'].find('UTF-8') != -1:
+ degree_symbol = "\u00b0"
+ else:
+ degree_symbol = ""
+
+ temp = str(current_temp.temperature) + degree_symbol + " F"
high = str(int(math.ceil(day.data[0].temperatureMax)))
low = str(int(math.ceil(day.data[0].temperatureMin)))
- out_filename = os.path.join(os.environ['HOME'], '.local/var/forecastio/current_forecast.txt')
+ out_filename = os.path.join(
+ os.environ['HOME'],
+ '.local/var/forecastio/current_forecast.txt'
+ )
with open(out_filename, 'w') as outfile:
- outfile.write(temp + ", " + str(current_temp.summary) + ", " + high +"\u00b0/"+low + "\u00b0 F")
- except requests.packages.urllib3.exceptions.NewConnectionError:
+ outfile.write(temp + ", " + str(current_temp.summary) + \
+ ", " + high + degree_symbol + "/" +low + \
+ degree_symbol + " F")
+ except requests.exceptions.ConnectionError:
error = True
- sys.stdout.writelines(datetime.now().strftime("%m/%d/%Y %I:%M:%S:") + " Could not connect. Will try again \
- in 5 minutes.\n")
+ sys.stdout.writelines(
+ datetime.now().strftime("%m/%d/%Y %I:%M:%S:") +
+ " Could not connect. Will try again in 5 minutes.\n")
if not error:
- sys.stdout.writelines(datetime.now().strftime("%m/%d/%Y %I:%M:%S:") + " Successfully updated weather\n")
+ sys.stdout.writelines(
+ datetime.now().strftime("%m/%d/%Y %I:%M:%S:") +
+ " Successfully updated weather\n")
if __name__ == '__main__':
- sys.stdout.writelines(datetime.now().strftime("%m/%d/%Y %I:%M:%S:") + " Updating weather\n")
+ sys.stdout.writelines(
+ datetime.now().strftime("%m/%d/%Y %I:%M:%S:") +
+ " Updating weather\n")
generate_forecast()