forked from pombredanne/meetup-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meetup.py
108 lines (92 loc) · 3.24 KB
/
meetup.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
# encoding: utf-8
"""
meetup.py
Created by mark henderson on 2013-02-19.
Copyright (c) 2013 __MyCompanyName__. All rights reserved.
"""
import os
import re
from tornado import httpserver, ioloop, web
from neo4jrestclient.client import Relationship
import util
class Application(web.Application):
def __init__(self):
settings = {
'debug': True,
'autoescape': None,
'static_path': os.path.join(os.path.dirname(__file__), 'static'),
}
routes = [
(r'/', PageHandler),
(r'/search', SearchHandler),
(r'/node/(\d+)', NodeHandler),
]
web.Application.__init__(self, routes, **settings)
class PageHandler(web.RequestHandler):
def get(self):
self.render('template/base.html')
class NodeHandler(web.RequestHandler):
def get(self, node_id):
node = util.get(node_id)
others = []
likes = []
dislikes = []
if node:
for rel in node.relationships.all():
if rel.end != node:
if rel.type == 'likes':
likes.append(rel.end)
elif rel.type == 'dislikes':
dislikes.append(rel.end)
else:
others.append(rel.end)
print likes, others
self.write({
'results': self.render_string('template/detail.html', node=node, others=others, likes=likes, dislikes=dislikes)
})
class SearchHandler(web.RequestHandler):
def get(self):
"""figure out the type of search that was performed
if it has the words like or dislike, capture the word after
and do a search
if it contains <--> capture the words before and after and do
a degree search
"""
nodes = []
results = ''
search = str(self.get_argument('search', ''))
bacon = False
template = 'template/node.html'
if '<-->' in search:
r = re.search('(.*?)<\-\->(.*)\W*', search)
bacon = True
if r:
left = r.group(1)
right = r.group(2)
nodes = util.degree(left, right, 'likes')
elif 'likes' in search:
r = re.search('(?:dis)?likes\s(\w+)', search)
feels = 'likes'
if 'dislikes' in search:
feels = 'dislikes'
if r:
nodes = util.search(r.group(1), feels)
for n in nodes:
if bacon:
if isinstance(n, Relationship):
template = 'template/relationship.html'
print '[[[[]', n.type
else:
template = 'template/node.html'
results += self.render_string(template, node=n, klass='relationship_node')
else:
results += self.render_string(template, node=n, klass="")
self.write({
'results': results
})
if __name__ == '__main__':
"""Start the application"""
http_server = httpserver.HTTPServer(Application())
http_server.listen(9003)
ioloop.IOLoop.instance().start()