Created
September 27, 2010 02:49
-
-
Save michaelschade/598527 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Michael A. Schade, www.mschade.me | |
URL = 'http://www.ksdk.com/weather/severe_weather/cancellations_closings/default.aspx' | |
closings = { | |
'School': [], | |
'Business': [], | |
} | |
def get_closings(): | |
"""Downloads and parses the KSDK school and business cancellation page, | |
returning only the data pertaining to closings that we need.""" | |
from urllib2 import urlopen | |
data = urlopen(URL).read() | |
from lxml import html | |
data = html.document_fromstring(data) | |
try: | |
data = data.get_element_by_id('schooclosings_dg') | |
except KeyError: | |
return None | |
data = data.findall('./tr/td/table/tr/td')[1::2] | |
return zip(*[iter(data)]*2) | |
def add_closings(): | |
"""Maintains the list of closed schools and businesses and notifies only | |
for new cancellations.""" | |
added = False | |
count = {'School': 0, 'Business': 0} | |
closings = get_closings() | |
if closings is None: | |
print "No closings at this time." | |
else: | |
for entity, place_type in closings: | |
entity = entity.text_content() | |
place_type = place_type.text_content() | |
if place_type == '': place_type = 'School' | |
if entity not in closings[place_type]: | |
added = True | |
closings[place_type].append(entity) | |
count[place_type] += 1 | |
print 'Adding new %s: %s' % (place_type, entity) | |
if added: | |
import time | |
now = time.strftime('%I:%M %p', time.localtime()) | |
print '%s) %d schools (%d new) and %d businesses (%d new) canceled as of now.' % ( | |
now, | |
len(closings['School']), | |
count['School'], | |
len(closings['Business']), | |
count['Business'] | |
) | |
print '=' * 78 | |
import time | |
while True: | |
add_closings() | |
time.sleep(120) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment