-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·56 lines (46 loc) · 2.19 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util, template
from google.appengine.api import urlfetch, memcache
import base64, time, urllib, logging
# Make sure you make a keys.py file with these
from keys import CREDS
REALM = 'twiliort.com'
TOKEN_TTL = 86400 # 24 hours
def baseN(num,b=36,numerals="0123456789abcdefghijklmnopqrstuvwxyz"):
return ((num == 0) and "0" ) or (baseN(num // b, b).lstrip("0") + numerals[num % b])
class MainHandler(webapp.RequestHandler):
def get(self):
self.response.out.write(template.render('main.html', locals()))
def post(self):
battle = baseN(abs(hash(time.time())))
resp = urlfetch.fetch('https://%s.%s/-/experimental/accesstoken' % (CREDS[REALM][0], REALM), method='POST',
headers={"Authorization": "Basic "+base64.b64encode('%s:' % CREDS[REALM][1])},
payload=urllib.urlencode({
'expires': int(time.time())+TOKEN_TTL,
'path': "/%s" % battle,
'listen': 'true',
'publish': 'true'}))
access_token = resp.content
memcache.set(battle, access_token)
self.redirect('/%s' % battle)
class BattleHandler(webapp.RequestHandler):
def get(self, battle):
self.response.out.write(template.render('battle.html', {
'account_sid': CREDS[REALM][0],
'battle': battle,
'access_token': memcache.get(battle),
'players': self.request.get('players', 2)}))
def post(self, battle):
if self.request.query_string == 'send':
logging.info(self.request.headers['content-type'])
resp = urlfetch.fetch('https://%s.%s/%s' % (CREDS[REALM][0], REALM, battle), method='POST',
headers={
"Authorization": "Basic "+base64.b64encode('%s:' % memcache.get(battle)),
"Content-Type": self.request.headers['content-type']},
payload=self.request.body)
logging.info(resp.content)
def main():
application = webapp.WSGIApplication([('/', MainHandler), ('/(.*)', BattleHandler)], debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()