Skip to content

Commit

Permalink
Add zerocater support.
Browse files Browse the repository at this point in the history
  • Loading branch information
zbowling committed Sep 25, 2011
1 parent 1a15f92 commit 476b944
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
59 changes: 59 additions & 0 deletions campy/plugins/zerocater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import urllib2, re
from BeautifulSoup import BeautifulSoup
from BeautifulSoup import BeautifulStoneSoup

from plugins import CampyPlugin
from campy import settings
import sys, traceback

class ZeroCater(CampyPlugin):
def __init__(self, *args):
pass

def handle_message(self, campfire, room, message, speaker):
body = message['body']
if not body:
return

if not body.startswith(settings.CAMPFIRE_BOT_NAME):
return

m = re.match('%s: zc$' % settings.CAMPFIRE_BOT_NAME, body)
if m:
try:
#thar be horrible looking python below!
page = urllib2.urlopen(settings.ZERO_CATER_URL)
soup = BeautifulSoup(page)
paste_text = []
for week_box in soup.html.body.findAll("div",{"class":"week_box"}):
paste_text.append(str(week_box.findPrevious("h2").renderContents()))
meal_dates = week_box.findAll("div",{"class":"meal_date"})
for meal_date in meal_dates:
meal_date_text = meal_date.getText().decode("UTF-8")
meal_name = meal_date.findNext("div",{"class":"meal_name collapser-container"})
time_name_size = meal_name.find("div",{"class":"grid_6 alpha"}).find("span")
timetag = time_name_size.find("span",{"class":"collapser-controller"})
timetag_text = str(timetag.find("span",{"class":"collapser-state "}).findNextSibling(text = True)).decode("UTF-8")
food = timetag.findNextSibling(text = True)
food_text = str(BeautifulStoneSoup(food.string,convertEntities=BeautifulStoneSoup.HTML_ENTITIES)).decode("UTF-8")
restaurant = food.findNextSibling("a")
restaurant_text = str(BeautifulStoneSoup(restaurant.string,convertEntities=BeautifulStoneSoup.HTML_ENTITIES)).decode("UTF-8").strip()
people = restaurant.findNextSibling(text = True)
people_text = str(BeautifulStoneSoup(people.string,convertEntities=BeautifulStoneSoup.HTML_ENTITIES)).decode("UTF-8")
paste_text.append(u"\t %s @ %s - %s %s %s" % (meal_date_text.strip(), timetag_text.strip(), food_text.strip(), restaurant_text.strip(), people_text.strip()))
paste_text.append(u"\nMenu and voting: %s" % settings.ZERO_CATER_URL)
text = ('\n'.join(paste_text))
room.paste(text)
except Exception, e:
speak_text = u"%s: error parsing ZeroCater output." % speaker['user']['name']
room.speak(speak_text)
exc_type, exc_value, exc_traceback = sys.exc_info()
room.paste(repr(traceback.format_exception(exc_type, exc_value, exc_traceback)))



def send_help(self, campfire, room, message, speaker):
help_text = """%s: Here is your help for the ZeroCater plugin:
zc - display the zerocater menu
""" % speaker['user']['name']
room.paste(help_text)
2 changes: 2 additions & 0 deletions campy/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
LEAVE_ON_EXIT = False
RSS_REFRESH_TIME = 10 # seconds between rss feed refreshes

ZERO_CATER_URL = "http://www.zerocater.com/seatme" #default URL

try:
import simplejson as json
except ImportError:
Expand Down

0 comments on commit 476b944

Please sign in to comment.