· 6 years ago · Jun 05, 2019, 10:26 AM
1# ======================================================
2# DarkskyJSON.py
3# ------------------------------------------------------
4# Created for Full Circle Magazine #146
5# Written by G.D. Walters
6# Copyright (c) 2019 by G.D. Walters
7# This source code is released under the MIT License
8# Creation date: 21 May, 2019
9# ======================================================
10import json
11import requests
12from datetime import datetime
13# ======================================================
14# Comment out this section to test with local file
15# ======================================================
16host = 'https://api.darksky.net/forecast'
17api_key = '{SECRETKEY}'
18# Austin Lat/Lon
19lat = 30.2672
20lon = -97.7431
21timeout = 10 # 10 second timeout
22excludes = 'exclude=minutely,hourly'
23url = '{host}/{api_key}/{lat},{lon}?{excludes}'.format(host=host,
24 api_key=api_key,
25 lat=lat,
26 lon=lon,
27 excludes=excludes)
28# ======================================================
29# To use "advanced" options like language or units, you
30# can use the following url string. You can get the language
31# and unit values from the API documentation page...
32# ======================================================
33# url = '{host}/{api_key}/{lat},{lon}?{excludes}&{unit}&{lang}'.format(
34# host=host,
35# api_key=api_key,
36# lat=lat,
37# lon=lon,
38# excludes=excludes,
39# unit=unittouse,
40# lang=langtouse)
41print(url)
42# Now start a session using requests and send a .get to obtain the response
43session = requests.Session()
44response = session.get(url, timeout=timeout).json()
45# ======================================================
46# Comment out this section and uncomment the above section to get live data
47# ======================================================
48# ~ localfile = 'sampledata.json' # '30.2672,-97.7431-2.json'
49# ~ with open(localfile) as f:
50# ~ response = json.load(f)
51
52print("Current weather:\n")
53
54tim = response['currently']['time']
55print(datetime.fromtimestamp(tim).strftime("%a %m/%d/%Y %H:%M:%S"))
56
57currents = response['currently']
58summary = currents['summary']
59
60print("Summary: {0}".format(summary))
61print(" POP: {0}%".format(response['currently']['precipProbability']*100))
62if 'precipType' in response['currently']:
63 print(" Precip Type: {0}".format(response['currently']['precipType']))
64else:
65 print(' preciptype NOT available')
66
67print(' Temperature: {0}'.format(response['currently']['temperature']))
68print(' Feels like: {0}'.format(response['currently']['apparentTemperature']))
69print(' Dew point: {0}'.format(response['currently']['dewPoint']))
70print(' Humidity: {0}%'.format((response['currently']['humidity']*100)))
71print(' Pressure: {0}'.format(response['currently']['pressure']))
72print(' Windspeed: {0}'.format(response['currently']['windSpeed']))
73print(' Windgusts: {0}'.format(response['currently']['windGust']))
74print(' Wind Bearing: {0}'.format(response['currently']['windBearing']))
75print(' Cloudcover: {0}%'.format(response['currently']['cloudCover']*100))
76print(' Visibility: {0} miles'.format(response['currently']['visibility']))
77print(' Icon: {0}'.format(response['currently']['icon']))
78print('-----------------------------------')
79print('Weekly forecast:\n')
80# Weekly forecast
81weekly = response['daily']['data']
82ltemp = weekly[0]['temperatureLow']
83print(ltemp)
84for i in weekly:
85 print(' {0}'.format((datetime.fromtimestamp(i['time']).strftime("%a %m/%d/%Y"))))
86 print(' Summary: {0}: '.format(i['summary']))
87 print(' High Temperature: {0}: '.format(i['temperatureHigh']))
88 print(' Low Temperature: {0}: '.format(i['temperatureLow']))
89 print(' POP: {0}%: '.format(int(i['precipProbability']*100)))
90 print(' Expected Windspeed {0}: '.format(i['windSpeed']))
91 print(' Expected Windgusts {0}: '.format(i['windGust']))
92 print(' Icon {0}: '.format(i['icon']))