def get(key): """ Return a list of similar movies """ movie = MovieRepository.getbykey(key) #return jsonify({"test":movie.json}) users_who_rated = RateRepository.get_all_by_movie(key) keys_of_users_who_rated = [u.user_rating for u in users_who_rated] movies_were_rated = {} for user in users_who_rated: for m in RateRepository.get_all_by_user(user.user_rating): if m.movie_rated in movies_were_rated: movies_were_rated[m.movie_rated].append(user.user_rating) else: movies_were_rated[m.movie_rated] = [user.user_rating] sim = {} #return jsonify({movie:movie for movie in movies_were_rated}) for m in movies_were_rated.keys(): users = [ u for u in movies_were_rated[m] if u in keys_of_users_who_rated ] sim[m] = sum( (RateRepository.get(user, movie.key).rating - RateRepository.average_rating(movie.key)) * (RateRepository.get(user, m).rating - RateRepository.average_rating(m)) for user in users) / prod([ math.sqrt(0.00001 + (RateRepository.get(user, movie.key).rating - RateRepository.average_rating(movie.key))**2 + (RateRepository.get(user, m).rating - RateRepository.average_rating(m))**2) for user in users ]) print(sim[m]) movies_to_be_returned = [ MovieRepository.getbykey(m) for m in sim.keys() if sim[m] > .8 ] return jsonify( {movie.key: movie.json for movie in movies_to_be_returned})
def put(user_rating, movie_rated, rating): """ Update a rating based on the sent information """ repository = RateRepository() rate = repository.update(user_rating=user_rating, movie_rated=movie_rated, rating=rating) return jsonify({"rate": rate.json})
def get(user_rating): """ Return a list of all ratings done by the user """ rates = RateRepository.get_all_by_user(key=user_rating) return jsonify({rate.movie_rated: rate.json for rate in rates})
def post(user_rating, movie_rated, rating): """ Create a rating based on the sent information """ rate = RateRepository.create( user_rating=user_rating, movie_rated=movie_rated, rating=rating ) return jsonify({"rate": rate.json})
def get(user_rating, movie_rated): """ Return an rate key information based on his user """ rate = RateRepository.get(user_rating=user_rating, movie_rated=movie_rated) return jsonify({"rate": rate.json})
def put(): """ Load the full rating database """ rate = RateRepository.post_db() return jsonify({"success": "the database was loaded successfully"})
def get(movie_rated): """ Return a list of all ratings of the movie """ rates = RateRepository.get_all_by_movie(key=movie_rated) return jsonify({rate.user_rating: rate.json for rate in rates})
def get(key): """ Return all movies """ rating = RateRepository.average_rating(key=key) return jsonify({"average_rating": rating})