diff --git a/examples/weather.py b/examples/weather.py index 8f49691..fe9f0af 100644 --- a/examples/weather.py +++ b/examples/weather.py @@ -16,7 +16,8 @@ from __future__ import print_function from datetime import datetime import calendar -import urllib, json +import urllib +import json import time import os @@ -36,6 +37,7 @@ API_KEY = "YOUR API KEY" LAT = "22.345490" # Your Location LONG = "114.189945" # Your Location + def forecast_icon(idx): icon = data['daily']['data'][idx]['icon'] image = GRAPHICS_PATH + icon + ".png" @@ -45,29 +47,35 @@ def forecast_icon(idx): # Dumps one forecast line to the printer def forecast(idx): date = datetime.fromtimestamp(int(data['daily']['data'][idx]['time'])) - day = calendar.day_name[date.weekday()] - lo = data['daily']['data'][idx]['temperatureMin'] - hi = data['daily']['data'][idx]['temperatureMax'] - cond = data['daily']['data'][idx]['summary'] + day = calendar.day_name[date.weekday()] + lo = data['daily']['data'][idx]['temperatureMin'] + hi = data['daily']['data'][idx]['temperatureMax'] + cond = data['daily']['data'][idx]['summary'] print(date) print(day) print(lo) print(hi) - print(cond) - time.sleep(1) - printer.set(font='a', height=2, align='left', bold=False, double_height=False) + print(cond) + time.sleep(1) + printer.set( + font='a', + height=2, + align='left', + bold=False, + double_height=False) printer.text(day + ' \n ') time.sleep(5) # Sleep to prevent printer buffer overflow printer.text('\n') printer.image(forecast_icon(idx)) - printer.text('low ' + str(lo) ) + printer.text('low ' + str(lo)) printer.text(deg) - printer.text('\n') + printer.text('\n') printer.text(' high ' + str(hi)) printer.text(deg) - printer.text('\n') - printer.text(cond.replace(u'\u2013', '-').encode('utf-8')) # take care of pesky unicode dash - printer.text('\n \n') + printer.text('\n') + # take care of pesky unicode dash + printer.text(cond.replace(u'\u2013', '-').encode('utf-8')) + printer.text('\n \n') def icon(): @@ -76,10 +84,11 @@ def icon(): return image -deg = ' C' # Degree symbol on thermal printer, need to find a better way to use a proper degree symbol +deg = ' C' # Degree symbol on thermal printer, need to find a better way to use a proper degree symbol # if you want Fahrenheit change units= to 'us' -url = "https://api.darksky.net/forecast/"+API_KEY+"/"+LAT+","+LONG+"?exclude=[alerts,minutely,hourly,flags]&units=si" #change last bit to 'us' for Fahrenheit +url = "https://api.darksky.net/forecast/" + API_KEY + "/" + LAT + "," + LONG + \ + "?exclude=[alerts,minutely,hourly,flags]&units=si" # change last bit to 'us' for Fahrenheit response = urllib.urlopen(url) data = json.loads(response.read())