From 2d337c1e053d1f0270cc282a90f1fd0c7a8e392f Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Thu, 21 Mar 2013 00:32:03 -0400 Subject: [PATCH] add foodforus module --- modules/foodforus.py | 95 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 modules/foodforus.py diff --git a/modules/foodforus.py b/modules/foodforus.py new file mode 100644 index 0000000..2cfbb4b --- /dev/null +++ b/modules/foodforus.py @@ -0,0 +1,95 @@ +#!/usr/bin/python3 +""" +foodforus.py - foodforus module +author: mutantmonkey +""" + +from urllib.error import HTTPError +from tools import GrumbleError +import hashlib +import json +import web + +API_URL = 'http://foodfor.vtluug.org' + + +def _sign_vote(api_key, args): + data = "ffu0" + api_key + for k, v in sorted(args.items()): + if k == 'sig': + continue + data += '{0}{1}'.format(k, v) + h = hashlib.sha256() + h.update(data.encode('utf-8')) + return h.hexdigest() + + +def food(phenny, input): + """.food""" + key = input.group(2) or input.sender + try: + req = web.get(API_URL + '/food/' + web.quote(key.strip())) + data = json.loads(req) + except (HTTPError, IOError): + raise GrumbleError("Uh oh, I couldn't contact foodforus. HOW WILL WE "\ + "EAT NOW‽") + + restaurants = data['restaurants'][:4] + times = data['times'][:4] + + restr = ", ".join(["{0} ({1})".format(r[0], r[1]) for r in + restaurants]) + tistr = ", ".join(["{0} ({1})".format(t[0], t[1]) for t in times]) + + if len(restr) > 0 and len(tistr) > 0: + return phenny.say("{0} at {1}".format(restr, tistr)) + else: + return phenny.say("Sorry, people need to vote before we can food!") +food.rule = (['food'], r'(.*)') + + +def foodvote(phenny, input): + """.foodvote""" + if not input.group(2) or not input.group(3): + return phenny.reply("You need to specify a place and time, as in "\ + ".foodvote hokie haus 18:45") + + key = input.group(4) or input.sender + postdata = { + 'user': input.nick, + 'restaurant': input.group(2), + 'start': input.group(3), + 'key': key.strip(), + } + postdata['sig'] = _sign_vote(phenny.config.foodforus_api_key, postdata) + + try: + req = web.post(API_URL + '/vote', postdata) + data = json.loads(req) + except (HTTPError, IOError): + raise GrumbleError("Uh oh, I couldn't contact foodforus. HOW WILL WE "\ + "EAT NOW‽") + + phenny.reply("Your vote has been recorded.") +foodvote.rule = (['foodvote'], r'(.*) (\d{2}:\d{2})( .*)?') + + +def pickfood(phenny, input): + key = input.group(2) or input.sender + try: + req = web.get(API_URL + '/food/' + web.quote(key.strip())) + data = json.loads(req) + except (HTTPError, IOError): + raise GrumbleError("Uh oh, I couldn't contact foodforus. HOW WILL WE "\ + "EAT NOW‽") + + if len(data['restaurants']) > 0 and len(data['times']) > 0: + restaurant = data['restaurants'][0] + time = data['times'][0] + + phenny.say("Food is {place} ({place_votes} votes) at {time} "\ + "({time_votes} votes). Happy fooding!".format(place=restaurant[0], + place_votes=restaurant[1], time=time[0], time_votes=time[1])) + else: + phenny.say("Sorry, people need to vote before we can food!") +pickfood.rule = (['pickfood'], r'(.*)')