2013-03-21 00:32:03 -04:00
|
|
|
#!/usr/bin/python3
|
|
|
|
"""
|
|
|
|
foodforus.py - foodforus module
|
|
|
|
author: mutantmonkey <mutantmonkey@mutantmonkey.in>
|
|
|
|
"""
|
|
|
|
|
|
|
|
from tools import GrumbleError
|
|
|
|
import hashlib
|
|
|
|
import json
|
|
|
|
import web
|
|
|
|
|
2014-12-28 20:15:52 -05:00
|
|
|
API_URL = 'https://foodfor.vtluug.org'
|
2013-03-21 00:32:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _sign_vote(api_key, args):
|
2013-05-22 10:30:42 -04:00
|
|
|
data = "ffu1"
|
2013-03-21 00:32:03 -04:00
|
|
|
for k, v in sorted(args.items()):
|
|
|
|
if k == 'sig':
|
|
|
|
continue
|
|
|
|
data += '{0}{1}'.format(k, v)
|
2013-05-22 10:30:42 -04:00
|
|
|
data += api_key
|
2013-03-21 00:32:03 -04:00
|
|
|
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)
|
2013-06-09 01:27:24 -04:00
|
|
|
except:
|
2013-03-21 00:32:03 -04:00
|
|
|
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)
|
2014-01-19 19:30:42 -05:00
|
|
|
data = json.loads(req)
|
2013-06-09 01:27:24 -04:00
|
|
|
except:
|
2013-03-21 00:32:03 -04:00
|
|
|
raise GrumbleError("Uh oh, I couldn't contact foodforus. HOW WILL WE "\
|
|
|
|
"EAT NOW‽")
|
|
|
|
|
2013-04-11 18:03:41 -04:00
|
|
|
if 'error' in data:
|
|
|
|
phenny.reply(data['error'])
|
|
|
|
else:
|
|
|
|
phenny.reply("Your vote has been recorded.")
|
2013-03-21 00:32:03 -04:00
|
|
|
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)
|
2013-06-09 01:27:24 -04:00
|
|
|
except:
|
2013-03-21 00:32:03 -04:00
|
|
|
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'(.*)')
|