Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def special(incoming):
body = laundry.special
elif incoming.upper() == "WEATHER":
body = weather.special
elif incoming.upper() == "EVENTS":
body = events.eval(None)
elif incoming.upper() == "DEMO":
## welcome/instructions
body = 'Thanks for using Harvard Now!\n'
Expand All @@ -45,6 +47,7 @@ def special(incoming):
body += 'Sending part of a name gives all information associated with that name.\n'
body += 'For example sending Quad will give information about the shuttle stop Quad and the shuttle'
body += 'route Quad Yard Express and sending Quincy laundry will give all the laundry rooms in Quincy.\n'
body += 'For a list of events in the next 24 hours, send events\n\n'
return body

## main function
Expand Down
Empty file added services/events/__init__.py
Empty file.
53 changes: 53 additions & 0 deletions services/events/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import urllib2
import datetime
from bs4 import BeautifulSoup


#####################################
## Club Hub Events Function ##
#####################################

def getEvents():
events = []
url = 'https://h.clubhub.live/'

website = urllib2.urlopen(urllib2.Request(
url,
headers={'User-Agent': 'Mozilla/5.0'}
))
soup = BeautifulSoup(website.read(), 'html.parser')
event_divs = soup.select(".event")

for event_div in event_divs:
if not event_div:
continue
event = {
'name': event_div['data-name'],
'host': event_div['data-host'],
'starts': int(event_div['data-start'])
}

now = int(datetime.datetime.now().strftime("%s"))
if event['starts'] > now + 24 * 60 * 60:
continue

events.append(event)

return events


############################
## Top-Level ##
############################

def eval(cmd):
events = getEvents()
return "Events in the next 24 hours:\n" + \
"\n".join([
"{}: {} ({})".format(
datetime.datetime.fromtimestamp(event['starts']).strftime("%a %-I:%M%p"),
event['name'],
event['host']
)
for event in events]) + "\n" + \
"(Read more on Club Hub: https://h.clubhub.live)\n\n"