Advertisement
mengyuxin

quickWeather.py

Jan 4th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. #! python3
  2. # quickWeather.py - Prints the weather for a location from the command line.
  3.  
  4. import json
  5. import requests
  6. import sys
  7.  
  8. # Compute location from command line arguments.
  9. if len(sys.argv) < 2:
  10.     print('Usage: quickWeather.py <location>')
  11.     sys.exit()
  12.  
  13. location = ' '.join(sys.argv[1:])
  14.  
  15. # Download the JSON data from OpenWeatherMap.org's API.
  16. url = 'http://api.openweathermap.org/data/2.5/forecast/daily?q=%s&cnt=3' % (location)
  17. response = requests.get(url)
  18. response.raise_for_status()
  19.  
  20. # Load JSON data into a Python variable.
  21. weatherData = json.loads(response.text)
  22.  
  23. # Print weather description.
  24. w = weatherData['list']
  25. print('Current weather in %s:' % (location))
  26. print(w[0]['weather'][0]['main'], '-', w[0]['weather'][0]['description'])
  27. print('\nTomorrow:')
  28. print(w[1]['weather'][0]['main'], '-', w[1]['weather'][0]['description'])
  29. print('\nDay after tomorrow:')
  30. print(w[2]['weather'][0]['main'], '-', w[2]['weather'][0]['description'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement