-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
131 lines (101 loc) · 3.89 KB
/
api.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from random import randint
from flask import Blueprint
from helper import *
api = Blueprint('api', __name__, template_folder='templates')
def error():
response = jsonify({'message': "THIS SI ERROR"})
response.headers.add('Access-Control-Allow-Origin', '*')
response.status_code = 400
return response
@api.route("/search/person/")
@api.route("/search/person/<pattern>")
@crossdomain(origin='*')
def search_person(pattern=""):
regex = '(?i).*{}.*'.format(pattern)
g = graph.cypher.execute("MATCH (a) WHERE a.name =~ '{}' AND (a:Actor OR a:Director) "
"RETURN a.name LIMIT {}".format(regex, 10))
actors = [x[0] for x in g]
return jsonify({"results": actors})
@api.route("/search/movie/")
@api.route("/search/movie/<pattern>")
@crossdomain(origin='*')
def search_movie(pattern=""):
regex = '(?i).*{}.*'.format(pattern)
g = graph.cypher.execute("MATCH (m:Movie) WHERE m.title =~ {title}"
" RETURN m.title LIMIT {limit}", title=regex, limit=10)
movies = [x[0] for x in g]
return jsonify({"results": movies})
@api.route("/around-node/<node_id>/<group>")
@crossdomain(origin='*')
def get_nodes_around(node_id, group):
if group not in ['Movie', 'Actor', 'Director']:
return "BAD"
if group == 'Movie':
g = graph.cypher.execute("MATCH p=(:Movie{title:{id}})-[]-(n) WHERE n:Actor OR n:Director "
"RETURN p", id=node_id)
elif group == 'Actor':
g = graph.cypher.execute("MATCH p=(:Actor{name:{id}})-[r]-(:Movie) RETURN p", id=node_id)
else:
g = graph.cypher.execute("MATCH p=(:Director{name:{id}})-[r]-(:Movie) RETURN p", id=node_id)
return jsonify(graph_to_nodes_edges(g))
@api.route('/random-graph')
@crossdomain(origin='*')
def random_graph():
skip = randint(0, 100)
g = graph.cypher.execute("MATCH p = ()-[]-()-[]-() return p skip {skip} limit 5", skip=skip)
return jsonify(graph_to_nodes_edges(g))
@api.route("/stats")
@crossdomain(origin='*')
def main_page_stats():
movie_count = get_movie_count()
actor_count = get_actor_count()
role_count = get_role_count()
director_count = get_director_count()
stats = {
"movie_count": movie_count,
"actor_count": actor_count,
"role_count": role_count,
"director_count": director_count
}
return jsonify(stats)
@api.route('/around-movie/')
@api.route('/around-movie/<title>')
@crossdomain(origin='*')
def around_movie(title=""):
node = found_movie(title)
if node is None:
return error()
g = graph.cypher.execute("MATCH p=(m)<-[]-(a) WHERE m.title = {title} AND (a:Actor OR a:Director)"
" return p", title=title)
data = graph_to_nodes_edges(g)
data['movie'] = node
return jsonify(data)
@api.route('/around-person/')
@api.route('/around-person/<name>')
@crossdomain(origin='*')
def around_person(name=""):
node = found_person(name)
if node is None:
return error()
g = graph.cypher.execute("MATCH p=(a)-[r]-(:Movie) WHERE a.name = {name} AND (a:Actor OR a:Director)"
" RETURN p", name=node['name'])
data = graph_to_nodes_edges(g)
data['person'] = node
return jsonify(data)
@api.route('/collaboratiosn/<first>/')
@api.route('/collaborations/<first>/<second>')
@crossdomain(origin='*')
def collaborations(first="", second=""):
person1 = found_person(first)
if person1 is None:
return error()
person2 = found_person(second)
if person2 is None:
return error()
g = graph.cypher.execute("MATCH p=(a:Person)-[]->(:Movie)<-[]-(b:Person)"
" WHERE a.name = {p1} AND b.name = {p2} "
" RETURN p", p1=person1['name'], p2=person2['name'])
data = graph_to_nodes_edges(g)
data['person1'] = person1
data['person2'] = person2
return jsonify(data)