summaryrefslogtreecommitdiff
path: root/Chap14ProjFetchWeatherData.py
blob: ddfd3aa57067b4adbd31d493604b09d934cd5e93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#! python3

# Chapter 14 Project Fetching Current Weather Data
# Prints the weather data for a location from the command line.

import json
import requests
import sys

if len(sys.argv) < 2:
    print('Usage: script.py location')
    sys.exit()
location = ' '.join(sys.argv[1:])

url = 'http://api.openweathermap.org/data/2.5/forecast/daily?q=%s&cnt=3' % (location)
response = requests.get(url)
response.raise_for_status()

weatherData = json.loads(response.text)
w = weatherData['list']
print('Current weather is %s:' % (location))
print(w[0]['weather'][0]['main'], '-', w[0]['weather'][0]['description'])
print()
print('Tomorrow:')
print(w[1]['weather'][0]['main'], '-', w[1]['weather'][0]['description'])
print()
print('Day after tomorrow:')
print(w[2]['weather'][0]['main'], '-', w[2]['weather'][0]['description'])

# 09/2016 - DOES NOT WORK! This script no longer works due to changes made by
# OpenWeatherMap.org and their API access.