-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
56 lines (35 loc) · 1.25 KB
/
app.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
import ast
import os
import psycopg2
from flask import Flask, jsonify, request, make_response
from flask_cors import CORS
from utils.helpers import connect_db, find_movies_recommend
app = Flask(__name__)
# CORS implemented so that we don't get errors when trying to access the server from a different server location
CORS(app)
# Connect DB
cur = connect_db()
# get movies recommend for user with username
@app.route("/")
def home():
return "Server RS"
# get movies recommend for user with username
@app.route("/api/recommend/<username>", methods=["GET"])
def get_movies_recommend(username):
cur.execute(
"select id from public.user where username = '{}';".format(username)
)
user_id = cur.fetchone()
movies_recommend = find_movies_recommend(user_id["id"])
if not movies_recommend:
message = jsonify(message="User not found.")
return make_response(message, 400)
movie_ids = movies_recommend.replace("[", "").replace("]", "").split()
# movie_ids = ast.literal_eval(movie_ids)
cur.execute(
"select * from public.movie where id in {}".format(tuple(movie_ids))
)
movies = cur.fetchall()
return jsonify(movies=movies)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)