-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovies.py
81 lines (68 loc) · 2.45 KB
/
movies.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
#!/usr/bin/env python
from json import dumps
from flask import Flask, Response, request
from neo4jrestclient.client import GraphDatabase, Node
app = Flask(__name__, static_url_path='/static/')
gdb = GraphDatabase("http://localhost:7474",username="root", password="admin@123")
@app.route("/")
def get_index():
return app.send_static_file('index.html')
@app.route("/graph")
def get_graph():
query = ("MATCH (m:Movie)<-[:ACTED_IN]-(a:Person) "
"RETURN m.title as movie, collect(a.name) as cast "
"LIMIT {limit}")
results = gdb.query(query,
params={"limit": request.args.get("limit", 100)})
nodes = []
rels = []
i = 0
for movie, cast in results:
nodes.append({"title": movie, "label": "movie"})
target = i
i += 1
for name in cast:
actor = {"title": name, "label": "actor"}
try:
source = nodes.index(actor)
except ValueError:
nodes.append(actor)
source = i
i += 1
rels.append({"source": source, "target": target})
return Response(dumps({"nodes": nodes, "links": rels}),
mimetype="application/json")
@app.route("/search")
def get_search():
try:
q = request.args["q"]
except KeyError:
return []
else:
query = ("MATCH (movie:Movie) "
"WHERE movie.title =~ {title} "
"RETURN movie")
results = gdb.query(
query,
returns=Node,
params={"title": "(?i).*" + q + ".*"}
)
return Response(dumps([{"movie": row.properties}
for [row] in results]),
mimetype="application/json")
@app.route("/movie/<title>")
def get_movie(title):
query = ("MATCH (movie:Movie {title:{title}}) "
"OPTIONAL MATCH (movie)<-[r]-(person:Person) "
"RETURN movie.title as title,"
"collect([person.name, "
" head(split(lower(type(r)), '_')), r.roles]) as cast "
"LIMIT 1")
results = gdb.query(query, params={"title": title})
title, cast = results[0]
return Response(dumps({"title": title,
"cast": [dict(zip(("name", "job", "role"), member))
for member in cast]}),
mimetype="application/json")
if __name__ == '__main__':
app.run(port=8080)