From bd98f48833630799a0fa03c23b8a88d7d4d14a26 Mon Sep 17 00:00:00 2001 From: Hari Kothapalli Date: Fri, 10 Nov 2017 19:58:51 -0500 Subject: [PATCH 1/2] Add boilerplate --- app.py | 4 +++ data.py | 3 +- services/__init__.py | 1 + services/courses/__init__.py | 0 services/courses/courses.py | 61 ++++++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 1 deletion(-) create mode 100755 services/courses/__init__.py create mode 100755 services/courses/courses.py 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..47c5207 --- /dev/null +++ b/services/courses/courses.py @@ -0,0 +1,61 @@ +import urllib2, urllib +from bs4 import BeautifulSoup +import data + +############################# +## Laundry Function ## +############################# + +def getMachines(roomid, machinetype): + machines = [] + url = 'http://m.laundryview.com/submitFunctions.php?' + url += 'cell=null&lr=%s&monitor=true' % roomid + website = urllib2.urlopen(url) + soup = BeautifulSoup(website.read(), 'html.parser') + washer_div = soup.find(id=machinetype) + machine = washer_div.next_sibling + if machinetype == 'washer': + while 'id' not in machine.attrs or machine['id'] != 'dryer': + machines.append({'lr': roomid, + 'id': machine.a['id'], + 'name': `(machine.a.text)`.split('\\xa0')[0][2:], + 'time': machine.a.p.text}) + machine = machine.next_sibling + else: + while machine and machine.name == 'li': + machines.append({'lr': roomid, + 'id': machine.a['id'], + 'name': `(machine.a.text)`.split('\\xa0')[0][2:], + 'time': machine.a.p.text}) + machine = machine.next_sibling + return machines + +def machines_to_string(machines): + s = '' + for machine in machines: + s += machine['name'] + ': ' + machine['time'] + '\n' + return s + +def room_names(): + s = 'Here are the laundry rooms that we have data for: \n' + used = [] + for room in data.rooms: + if data.rooms[room] not in used: + s += room + '\n' + used.append(data.rooms[room]) + return s + +def makeSpecial(): + s = "Laundry Rooms: \n" + s += '\n'.join([room for room in data.rooms]) + return s + +############################ +## Top-Level ## +############################ + +## return list of valid laundry rooms +special = makeSpecial() + +def eval(cmd): + return cmd['label']+'\n'+machines_to_string(getMachines(cmd['roomid'],cmd['machinetype'])) From 4d72bbfe5a893c82d01dfecabee2cf096b34b696 Mon Sep 17 00:00:00 2001 From: Hari Kothapalli Date: Fri, 10 Nov 2017 21:17:32 -0500 Subject: [PATCH 2/2] Implement course scraper --- services/courses/courses.py | 68 ++++++++++++++----------------------- 1 file changed, 26 insertions(+), 42 deletions(-) diff --git a/services/courses/courses.py b/services/courses/courses.py index 47c5207..4942586 100755 --- a/services/courses/courses.py +++ b/services/courses/courses.py @@ -1,53 +1,37 @@ -import urllib2, urllib +import urllib2 from bs4 import BeautifulSoup -import data ############################# -## Laundry Function ## +## Course Function ## ############################# -def getMachines(roomid, machinetype): - machines = [] - url = 'http://m.laundryview.com/submitFunctions.php?' - url += 'cell=null&lr=%s&monitor=true' % roomid +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') - washer_div = soup.find(id=machinetype) - machine = washer_div.next_sibling - if machinetype == 'washer': - while 'id' not in machine.attrs or machine['id'] != 'dryer': - machines.append({'lr': roomid, - 'id': machine.a['id'], - 'name': `(machine.a.text)`.split('\\xa0')[0][2:], - 'time': machine.a.p.text}) - machine = machine.next_sibling - else: - while machine and machine.name == 'li': - machines.append({'lr': roomid, - 'id': machine.a['id'], - 'name': `(machine.a.text)`.split('\\xa0')[0][2:], - 'time': machine.a.p.text}) - machine = machine.next_sibling - return machines - -def machines_to_string(machines): - s = '' - for machine in machines: - s += machine['name'] + ': ' + machine['time'] + '\n' - return s -def room_names(): - s = 'Here are the laundry rooms that we have data for: \n' - used = [] - for room in data.rooms: - if data.rooms[room] not in used: - s += room + '\n' - used.append(data.rooms[room]) - return s + 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 = "Laundry Rooms: \n" - s += '\n'.join([room for room in data.rooms]) + s = 'To get the info for a particular course, use the format \'courses course\'.' return s ############################ @@ -57,5 +41,5 @@ def makeSpecial(): ## return list of valid laundry rooms special = makeSpecial() -def eval(cmd): - return cmd['label']+'\n'+machines_to_string(getMachines(cmd['roomid'],cmd['machinetype'])) +def eval(input): + return getCourseInfo(input)