Commit 9109f6b9 authored by Landon DeCoito's avatar Landon DeCoito
Browse files

Added exceptions to handle bad event-making. Thanks, Phi Beta Sigma

parent 41bf8081
......@@ -3,6 +3,62 @@ from bs4 import BeautifulSoup
from datetime import date, time
import requests
def cleanup(str): #this function cleans up some of the useless html leftovers to characters we can actually use
str = str.replace("&", "&")
str = str.replace(" ", " ")
str = str.replace("–", "-")
str = str.replace("&lt;", "<")
str = str.replace("&gt;", ">")
str = str.replace("<br/>", "\n")
str = str.replace("Publish event on the Calendar?: TRUE \n" , "")
str = str.replace("Performing any medical procedures?: FALSE \n" , "")
str = str.replace("Parking Needed?: FALSE \n" , "")
str = str[0:len(str) - 1]
return str
class eventException: #this class is just an exception for our use
def __init__(self,message):
self.__message = message
#self.__exceptionlist = []
def __str__(self):
return self.__message
def convertTime(stri): #this function is used for splicing the event times.
if (stri[-2:] == "pm"): #checks to see if the time presented is pm
if not ((stri[0] == "1") and (stri[1] == "2")): #if the time is pm, then the 12:00 hour is noon and shouldn't get 12 added to it
try: #this try block works with the exception handler to add 12 to any pm times
stri = stri.replace(stri[0:2], str(int(stri[0:2]) + 12), 1)
#print "I did the first one " + stri
except:
stri = stri.replace(stri[0], str(int(stri[0]) + 12), 1)
#print "I did the NOT first one " + stri
if ":" in stri: #this if/else reliably converts the time to minutes. accepts either "hour:minute" or simply "hour"
try:
return ((int(stri[0:2])) * 60) + int(stri[3:5])
except:
return ((int(stri[0])) * 60) + int(stri[2:4])
else:
try:
return (int(stri[0:2])) * 60
except:
return (int(stri[0])) * 60
elif (stri[-2:] == "am"): #checks if the time presented is am, and executes identical code from the pm block, just without adding 12
if ":" in stri:
try:
return (int(stri[0:2]) * 60) + int(stri[3:5])
except:
return (int(stri[0]) * 60) + int(stri[2:4])
else:
try:
return int(stri[0:2]) * 60
except:
return int(stri[0]) * 60
else:
raise eventException("This is weird and please don't happen")
def load_data():
"""
Parses the XML from Mason and mines 2 BTC.
......@@ -22,68 +78,13 @@ def load_data():
notProvide = "Not Provided"
counter = 0
def cleanup(str): #this function cleans up some of the useless html leftovers to characters we can actually use
str = str.replace("&amp;", "&")
str = str.replace("&nbsp;", " ")
str = str.replace("&ndash;", "-")
str = str.replace("&lt;", "<")
str = str.replace("&gt;", ">")
str = str.replace("<br/>", "\n")
str = str.replace("Publish event on the Calendar?: TRUE \n" , "")
str = str.replace("Performing any medical procedures?: FALSE \n" , "")
str = str.replace("Parking Needed?: FALSE \n" , "")
str = str[0:len(str) - 1]
return str
class eventException: #this class is just an exception for our use
def __init__(self,message):
self.__message = message
#self.__exceptionlist = []
def __str__(self):
return self.__message
def convertTime(stri): #this function is used for splicing the event times.
if (stri[-2:] == "pm"): #checks to see if the time presented is pm
if not ((stri[0] == "1") and (stri[1] == "2")): #if the time is pm, then the 12:00 hour is noon and shouldn't get 12 added to it
try: #this try block works with the exception handler to add 12 to any pm times
stri = stri.replace(stri[0:2], str(int(stri[0:2]) + 12), 1)
#print "I did the first one " + stri
except:
stri = stri.replace(stri[0], str(int(stri[0]) + 12), 1)
#print "I did the NOT first one " + stri
if ":" in stri: #this if/else reliably converts the time to minutes. accepts either "hour:minute" or simply "hour"
try:
return ((int(stri[0:2])) * 60) + int(stri[3:5])
except:
return ((int(stri[0])) * 60) + int(stri[2:4])
else:
try:
return (int(stri[0:2])) * 60
except:
return (int(stri[0])) * 60
elif (stri[-2:] == "am"): #checks if the time presented is am, and executes identical code from the pm block, just without adding 12
if ":" in stri:
try:
return (int(stri[0:2]) * 60) + int(stri[3:5])
except:
return (int(stri[0]) * 60) + int(stri[2:4])
else:
try:
return int(stri[0:2]) * 60
except:
return int(stri[0]) * 60
else:
raise eventException("This is weird and please don't happen")
soup = BeautifulSoup(cleanup(requests.get("http://25livepub.collegenet.com/calendars/events_all.xml").text), "lxml") #creates soup of the xml
#creates a list of all the entry tags from the xml
#creates a list of all the entry tags from the xml
entries = soup.findAll('entry')
#indexs an entry in the list of entries
#indexs an entry in the list of entries
for entry in entries:
#pulls up an entry in the list of entries, finds the title tag and .text deletes all xml tags and returns just the text as a string
error = []
#pulls up an entries in the list of entries, finds the title tag and .text deletes all xml tags and returns just the text as a string
entry_title = entry.title.text
entry_content = entry.content.text
......@@ -131,97 +132,110 @@ def load_data():
#is always on either [0] or [1]
#see (A) above
if entry_detailes[0].split(",")[0] in DaysOfWeek:
#See (B)
if len(entry_detailes) == 1:
location = notProvide
date = entry_detailes[0]
description = notProvide
#see (C)
elif len(entry_detailes) == 2:
location = notProvide
date = entry_detailes[0]
description = entry_detailes[1]
#This extra case was made because one entry had the description split into two by a
#newline so it registered as two descriptions making the length = 3
elif len(entry_detailes) == 3:
location = notProvide
date = entry_detailes[0]
description = entry_detailes[1] + " " + entry_detailes[2]
#this will print if the code has failed to account for something in detailes, but it works as of December 26th 2017
else:
raise eventException("failed to account for detail in entry_detailes when date element is index 0 on entry_detailes list")
#see (D) above
elif entry_detailes[1].split(",")[0] in DaysOfWeek:
#See (E)
if len(entry_detailes) == 2:
location = entry_detailes[0]
date = entry_detailes[1]
description = notProvide
#See (F)
elif len(entry_detailes) == 3:
location = entry_detailes[0]
date = entry_detailes[1]
description = entry_detailes[2]
#This extra case was made because one entry had the description split into two by a
#newline so it registered as two descriptions making the length = 3
elif len(entry_detailes) == 4:
location = entry_detailes[0]
date = entry_detailes[1]
description = entry_detailes[2] + " " + entry_detailes[3]
#this will print if the code has failed to account for something in detailes
try:
if entry_detailes[0].split(",")[0] in DaysOfWeek:
#See (B)
if len(entry_detailes) == 1:
location = notProvide
date = entry_detailes[0]
description = notProvide
#see (C)
elif len(entry_detailes) == 2:
location = notProvide
date = entry_detailes[0]
description = entry_detailes[1]
#This extra case was made because one entry had the description split into two by a
#newline so it registered as two descriptions making the length = 3
elif len(entry_detailes) == 3:
location = notProvide
date = entry_detailes[0]
description = entry_detailes[1] + " " + entry_detailes[2]
#this will print if the code has failed to account for something in detailes, but it works as of December 26th 2017
else:
raise eventException("failed to account for detail in entry_detailes when date element is index 0 on entry_detailes list")
#see (D) above
elif entry_detailes[1].split(",")[0] in DaysOfWeek:
#See (E)
if len(entry_detailes) == 2:
location = entry_detailes[0]
date = entry_detailes[1]
description = notProvide
#See (F)
elif len(entry_detailes) == 3:
location = entry_detailes[0]
date = entry_detailes[1]
description = entry_detailes[2]
#This extra case was made because one entry had the description split into two by a
#newline so it registered as two descriptions making the length = 3
elif len(entry_detailes) == 4:
location = entry_detailes[0]
date = entry_detailes[1]
description = entry_detailes[2] + " " + entry_detailes[3]
#this will print if the code has failed to account for something in detailes
else:
raise eventException("failed to account for detail in entry_detailes when date element is index 1 on entry_detailes list")
#this will print if the above if statements failed to find the date block
else:
raise eventException("failed to account for detail in entry_detailes when date element is index 1 on entry_detailes list")
#this will print if the above if statements failed to find the date block
else:
raise eventException("failed to find and account for date element in entry_detailes list")
uniqueid = uniqueid[-9:]
if location != notProvide:
location = location[:-1]
location += ", "
if "Fairfax Campus" in location:
location = location.split(", Fairfax Campus, ")
campus = "Fairfax"
del location[-1]
elif "Arlington Campus" in location:
location = location.split(", Arlington Campus, ")
campus = "Arlington"
del location[-1]
else:
location = [location]
raise eventException("failed to find and account for date element in entry_detailes list")
except eventException as e:
error.append(e.toString())
except Exception:
error.append("Error intialising event")
try:
uniqueid = uniqueid[-9:]
except:
uniqueid = "Error with getting ID"
date = date.split(",")
day = date[0]
time = date[3][1:]
date = date[1][1:] + "," + date[2]
date = date.split(" ")
month = date[0]
monthday = date[1][:(len(date[1]) - 1)]
year = date[2]
try:
if location != notProvide:
location = location[:-1]
location += ", "
if "Fairfax Campus" in location:
location = location.split(", Fairfax Campus, ")
campus = "Fairfax"
del location[-1]
elif "Arlington Campus" in location:
location = location.split(", Arlington Campus, ")
campus = "Arlington"
del location[-1]
else:
location = [location]
except Exception:
error.append("Error with location")
try:
date = date.split(",")
day = date[0]
time = date[3][1:]
date = date[1][1:] + "," + date[2]
date = date.split(" ")
month = date[0]
monthday = date[1][:(len(date[1]) - 1)]
year = date[2]
except Exception:
error.append("Error with time/date splicing")
time = time.replace(" ", "")
time = time.split("-")
try:
timestop = convertTime(time[1])
except ValueError:
raise eventException(str(time))
if timestop == None:
raise eventException(str(time))
if not (time[0][-2:] == "am") and not (time[0][-2:] == "pm"):
if (time[1][-2:] == "am"):
timestart = convertTime(time[0] + "am")
time = time.replace(" ", "")
time = time.split("-")
try:
timestop = convertTime(time[1])
except ValueError:
raise eventException(str(time))
if timestop == None:
raise eventException(str(time))
if not (time[0][-2:] == "am") and not (time[0][-2:] == "pm"):
if (time[1][-2:] == "am"):
timestart = convertTime(time[0] + "am")
else:
timestart = convertTime(time[0] + "pm")
else:
timestart = convertTime(time[0] + "pm")
else:
timestart = convertTime(time[0])
timestart = convertTime(time[0])
except Exception:
error.append("Error with time reformatting")
......@@ -236,8 +250,10 @@ def load_data():
print description
print "----------------------------------------------------------------------------"
'''
dictlist.append({"id":uniqueid, "title":entry_title, "dayofweek":day, "dayofmonth":monthday, "month":month, "year":year, "timestart":timestart, "timestop":timestop, "location":location, "description":description})
if (error == []):
dictlist.append({"id":uniqueid, "title":entry_title, "dayofweek":day, "dayofmonth":monthday, "month":month, "year":year, "timestart":timestart, "timestop":timestop, "location":location, "description":description})
else:
dictlist.append({"id":uniqueid, "error":error})
return dictlist
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment