diff --git a/app.py b/app.py index 78c7b8f..25633a0 100644 --- a/app.py +++ b/app.py @@ -18,6 +18,8 @@ def eval(cmd, input=None): return shuttle.eval(cmd['args']) elif cmd['service'] == 'W': ## Weather return weather.eval(input) + elif cmd['courses'] == 'M': ## Courses + return courses.eval(input) else: return "ERROR 42: service not recognized" @@ -33,6 +35,8 @@ def special(incoming): body = laundry.special elif incoming.upper() == "WEATHER": body = weather.special + elif incoming.upper() == "COURSES": + body = courses.special elif incoming.upper() == "DEMO": ## welcome/instructions body = 'Thanks for using Harvard Now!\n' diff --git a/data.py b/data.py index 2fdf85b..6d7e5e3 100755 --- a/data.py +++ b/data.py @@ -141,5 +141,6 @@ {'service': 'S', 'args':{'endpoint': 'route', 'routeid': '4007272' , 'label': 'Barry\'s Corner Shuttle Route'}, 'tags':["BARRY'S",'BARRY', 'CORNER', 'SHUTTLE', 'ROUTE']}, {'service': 'S', 'args':{'endpoint': 'route', 'routeid': '4007610' , 'label': 'Quad Stadium Express Shuttle Route'}, 'tags':['QUAD', 'STADIUM', 'EXPRESS', 'SHUTTLE', 'ROUTE']}, {'service': 'S', 'args':{'endpoint': 'route', 'routeid': '4007650' , 'label': 'Allston Campus Express Shuttle Route'}, 'tags':['ALLSTON', 'CAMPUS', 'EXPRESS', 'SHUTTLE', 'ROUTE']}, - {'service': 'W', 'args':{}, 'tags':['WEATHER']} + {'service': 'W', 'args':{}, 'tags':['WEATHER']}, + {'service': 'M', 'args':{}, 'tags':['COURSES']} ] diff --git a/services/__init__.py b/services/__init__.py index f598ef5..20b0400 100755 --- a/services/__init__.py +++ b/services/__init__.py @@ -1,3 +1,4 @@ from laundry import laundry from shuttle import shuttle from weather import weather +from courses import courses \ No newline at end of file diff --git a/services/courses/__init__.py b/services/courses/__init__.py new file mode 100755 index 0000000..e69de29 diff --git a/services/courses/courses.py b/services/courses/courses.py new file mode 100755 index 0000000..4942586 --- /dev/null +++ b/services/courses/courses.py @@ -0,0 +1,45 @@ +import urllib2 +from bs4 import BeautifulSoup + +############################# +## Course Function ## +############################# + +def getCourseInfo(input): + url = 'https://courses.harvard.edu/search?' + url += 'sort=score%20desc%2Ccourse_title%20asc&start=0&rows=25' + url += '&q=%s' % input + website = urllib2.urlopen(url) + soup = BeautifulSoup(website.read(), 'html.parser') + + try: + instructors = soup.find(id='srl_instructor').text.encode('unicode-escape') + desc = soup.find(id='srl_description').text.encode('unicode-escape') + credits = soup.find_all('p')[2].text.encode('unicode-escape') + location = soup.find_all('p')[3].text.encode('unicode-escape') + + body = 'Instructors: ' + instructors + '\n' + body += desc + '\n' + body += credits + '\n' + body += location + '\n' + + except Exception, e: + print str(e) + return "Could not find course data. Are you sure you gave a proper course name?" + + return body + + +def makeSpecial(): + s = 'To get the info for a particular course, use the format \'courses course\'.' + return s + +############################ +## Top-Level ## +############################ + +## return list of valid laundry rooms +special = makeSpecial() + +def eval(input): + return getCourseInfo(input)