summaryrefslogtreecommitdiff
path: root/Chap14ProjFetchWeatherData.py
diff options
context:
space:
mode:
Diffstat (limited to 'Chap14ProjFetchWeatherData.py')
-rw-r--r--Chap14ProjFetchWeatherData.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/Chap14ProjFetchWeatherData.py b/Chap14ProjFetchWeatherData.py
new file mode 100644
index 0000000..ddfd3aa
--- /dev/null
+++ b/Chap14ProjFetchWeatherData.py
@@ -0,0 +1,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.
+
+
+