Last active
October 30, 2016 18:59
-
-
Save mrecachinas/96055a35b153d4ba965a to your computer and use it in GitHub Desktop.
Useful script for when it snows in the DC metro area
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
#!/usr/bin/env python | |
# vim:sw=4:sts=4:et | |
import bs4 | |
import urllib2 | |
import httplib | |
import datetime | |
def parse_html(html_str, now): | |
"""Parses HTML from OPM's current status website to return open/close | |
:param html_str: HTML response of OPM's website | |
:type html_str: str | |
:param now: the current datetime | |
:type now: datetime.datetime | |
:return: whether or not there is any news regarding tomorrow's status | |
:rtype: str | |
""" | |
html = bs4.BeautifulSoup(html_str, 'html.parser') | |
status = html.find('div', {'class': 'Status'}) | |
tomorrow = now.day + 1 | |
if str(tomorrow) in status.find('strong').get_text(): | |
return status | |
else: | |
return "No news for {tomorrow}".format(tomorrow=tomorrow) | |
def get_status(): | |
"""Fetches the status from OPM's current status website | |
:return: OPM's status for tomorrow | |
:rtype: str | |
""" | |
OPM_URL = 'https://www.opm.gov/policy-data-oversight/snow-dismissal-procedures/current-status/' | |
try: | |
res = urllib2.urlopen(OPM_URL) | |
today = datetime.datetime.now() | |
return parse_html(res.read(), today) | |
except urllib2.HTTPError, e: | |
return "HTTPError:", e | |
except urllib2.URLError, e: | |
return "URLError:", e | |
except httplib.HTTPException, e: | |
return "HTTPException:", e | |
if __name__ == '__main__': | |
print get_status() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment