|
15 | 15 |
|
16 | 16 | from __future__ import print_function |
17 | 17 | from Adafruit_Thermal import * |
18 | | -import time, urllib, json |
| 18 | +import time, urllib.request, json |
19 | 19 | from PIL import Image, ImageDraw |
20 | 20 |
|
21 | | -API_KEY = "YOUR_API_KEY" |
| 21 | +API_KEY = "YOUR_OPEN_WEATHER_API_KEY" |
22 | 22 |
|
23 | | -LAT = "40.726019" |
24 | | -LONG = "-74.00536" |
| 23 | +cityName = "YOUR_CITY_NAME" |
25 | 24 |
|
26 | 25 | # Fetch weather data from DarkSky, parse resulting JSON |
27 | | -url = "https://api.darksky.net/forecast/"+API_KEY+"/"+LAT+","+LONG+"?exclude=[alerts,minutely,hourly,flags]&units=us" |
28 | | -response = urllib.urlopen(url) |
| 26 | +url = f"http://api.openweathermap.org/data/2.5/weather?q={cityName}&appid={API_KEY}" |
| 27 | +response = urllib.request.urlopen(url) |
29 | 28 | data = json.loads(response.read()) |
30 | | - |
| 29 | +print(data) |
31 | 30 | # Extract values relating to current temperature, humidity, wind |
32 | 31 |
|
33 | | -temperature = int(data['currently']['temperature']) |
34 | | -humidity = int(data['currently']['humidity'] * 100); |
35 | | -windSpeed = int(data['currently']['windSpeed']) |
36 | | -windDir = data['currently']['windBearing'] |
| 32 | +temperature = (int(data['main']['temp']) - 273.15) * 9/5 + 32 |
| 33 | +humidity = int(data['main']['humidity'] * 100); |
| 34 | +windSpeed = int(data['wind']['speed']) |
| 35 | +windDir = data['wind']['deg'] |
37 | 36 | windUnits = "mph" |
38 | 37 |
|
39 | 38 | # print(temperature) |
40 | | -# print(humidity) |
41 | | -# print(windSpeed) |
42 | | -# print(windDir) |
43 | | -# print(windUnits) |
| 39 | +# print(humidity) |
| 40 | +# print(windSpeed) |
| 41 | +# print(windDir) |
| 42 | +# print(windUnits) |
44 | 43 |
|
45 | 44 | # Although the Python Imaging Library does have nice font support, |
46 | 45 | # I opted here to use a raster bitmap for all of the glyphs instead. |
@@ -137,7 +136,7 @@ def numWidth(str, list): |
137 | 136 | if windSpeed > 0: |
138 | 137 | for winDirNum in range(len(DirAngle) - 1): |
139 | 138 | if windDir < DirAngle[winDirNum]: break |
140 | | -winDirNum+=1 |
| 139 | +winDirNum+=1 |
141 | 140 | w = Humidity.size[0] + 5 + numWidth(s, HumiDigit) |
142 | 141 | w2 = Wind.size[0] + 5 + numWidth(s2, HumiDigit) |
143 | 142 | if windSpeed > 0: |
|
0 commit comments