Dotfiles, utilities, and other apparatus.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.2 KiB

  1. #!/usr/bin/python3
  2. import requests
  3. import textwrap
  4. # Docs:
  5. # - https://www.weather.gov/documentation/services-web-api
  6. # - https://weather-gov.github.io/api/general-faqs
  7. # markers for vimwiki:
  8. print('[[/lyons-colorado]] [[/weather]]')
  9. print()
  10. print('{{{')
  11. # This address is geocoded from the forecast property in
  12. # https://api.weather.gov/points/40.226,-105.2713
  13. # r = requests.get('https://api.weather.gov/gridpoints/BOU/55,84/forecast/hourly')
  14. r = requests.get('https://api.weather.gov/gridpoints/BOU/55,84/forecast')
  15. data = r.json()
  16. # Pull out forecast periods from API
  17. for period in data['properties']['periods']:
  18. precip = period['probabilityOfPrecipitation']['value']
  19. if not precip:
  20. precip = ''
  21. else:
  22. precip = ", {0}% chance precip".format(precip)
  23. if not period['name']:
  24. prefix = period['startTime']
  25. else:
  26. prefix = period['name']
  27. print('{0}: {1[temperature]}°{1[temperatureUnit]}, wind {1[windSpeed]} {1[windDirection]}{2}\n [{1[shortForecast]}]'.format(prefix, period, precip))
  28. if period['detailedForecast']:
  29. print(textwrap.fill(period['detailedForecast'], width=70, initial_indent=' ', subsequent_indent=' ') + "\n")
  30. print('}}}')