mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-06 19:23:07 +02:00
Merge branch 'refs/heads/dev_Alkaratus' into get_movie_recommendations
# Conflicts: # connector/Include/frontend_AI_connector.py # movie_recommendations/Test.http # movie_recommendations/movie_recommender.py
This commit is contained in:
commit
f20e6e483a
116
connector/Include/analytics.py
Normal file
116
connector/Include/analytics.py
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
from flask import Flask, request, jsonify
|
||||||
|
from flask_caching import Cache
|
||||||
|
import psycopg2
|
||||||
|
import pandas
|
||||||
|
import json
|
||||||
|
from configparser import ConfigParser
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
|
||||||
|
db_connector = None
|
||||||
|
conn = None
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/get_number_of_ratings", methods=["GET"])
|
||||||
|
@cache.cached(timeout=500)
|
||||||
|
def get_number_of_ratings():
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("select count(*) as num_of_ratings from ratings")
|
||||||
|
res = cursor.fetchall()
|
||||||
|
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
|
return jsonify(res[0]), 200
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/get_movie_ratings/<string:movie_id>", methods=["GET"])
|
||||||
|
@cache.cached(timeout=50)
|
||||||
|
def get_movie_ratings(movie_id):
|
||||||
|
cursor = conn.cursor()
|
||||||
|
ratings = {}
|
||||||
|
rating_values = [5, 4, 3, 2, 1]
|
||||||
|
|
||||||
|
for rating in rating_values:
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT COUNT(*) as count
|
||||||
|
FROM ratings
|
||||||
|
WHERE rating = %s AND movie_ID = %s;
|
||||||
|
""", (rating, movie_id))
|
||||||
|
result = cursor.fetchone()
|
||||||
|
ratings[f'{rating}_star'] = result[0]
|
||||||
|
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
|
return jsonify(ratings), 200
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/get_users_number", methods=["GET"])
|
||||||
|
@cache.cached(timeout=50)
|
||||||
|
def get_number_of_users():
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("select count(*) as num_of_users from users")
|
||||||
|
res = cursor.fetchall()
|
||||||
|
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
|
return jsonify(res[0]), 200
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/get_movie_rating_avg/<string:movie_id>", methods=["GET"])
|
||||||
|
@cache.cached(timeout=50)
|
||||||
|
def get_movie_rating_avg(movie_id):
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT AVG(rating) as avg_rating
|
||||||
|
FROM ratings
|
||||||
|
WHERE movie_ID = %s;
|
||||||
|
""", (movie_id,))
|
||||||
|
res = cursor.fetchall()
|
||||||
|
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
|
return jsonify(res[0]), 200
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/get_user_ratings/<string:user_id>", methods=["GET"])
|
||||||
|
@cache.cached(timeout=50)
|
||||||
|
def get_user_ratings(user_id):
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT *
|
||||||
|
FROM ratings
|
||||||
|
WHERE oauth_ID = %s;
|
||||||
|
""", (user_id,))
|
||||||
|
res = cursor.fetchall()
|
||||||
|
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
|
return jsonify(res), 200
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
config = ConfigParser()
|
||||||
|
config.read("init_scripts/constants.ini")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
conn = psycopg2.connect(
|
||||||
|
host=config["postgres"]["host"],
|
||||||
|
database=config["postgres"]["database"],
|
||||||
|
user=config["postgres"]["user"],
|
||||||
|
password=config["postgres"]["password"],
|
||||||
|
port=int(config["postgres"]["port"])
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
print("Trying to connect with database")
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
cache.init_app(app)
|
||||||
|
app.run(host="0.0.0.0", port=8090, debug=True)
|
||||||
|
|
||||||
|
conn.close()
|
||||||
@ -1,4 +1,5 @@
|
|||||||
from flask import Flask, request, jsonify
|
from flask import Flask, request, jsonify
|
||||||
|
from flask_caching import Cache
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import pandas
|
import pandas
|
||||||
import json
|
import json
|
||||||
@ -13,6 +14,7 @@ db_connector = None
|
|||||||
conn = None
|
conn = None
|
||||||
movie_list = None
|
movie_list = None
|
||||||
|
|
||||||
|
|
||||||
def error_decorator(fun):
|
def error_decorator(fun):
|
||||||
def inner1(*args, **kwargs):
|
def inner1(*args, **kwargs):
|
||||||
try:
|
try:
|
||||||
@ -22,18 +24,21 @@ def error_decorator(fun):
|
|||||||
|
|
||||||
return inner1
|
return inner1
|
||||||
|
|
||||||
|
|
||||||
@app.route("/", methods=["GET"])
|
@app.route("/", methods=["GET"])
|
||||||
@cache.cached(timeout=69)
|
@cache.cached(timeout=69)
|
||||||
def hello():
|
def hello():
|
||||||
return jsonify({"response": "Hello there", "time": datetime.now()}), 200
|
return jsonify({"response": "Hello there", "time": datetime.now()}), 200
|
||||||
|
|
||||||
#endpoint do wyciągania danych o userze
|
|
||||||
|
# endpoint do wyciągania danych o userze
|
||||||
@app.route("/api/v3/get/<string:username>", methods=["GET"])
|
@app.route("/api/v3/get/<string:username>", methods=["GET"])
|
||||||
def access_user(username):
|
def access_user(username):
|
||||||
return jsonify({"us": "er"}), 200
|
return jsonify({"us": "er"}), 200
|
||||||
|
|
||||||
#endpoint służący do zapisu danych nowostworzonego użytkownika, podajemy mu
|
|
||||||
#id z oautha oraz login
|
# endpoint służący do zapisu danych nowo stworzonego użytkownika, podajemy mu
|
||||||
|
# id z oautha oraz login
|
||||||
@app.route("/api/v3/add/<string:oauth_ID>/<string:username>", methods=["POST"])
|
@app.route("/api/v3/add/<string:oauth_ID>/<string:username>", methods=["POST"])
|
||||||
def add_user(oauth_ID, username):
|
def add_user(oauth_ID, username):
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
@ -54,7 +59,7 @@ def add_user(oauth_ID, username):
|
|||||||
return jsonify({"status": "success"}), 200
|
return jsonify({"status": "success"}), 200
|
||||||
|
|
||||||
|
|
||||||
#roboczy endpoint służący do wyciąganiu rekomendacji
|
# roboczy endpoint służący do wyciąganiu rekomendacji
|
||||||
@app.route("/api/v3/ai/<string:oauth_ID>", methods=["GET"])
|
@app.route("/api/v3/ai/<string:oauth_ID>", methods=["GET"])
|
||||||
def get_recommendations(oauth_ID):
|
def get_recommendations(oauth_ID):
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
@ -67,6 +72,7 @@ def get_recommendations(oauth_ID):
|
|||||||
headers={'Content-Type': 'application/json'})
|
headers={'Content-Type': 'application/json'})
|
||||||
return jsonify(response.json()), 200
|
return jsonify(response.json()), 200
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/v3/get_movie/<int:movie_ID>", methods=["GET"])
|
@app.route("/api/v3/get_movie/<int:movie_ID>", methods=["GET"])
|
||||||
def get_movie(movie_ID):
|
def get_movie(movie_ID):
|
||||||
movie_info = movie_list.loc[movie_list['movie_id'] == movie_ID]
|
movie_info = movie_list.loc[movie_list['movie_id'] == movie_ID]
|
||||||
@ -74,8 +80,8 @@ def get_movie(movie_ID):
|
|||||||
return jsonify({"status": "Movie with ID {} doesn't exist".format(movie_ID)}
|
return jsonify({"status": "Movie with ID {} doesn't exist".format(movie_ID)}
|
||||||
), 500
|
), 500
|
||||||
|
|
||||||
cast = json.loads(movie_info["cast"][0].replace('\\"','"'))
|
cast = json.loads(movie_info["cast"][0].replace('\\"', '"'))
|
||||||
crew = json.loads(movie_info["crew"][0].replace('\\"','"'))
|
crew = json.loads(movie_info["crew"][0].replace('\\"', '"'))
|
||||||
|
|
||||||
output_json = {"movie_id": movie_ID,
|
output_json = {"movie_id": movie_ID,
|
||||||
"title": movie_info["title"][0],
|
"title": movie_info["title"][0],
|
||||||
@ -84,6 +90,7 @@ def get_movie(movie_ID):
|
|||||||
|
|
||||||
return jsonify(output_json), 200
|
return jsonify(output_json), 200
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/v3/rate_movie/<string:uID>/<string:movie_ID>/<int:rating>", methods=["POST"])
|
@app.route("/api/v3/rate_movie/<string:uID>/<string:movie_ID>/<int:rating>", methods=["POST"])
|
||||||
def rate_movie(uID, movie_ID, rating):
|
def rate_movie(uID, movie_ID, rating):
|
||||||
movie_info = movie_list.loc[movie_list['movie_id'] == int(movie_ID)]
|
movie_info = movie_list.loc[movie_list['movie_id'] == int(movie_ID)]
|
||||||
@ -146,7 +153,7 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
movie_list = pandas.read_csv(config["movie"]["csv_path"])
|
movie_list = pandas.read_csv(config["movie"]["csv_path"])
|
||||||
cache.init_app(app)
|
cache.init_app(app)
|
||||||
app.run(host="0.0.0.0",port=8090, debug=True)
|
app.run(host="localhost", port=8090, debug=True)
|
||||||
|
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|||||||
24
test_scripts/Test.http
Normal file
24
test_scripts/Test.http
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
###
|
||||||
|
GET http://127.0.0.1:8090/get/boop
|
||||||
|
###
|
||||||
|
POST http://127.0.0.1:8090/api/v3/AI_recommendations
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
[
|
||||||
|
49026,
|
||||||
|
155,
|
||||||
|
312113
|
||||||
|
]
|
||||||
|
###
|
||||||
|
POST http://127.0.0.1:8090/api/v3/add/1111/boop
|
||||||
|
###
|
||||||
|
POST http://127.0.0.1:8090/api/v3/rate_movie/1111/155/4
|
||||||
|
###
|
||||||
|
GET http://localhost:8090/api/get_number_of_ratings
|
||||||
|
###
|
||||||
|
GET http://localhost:8090/api/get_movie_ratings/155
|
||||||
|
###
|
||||||
|
GET http://localhost:8090/api/get_users_number
|
||||||
|
###
|
||||||
|
GET http://localhost:8090/api/get_movie_rating_avg/155
|
||||||
|
###
|
||||||
Loading…
Reference in New Issue
Block a user