-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsite.py
79 lines (67 loc) · 2.52 KB
/
site.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import tornado.ioloop
import tornado.web
import json
import entities, parliament
import urllib2
import memcache
mc = memcache.Client(['127.0.0.1:11211'], debug=1)
class MainHandler(tornado.web.RequestHandler):
def get(self):
indextxt = open('./site/index.html').read()
self.write(indextxt)
class DemoHandler(tornado.web.RequestHandler):
def mp_places(self, mpid):
mckey = str('places_%s' % mpid)
print('key is %s' % mckey)
places = mc.get(mckey)
if places is None:
print('not in cache')
text = parliament.mptext(mpid)
text = ''.join(x for x in text if ord(x)<127)
places = entities.places_from_text(text)
mc.set(mckey, places)
return places
def get_locations(self, mpid):
places = self.mp_places(mpid)
locations = entities.placelocations(places)
return json.dumps(locations)
def get(self):
#import pdb; pdb.set_trace()
mpid = self.get_argument('mp', '10251')
print('mpid is %s' % mpid)
self.content_type = 'application/json'
self.set_header("Content-Type", "application/json")
self.write(self.get_locations(mpid))
class NewDemoHandler(tornado.web.RequestHandler):
def get(self):
mpid = self.get_argument('mp', '10251')
mckey = str('places_%s' % mpid)
places = mc.get(mckey)
if places is None:
text = parliament.mptext()
places = entities.places_from_text(text)
mc.set(mckey, places)
locations = entities.placelocations(places)
output = json.dumps(locations)
self.content_type = 'application/json'
self.set_header("Content-Type", "application/json")
self.write(output)
class MPListHandler(tornado.web.RequestHandler):
def get(self):
cached = mc.get('mplist')
if cached is None:
mplist = urllib2.urlopen('http://www.theyworkforyou.com/api/getMPs?key=C96JqqCACkSJGaXNMyGxyLei&output=js').read()
mc.set('mplist', mplist)
self.set_header("Content-Type", "application/json")
self.write(cached or mplist)
static_path = os.path.join(os.path.dirname(__file__), "static")
application = tornado.web.Application([
(r"/", MainHandler),
(r"/code/demo", DemoHandler),
(r"/entities.json", DemoHandler),
(r"/code/mplist", MPListHandler),
])
if __name__ == "__main__":
application.listen(9000)
tornado.ioloop.IOLoop.instance().start()