Advertisement
gregwa

FCM146 - DarkSky API Demo

Jun 5th, 2019
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.47 KB | None | 0 0
  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. # ======================================================
  10. import json
  11. import requests
  12. from datetime import datetime
  13. # ======================================================
  14. # Comment out this section to test with local file
  15. # ======================================================
  16. host = 'https://api.darksky.net/forecast'
  17. api_key = '{SECRETKEY}'
  18. # Austin Lat/Lon
  19. lat = 30.2672
  20. lon = -97.7431
  21. timeout = 10   # 10 second timeout
  22. excludes = 'exclude=minutely,hourly'
  23. url = '{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)
  41. print(url)
  42. # Now start a session using requests and send a .get to obtain the response
  43. session = requests.Session()
  44. response = 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.  
  52. print("Current weather:\n")
  53.  
  54. tim = response['currently']['time']
  55. print(datetime.fromtimestamp(tim).strftime("%a %m/%d/%Y %H:%M:%S"))
  56.  
  57. currents = response['currently']
  58. summary = currents['summary']
  59.  
  60. print("Summary: {0}".format(summary))
  61. print("   POP: {0}%".format(response['currently']['precipProbability']*100))
  62. if 'precipType' in response['currently']:
  63.     print("   Precip Type: {0}".format(response['currently']['precipType']))
  64. else:
  65.     print('   preciptype NOT available')
  66.  
  67. print('   Temperature: {0}'.format(response['currently']['temperature']))
  68. print('   Feels like: {0}'.format(response['currently']['apparentTemperature']))
  69. print('   Dew point: {0}'.format(response['currently']['dewPoint']))
  70. print('   Humidity: {0}%'.format((response['currently']['humidity']*100)))
  71. print('   Pressure: {0}'.format(response['currently']['pressure']))
  72. print('   Windspeed: {0}'.format(response['currently']['windSpeed']))
  73. print('   Windgusts: {0}'.format(response['currently']['windGust']))
  74. print('   Wind Bearing: {0}'.format(response['currently']['windBearing']))
  75. print('   Cloudcover: {0}%'.format(response['currently']['cloudCover']*100))
  76. print('   Visibility: {0} miles'.format(response['currently']['visibility']))
  77. print('   Icon: {0}'.format(response['currently']['icon']))
  78. print('-----------------------------------')
  79. print('Weekly forecast:\n')
  80. # Weekly forecast
  81. weekly = response['daily']['data']
  82. ltemp = weekly[0]['temperatureLow']
  83. print(ltemp)
  84. for 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']))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement