def rated_movie(username, skip): query = ''' MATCH(n: User {username: {username}})-[r: RATED]->(m: Movie) with count(m) as total return total''' count = graph.run(query, username=username) for x in count: resultscount = x['total'] query = '''MATCH (n:User{username:{username}})-[r:RATED]->(m:Movie) return m.title as title ,m.imdbId as imdbId ,m.imageUrl as imageUrl, r.stars as stars Skip {skip} Limit 102''' results = graph.run(query, username=username, skip=skip) return results, resultscount
def genres_list(genre_name, limit, skip): query = ''' MATCH (n:Movie)where n.genre={genre_name} with count(n) as total return total ''' count = graph.run(query, genre_name=genre_name) query = '''MATCH (m:Movie) where m.genre={genre_name} RETURN m.title as title ,m.imdbId as imdbId ,m.imageUrl as imageUrl Skip {skip} Limit {limit}''' results = graph.run(query, genre_name=genre_name, limit=limit, skip=skip) for x in count: resultscount = x['total'] return results, resultscount
def get_recent_posts(username): query = ''' MATCH (user:User)-[:PUBLISHED]->(post:Post)<-[:TAGGED]-(tag:Tag) WHERE user.username = {username} RETURN post, COLLECT(tag.name) AS tags ORDER BY post.timestamp DESC LIMIT 5 ''' return graph.run(query, username=username)
def my_books(username): query = ''' MATCH(n: User{username: {username}})-[: STATUS{status:'Read'}]->(book: Book)<-[: author]-(a:author) return book.title as title,book.year as year, book.isbn as isbn ,book.imageurl as imageurl ,a.name as author''' result1 = graph.run(query, username=username) return result1
def get_todays_recent_posts(): query = ''' MATCH (user:User)-[:PUBLISHED]->(post:Post)<-[:TAGGED]-(tag:Tag) WHERE post.date = {today} RETURN user.username AS username, post, COLLECT(tag.name) AS tags ORDER BY post.timestamp DESC LIMIT 5 ''' return graph.run(query, today=date())
def update_similarity(): query = '''MATCH (p1:User)-[x:RATED]->(m:Movie)<-[y:RATED]-(p2:User) WITH SUM(x.stars * y.stars) AS xyDotProduct, SQRT(REDUCE(xDot = 0.0, a IN COLLECT(x.stars) | xDot + a^2)) AS xLength, SQRT(REDUCE(yDot = 0.0, b IN COLLECT(y.stars) | yDot + b^2)) AS yLength, p1, p2 MERGE (p1)-[s:SIMILARITY]-(p2) SET s.similarity = xyDotProduct / (xLength * yLength) ''' results = graph.run(query)
def book_recommendations(username): query = ''' MATCH(n: User{username: {username}})-[: STATUS]->(: Book)<-[: author]-(a:author)-[: author]->(book:Book) where not (n: User{username: {username}})-[: STATUS]->(book: Book) return DISTINCT book.title as title,book.year as year, book.isbn as isbn ,book.imageurl as imageurl ,a.name as author ''' results = graph.run(query, username=username) return results
def get_commonality_of_user(username, other): # Find how many of the logged-in user's posts the other user # has liked and which tags they've both blogged about. query = ''' MATCH (they:User {username: {they} }) MATCH (you:User {username: {you} }) OPTIONAL MATCH (they)-[:PUBLISHED]->(:Post)<-[:TAGGED]-(tag:Tag), (you)-[:PUBLISHED]->(:Post)<-[:TAGGED]-(tag) RETURN SIZE((they)-[:LIKED]->(:Post)<-[:PUBLISHED]-(you)) AS likes, COLLECT(DISTINCT tag.name) AS tags ''' return graph.run(query, they=other, you=username).next
def similar_movies(username): query = ''' MATCH (b:User)-[r:RATED]->(m:Movie), (b)-[s:SIMILARITY]-(a:User {username:{username}}) WHERE NOT((a)-[:RATED]->(m)) WITH m, s.similarity AS similarity, r.stars AS stars ORDER BY m.title, similarity DESC WITH m.title AS movie,m.imageUrl AS imageUrl,m.description AS description ,COLLECT(stars)[0..3] AS ratings WITH movie,imageUrl,description, REDUCE(s = 0, i IN ratings | s + i)*1.0 / SIZE(ratings) AS reco ORDER BY reco DESC RETURN movie AS title, reco AS Recommendation ,imageUrl,description LIMIT 200 ''' results = graph.run(query, username=username) return results
def delete_post(username, post_id): print(post_id) print("Jadkjsh dkdh fkdhf gjdf gdfk ") query = ''' MATCH (user:User)-[:PUBLISHED]->(post:Post)<-[:TAGGED]-(tag:Tag) WHERE user.username ={username} AND post.id={post_id} Detach Delete post ''' return graph.run(query, username=username, post_id=post_id)
def get_similar_users(username): # Find three users who are most similar to the logged-in user # based on tags they've both blogged about. query = ''' MATCH (you:User)-[:PUBLISHED]->(:Post)<-[:TAGGED]-(tag:Tag), (they:User)-[:PUBLISHED]->(:Post)<-[:TAGGED]-(tag) WHERE you.username = {username} AND you <> they WITH they , COLLECT(DISTINCT tag.name) AS tags ORDER BY SIZE(tags) DESC LIMIT 3 RETURN they.username AS similar_user, tags ''' return graph.run(query, username=username)
def suggest_movies(username, gender, companion, emotions, criteria): query1 = query2 = "" if gender == 'female': query1, query2 = suggest_female_movies(companion, emotions) elif gender == 'male': query1, query2 = suggest_male_movies(companion, emotions) else: query1, query2 = suggest_general_movies(companion, emotions) if criteria == "both": results1 = graph.run(query1, username=username) results2 = graph.run(query2, username=username) set1 = set() set2 = set() for row in results1: set1.add(row) for row in results2: set2.add(row) movies = set1.intersection(set2) elif criteria == "emotion": movies = graph.run(query1, username=username) elif criteria == "companion": movies = graph.run(query2, username=username) else: results1 = graph.run(query1, username=username) results2 = graph.run(query2, username=username) set1 = set() set2 = set() for row in results1: set1.add(row) for row in results2: set2.add(row) movies = set1.union(set2) return movies
def listmovie(search): query = '''MATCH (author:author)-[:author]->(book:Book)<-[:Published]-(publisher:Publisher) where book.title STARTS WITH {search} return book.title as title , book.year as year, book.isbn as isbn ,book.imageurl as imageurl, publisher.name as publisher,author.name as author ''' results = graph.run(query, search=search) return results
def rate_movie(username, imdbId, rating): query = '''MATCH (n:User{username:{username}}),(m:Movie{imdbId:{imdbId}}) MERGE (n)-[r:RATED]->(m) set r.stars={rating} return r.stars as stars''' results = graph.run(query, username=username, imdbId=imdbId, rating=rating) return results
def random_movies(username): query = '''MATCH (m: Movie)where NOT (:User{username:{username}})-[:RATED]->(m:Movie)with m, rand() AS number return m.title as title , m.imdbId as imdbId ,m.imageUrl as imageUrl, m.description as description ,m.releaseDate as releaseDate ORDER BY number LIMIT 10 ''' results = graph.run(query, username=username) return results
def book_status(username, isbn, status): query = '''MATCH (n:User{username:{username}}),(b:Book{isbn:{isbn}}) MERGE (n)-[r:STATUS]->(b) set r.status={status} return r.status as status''' results = graph.run(query, username=username, isbn=isbn, status=status) return results
def get_movies(limit): query = '''MATCH (m:Movie)<-[:ACTS_IN]-(a:Person) RETURN m.title as movie,m.imdbId as imdbId , m.imageUrl as imageUrl , collect(a.name) as cast Limit {limit}''' results = graph.run(query, limit=limit) return results
from views import app from models import graph # graph.cypher.execute("CREATE CONSTRAINT ON (n:User) ASSERT n.username IS UNIQUE") # graph.cypher.execute("CREATE CONSTRAINT ON (n:Post) ASSERT n.id IS UNIQUE") # graph.cypher.execute("CREATE CONSTRAINT ON (n:Tag) ASSERT n.name IS UNIQUE") # TODO: What is the idea of the INDEX ON? graph.run("CREATE INDEX ON :Post(date)") users = ["pgenev", "kbori"] # TODO (1): Find a way to implement these constraints # TODO (1) answer: Not needed. Already implemented in the database. # TODO: Check how bcrypt/passlib works
from models import graph import json import requests import os url = os.environ.get('GRAPHENEDB_URL', 'http://localhost:7474') username = os.environ.get('neo4j') password = os.environ.get('deepak007') newgraph = Graph(url + '/db/data/', username=username, password=password) query = '''MATCH (m:Movie) where m.imdbRating='Rating Not Available' RETURN m.title as movie,m.imdbId as imdbId''' results = graph.run(query) count=0 URL={} for row in results: imdbId=(row['imdbId']).strip() if not (imdbId==''): try: