Commit dc41fe96 authored by Landon DeCoito's avatar Landon DeCoito

Merge branch 'Landon' into 'master'

parscript is now a complete function, returning a list of dictionaries

See merge request apoulter/mason-today-web!14
parents 368dcfce af3aa97b
#print "and we begin"
#print "and we begin"
from bs4 import BeautifulSoup
from datetime import date, time
import requests
eventDict = {}
def what():
dictlist = []
DaysOfWeek = {
DaysOfWeek = {
"Sunday" : 0,
"Monday" : 1,
"Tuesday" : 2,
......@@ -14,48 +15,12 @@ DaysOfWeek = {
"Thursday" : 4,
"Friday" : 5,
"Saturday" : 6,
}
notProvide = "Not not provided"
counter = 0
class Event:
def __init__(self, entryTag): #where var entryTag is a specific event tag
self.__name = entryTag.find('title').string
self.__description = entryTag.find('content').string
self.__time = "timeplaceholder"
self.__date = "dateplaceholder"
self.__location = "locationplaceholder"
self.__timestart = "00"
self.__timestop = "00"
self.__day = "null day"
def __str__(self):
return self.__name + ": " + self.__description + "\n\n"
'''def setDescription(description):
self.__description = description
}
def setTime(time):
self.__time = time
notProvide = "Not not provided"
counter = 0
def setDate(date):
self.__date = date
def setLocation(location):
self.__location = location
def setTimehour(timehour):
self.__timehour = timehour
def setTimeminute(timemin):
self.__timemin = timemin
def setDay(day):
self.__day = day'''
def cleanup(str): #this function cleans up some of the useless html leftovers to characters we can actually use
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("–", "-")
......@@ -68,7 +33,7 @@ def cleanup(str): #this function cleans up some of the useless html leftovers to
str = str[0:len(str) - 1]
return str
class eventException: #this class is just an exception for our use
class eventException: #this class is just an exception for our use
def __init__(self,message):
self.__message = message
......@@ -77,16 +42,15 @@ class eventException: #this class is just an exception for our use
def __str__(self):
return self.__message
def convertTime(stri): #this function is used for splicing the event times.
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
#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
#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])
......@@ -111,23 +75,11 @@ def convertTime(stri): #this function is used for splicing the event times.
else:
raise eventException("This is weird and please don't happen")
#xmldoc = requests.get("http://25livepub.collegenet.com/calendars/events_all.xml") #grabs the xml from 25live
#xmldoc = open("events.xml", "r") #Opens a local document. events.xml is a shortened version of the larger events doc
#xmldoc = cleanup(xmldoc.text)
#print xmldoc
soup = BeautifulSoup(cleanup(requests.get("http://25livepub.collegenet.com/calendars/events_all.xml").text), "lxml") #creates soup of the xml
#print soup.prettify(), "\n\n"
#creates a list of all the entry tags from the xml
entries = soup.findAll('entry')
events = []
#indexs an entry in the list of entries
for entry in entries:
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
entries = soup.findAll('entry')
#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
entry_title = entry.title.text
......@@ -230,9 +182,11 @@ for entry in entries:
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]
......@@ -266,8 +220,7 @@ for entry in entries:
print "-----------------------------------------------------------------------------"
print time
'''print "-----------------------------------------------------------------------------"
print location
print day
print month
......@@ -277,27 +230,9 @@ for entry in entries:
print timestop
print description
print "----------------------------------------------------------------------------"
'''
dictlist.append({"title":entry_title, "dayofweek":day, "dayofmonth":monthday, "month":month, "year":year, "timestart":timestart, "timestop":timestop, "location":location, "description":description})
#this exception here stops the program on a specific case where the time is "12 - 11pm"
#if location == ['Robinson Hall B111 - Classroom (TDC)', ' ']:
# raise eventException("FOUND IT")
'''
possibilities:
1) split at -
2) check if 1 has nothing and 2 has am (this will be [-2:])
check if 1 has nothing and 2 has pm
check if 1 has am and 2 has pm
3) for each: check 1 is hour or hour:min ([] or [2] will be ':')
4) if hour, multiply by 60; if hour:min, multiply [0:1] or [0] by 60 and add [3:4] or [2:3].
'''
return dictlist
#everything in the house is fuzzy, stupid dogs were acting like pollinators, if that's how you even spell it
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