-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerts.py
44 lines (35 loc) · 1.13 KB
/
alerts.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
42
43
44
#!/usr/local/bin/python
# coding: latin-1
"""
alerts.py -- Display weather alerts for 95051 using Weather Underground's API
"""
# MLPrince 2012 。◕ ‿ ◕。
import urllib2
import json
from private import APIKEY
def get_alert():
"""
Makes a call to wunderground's API and displays weather alerts
if there are any.
"""
# Open url, make call to the API using an API key.
f = urllib2.urlopen('http://api.wunderground.com/api/' + APIKEY + '/alerts/q/95051.json')
json_string = f.read() # Save response as a json string.
parsed_json = json.loads(json_string) # Parse the json string.
# If there are alerts...
if parsed_json['alerts']:
# Print the date, expiration date, and message to the console.
for alert in parsed_json['alerts']:
print "Date: %s" % alert['date']
print "Expires: %s" % alert['expires']
print alert['message']
# Otherwise...
else:
# Print the following string.
print "There are no alerts today."
# Hang up the phone.
f.close()
def main():
get_alert()
if __name__ == '__main__':
main()