esp_time/darksky.py

60 lines
1.6 KiB
Python
Raw Normal View History

2019-11-23 20:21:10 +11:00
#!/usr/bin/env python3
#
# Pulls out only the required pieces for the esp_time.ino arduino program
# the ESP8266 doesn't have enough RAM to parse the whole JSON object.
# substitue in api_key, run this and place the output at a web server
# adjust ds_url in esp_time.ino to point at that web server!
import json
import urllib.request
ds_url = "https://api.darksky.net/forecast/";
ds_ll = "/-35.135,149.61";
ds_opts = "?units=auto"
api_key = "PUT YOUR API KEY HERE"
contents = urllib.request.urlopen(ds_url + api_key + ds_ll + ds_opts)
d = json.load(contents)
new = {}
current = d['currently']
today = d['daily']['data'][0]
tomorrow = d['daily']['data'][1]
day_after = d['daily']['data'][2]
new['current'] = {}
new['current']['temp'] = current['temperature']
new['current']['icon'] = current['icon']
new['today'] = {}
new['today']['max'] = today['temperatureMax']
new['today']['icon'] = today['icon']
new['tomorrow'] = {}
new['tomorrow']['max'] = tomorrow['temperatureMax']
new['tomorrow']['icon'] = tomorrow['icon']
new['day_after'] = {}
new['day_after']['max'] = day_after['temperatureMax']
new['day_after']['icon'] = day_after['icon']
with open('darksky.json', 'w') as output:
output.write(json.dumps(new))
output.close()
# OWM
# for forecast in d['list']:
# utcDate = datetime.datetime.strptime(forecast['dt_txt']+"+0000", "%Y-%m-%d %H:%M:%S%z")
# utcDate.replace(tzinfo=UTC)
# print("********")
# print(utcDate)
# print(utcDate.astimezone(local))
# print(forecast['weather'][0]['description'], forecast['weather'][0]['icon'], forecast['main']['temp'])