Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ======================================================
- # DarkskyJSON.py
- # ------------------------------------------------------
- # Created for Full Circle Magazine #146
- # Written by G.D. Walters
- # Copyright (c) 2019 by G.D. Walters
- # This source code is released under the MIT License
- # Creation date: 21 May, 2019
- # ======================================================
- import json
- import requests
- from datetime import datetime
- # ======================================================
- # Comment out this section to test with local file
- # ======================================================
- host = 'https://api.darksky.net/forecast'
- api_key = '{SECRETKEY}'
- # Austin Lat/Lon
- lat = 30.2672
- lon = -97.7431
- timeout = 10 # 10 second timeout
- excludes = 'exclude=minutely,hourly'
- url = '{host}/{api_key}/{lat},{lon}?{excludes}'.format(host=host,
- api_key=api_key,
- lat=lat,
- lon=lon,
- excludes=excludes)
- # ======================================================
- # To use "advanced" options like language or units, you
- # can use the following url string. You can get the language
- # and unit values from the API documentation page...
- # ======================================================
- # url = '{host}/{api_key}/{lat},{lon}?{excludes}&{unit}&{lang}'.format(
- # host=host,
- # api_key=api_key,
- # lat=lat,
- # lon=lon,
- # excludes=excludes,
- # unit=unittouse,
- # lang=langtouse)
- print(url)
- # Now start a session using requests and send a .get to obtain the response
- session = requests.Session()
- response = session.get(url, timeout=timeout).json()
- # ======================================================
- # Comment out this section and uncomment the above section to get live data
- # ======================================================
- # ~ localfile = 'sampledata.json' # '30.2672,-97.7431-2.json'
- # ~ with open(localfile) as f:
- # ~ response = json.load(f)
- print("Current weather:\n")
- tim = response['currently']['time']
- print(datetime.fromtimestamp(tim).strftime("%a %m/%d/%Y %H:%M:%S"))
- currents = response['currently']
- summary = currents['summary']
- print("Summary: {0}".format(summary))
- print(" POP: {0}%".format(response['currently']['precipProbability']*100))
- if 'precipType' in response['currently']:
- print(" Precip Type: {0}".format(response['currently']['precipType']))
- else:
- print(' preciptype NOT available')
- print(' Temperature: {0}'.format(response['currently']['temperature']))
- print(' Feels like: {0}'.format(response['currently']['apparentTemperature']))
- print(' Dew point: {0}'.format(response['currently']['dewPoint']))
- print(' Humidity: {0}%'.format((response['currently']['humidity']*100)))
- print(' Pressure: {0}'.format(response['currently']['pressure']))
- print(' Windspeed: {0}'.format(response['currently']['windSpeed']))
- print(' Windgusts: {0}'.format(response['currently']['windGust']))
- print(' Wind Bearing: {0}'.format(response['currently']['windBearing']))
- print(' Cloudcover: {0}%'.format(response['currently']['cloudCover']*100))
- print(' Visibility: {0} miles'.format(response['currently']['visibility']))
- print(' Icon: {0}'.format(response['currently']['icon']))
- print('-----------------------------------')
- print('Weekly forecast:\n')
- # Weekly forecast
- weekly = response['daily']['data']
- ltemp = weekly[0]['temperatureLow']
- print(ltemp)
- for i in weekly:
- print(' {0}'.format((datetime.fromtimestamp(i['time']).strftime("%a %m/%d/%Y"))))
- print(' Summary: {0}: '.format(i['summary']))
- print(' High Temperature: {0}: '.format(i['temperatureHigh']))
- print(' Low Temperature: {0}: '.format(i['temperatureLow']))
- print(' POP: {0}%: '.format(int(i['precipProbability']*100)))
- print(' Expected Windspeed {0}: '.format(i['windSpeed']))
- print(' Expected Windgusts {0}: '.format(i['windGust']))
- print(' Icon {0}: '.format(i['icon']))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement