mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 14:43:08 +02:00
Naprawiony stuff Alkaratusa, przerobione na flask, małe poprawki
This commit is contained in:
parent
fb86be5af8
commit
b133fa1b07
@ -1,24 +0,0 @@
|
||||
from User import User
|
||||
from Rating import Rating
|
||||
|
||||
users = [
|
||||
User(1, "Lola"),
|
||||
User(2, "Tony"),
|
||||
User(3, "Lorry"),
|
||||
User(4, "Betty")
|
||||
]
|
||||
|
||||
ratings = [
|
||||
Rating(1, 1, 1, 5, "15-06-2024 15:04:32"),
|
||||
Rating(2, 1, 2, 6, "15-06-2024 15:04:36"),
|
||||
Rating(3, 2, 1, 7, "15-06-2024 15:04:40"),
|
||||
Rating(4, 2, 2, 8, "15-06-2024 15:04:44"),
|
||||
Rating(5, 2, 3, 9, "15-06-2024 15:04:48"),
|
||||
Rating(6, 3, 1, 10, "15-06-2024 15:04:52"),
|
||||
Rating(7, 3, 2, 6, "15-06-2024 15:04:56"),
|
||||
Rating(8, 3, 3, 5, "15-06-2024 15:05:00"),
|
||||
Rating(9, 4, 1, 4, "15-06-2024 15:05:04"),
|
||||
Rating(10, 4, 2, 3, "15-06-2024 15:05:08"),
|
||||
Rating(11, 4, 3, 2, "15-06-2024 15:05:12"),
|
||||
Rating(12, 4, 4, 1, "15-06-2024 15:05:16")
|
||||
]
|
||||
@ -1,9 +0,0 @@
|
||||
|
||||
|
||||
class Rating:
|
||||
def __init__(self, id, movie_id, user_id, ranting, time):
|
||||
self.id=id
|
||||
self.movie_id=movie_id
|
||||
self.user_id=user_id
|
||||
self.ranting=ranting
|
||||
self.time=time
|
||||
@ -1,33 +0,0 @@
|
||||
from User import User
|
||||
from Rating import Rating
|
||||
from MockUps import users, ratings
|
||||
|
||||
|
||||
def get_ratings():
|
||||
return ratings
|
||||
|
||||
|
||||
def get_number_of_ratings():
|
||||
return len(get_ratings())
|
||||
|
||||
|
||||
def get_ratings_in_time(begin_time, end_time):
|
||||
pass
|
||||
|
||||
|
||||
def get_ratings_of_movie(movie_id):
|
||||
results = []
|
||||
for ranting in ratings:
|
||||
if ranting.movie_id == movie_id:
|
||||
results.append(ranting)
|
||||
return results
|
||||
|
||||
|
||||
def get_ratings_of_movie_numbers(movie_id):
|
||||
return len(get_ratings_of_movie(movie_id))
|
||||
|
||||
|
||||
def get_ratings_of_movie_in_time(movie_id, begin_time, end_time):
|
||||
pass
|
||||
|
||||
|
||||
@ -1,4 +0,0 @@
|
||||
class User:
|
||||
def __init__(self, user_id, name):
|
||||
self.id = user_id
|
||||
self.name = name
|
||||
@ -1,25 +0,0 @@
|
||||
from User import User
|
||||
from Rating import Rating
|
||||
from MockUps import users, ratings
|
||||
|
||||
|
||||
def get_number_of_users():
|
||||
return len(users)
|
||||
|
||||
|
||||
def get_user_ratings(user_id):
|
||||
results = []
|
||||
for rating in ratings:
|
||||
if rating.user_id == user_id:
|
||||
results.append(rating)
|
||||
return results
|
||||
|
||||
|
||||
def get_user_ratings_number(user_id):
|
||||
return len(get_user_ratings(user_id))
|
||||
|
||||
|
||||
def get_user_ratings_in_time(user_id, begin, end):
|
||||
pass
|
||||
|
||||
|
||||
@ -1,51 +0,0 @@
|
||||
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")
|
||||
@cache.cached(timeout=1000)
|
||||
def get_number_of_ratings():
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select count(*) as num_of_ratings from ratings")
|
||||
res = cursor.fetchall()
|
||||
|
||||
return len(res['num_of_ratings'])
|
||||
|
||||
|
||||
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()
|
||||
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()
|
||||
@ -8,32 +8,36 @@ from datetime import datetime
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
|
||||
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
|
||||
db_connector = None
|
||||
conn = None
|
||||
movie_list = None
|
||||
|
||||
|
||||
def error_decorator(fun):
|
||||
def inner1(*args, **kwargs):
|
||||
try:
|
||||
fun(*args, **kwargs)
|
||||
except psycopg2.DatabaseError:
|
||||
return jsonify({"status": "Something... unexpected has occured :sweat_smile:"}), 500
|
||||
|
||||
|
||||
return inner1
|
||||
|
||||
|
||||
@app.route("/", methods=["GET"])
|
||||
@cache.cached(timeout=69)
|
||||
def hello():
|
||||
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"])
|
||||
def access_user(username):
|
||||
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"])
|
||||
def add_user(oauth_ID, username):
|
||||
cursor = conn.cursor()
|
||||
@ -50,11 +54,11 @@ def add_user(oauth_ID, username):
|
||||
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
|
||||
|
||||
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"])
|
||||
def get_recommendations(oauth_ID):
|
||||
#request od frontu na rekomendacje
|
||||
@ -62,6 +66,7 @@ def get_recommendations(oauth_ID):
|
||||
#przesłanie danych do
|
||||
return jsonify({"movies": ["3", "Wiedźmin 3", "Najlepszy."]}), 200
|
||||
|
||||
|
||||
@app.route("/api/v3/get_movie/<int:movie_ID>", methods=["GET"])
|
||||
def get_movie(movie_ID):
|
||||
movie_info = movie_list.loc[movie_list['movie_id'] == movie_ID]
|
||||
@ -69,8 +74,8 @@ def get_movie(movie_ID):
|
||||
return jsonify({"status": "Movie with ID {} doesn't exist".format(movie_ID)}
|
||||
), 500
|
||||
|
||||
cast = json.loads(movie_info["cast"][0].replace('\\"','"'))
|
||||
crew = json.loads(movie_info["crew"][0].replace('\\"','"'))
|
||||
cast = json.loads(movie_info["cast"][0].replace('\\"', '"'))
|
||||
crew = json.loads(movie_info["crew"][0].replace('\\"', '"'))
|
||||
|
||||
output_json = {"movie_id": movie_ID,
|
||||
"title": movie_info["title"][0],
|
||||
@ -79,6 +84,7 @@ def get_movie(movie_ID):
|
||||
|
||||
return jsonify(output_json), 200
|
||||
|
||||
|
||||
@app.route("/api/v3/rate_movie/<string:uID>/<string:movie_ID>/<int:rating>", methods=["POST"])
|
||||
def rate_movie(uID, movie_ID, rating):
|
||||
movie_info = movie_list.loc[movie_list['movie_id'] == int(movie_ID)]
|
||||
@ -115,7 +121,7 @@ def rate_movie(uID, movie_ID, rating):
|
||||
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
|
||||
|
||||
return jsonify({"status": "success"}), 200
|
||||
|
||||
|
||||
@ -133,17 +139,15 @@ if __name__ == "__main__":
|
||||
port=int(config["postgres"]["port"])
|
||||
)
|
||||
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print("Trying to connect with database")
|
||||
continue
|
||||
else:
|
||||
break
|
||||
|
||||
|
||||
movie_list = pandas.read_csv(config["movie"]["csv_path"])
|
||||
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()
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
[postgres]
|
||||
host=db
|
||||
host=localhost
|
||||
database=test_db
|
||||
user=root
|
||||
password=root
|
||||
|
||||
@ -1,15 +0,0 @@
|
||||
### GET request to example server
|
||||
GET http://127.0.0.1:5000/boop
|
||||
|
||||
###
|
||||
|
||||
POST http://127.0.0.1:5000/api/v3/AI_recommendations
|
||||
Content-Type: application/json
|
||||
|
||||
[
|
||||
49026,
|
||||
155,
|
||||
312113
|
||||
]
|
||||
|
||||
###
|
||||
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