/
ibm_watson_weather.py
41 lines (34 loc) · 1.6 KB
/
ibm_watson_weather.py
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
35
36
37
38
39
40
41
# Using the IBM Bluemix Weather Company API
# Bruce Elgort
# July 9, 2016
# Version 1.0
# IBM Weather Company Docs: https://console.ng.bluemix.net/docs/services/Weather/weather_rest_apis.html#rest_apis
import requests
import json
def get_weather(zip):
username = 'your username'
password = 'your password'
watsonUrl = 'https://twcservice.mybluemix.net/api/weather/v1/location/' + zip + ':4:US' + '/observations.json?language=en-US'
try:
r = requests.get(watsonUrl,auth=(username,password))
return r.text
except:
return False
def display_weather(results):
print()
print('Here is the weather for {0}'.format(results['observation']['obs_name']))
print('{0:20} {1:<10}'.format('Current Temperature:',str(results['observation']['temp']) + '° and ' + results['observation']['wx_phrase']))
print('{0:20} {1:<10}'.format('Feels Like: ',str(results['observation']['feels_like']) + '°'))
print('{0:20} {1:<10}'.format('Low Temp: ',str(results['observation']['min_temp']) + '°'))
print('{0:20} {1:<10}'.format('High Temp: ',str(results['observation']['max_temp']) + '°'))
print('{0:20} {1:<10}'.format('Winds:',str(results['observation']['wspd']) + ' mph coming from the ' + results['observation']['wdir_cardinal']))
def get_weather():
zip = input('Enter US ZIP code to get weather for:\n')
results = get_weather(zip)
if results != False:
results = json.loads(str(results))
display_weather(results)
else:
print('Something went wrong :-(')
if __name__ == '__main__':
get_weather()