mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-06 18:43:07 +02:00
little cleanup + endpoint for sending info about movies
This commit is contained in:
parent
903a4ccf05
commit
9808014e53
@ -1,9 +1,12 @@
|
|||||||
# instalacja:
|
# instalacja:
|
||||||
pip install -r requirements.txt
|
pip install -r requirements.txt
|
||||||
|
|
||||||
|
# budowanie i uruchamianie obrazu z bazą danych:
|
||||||
|
cd ai_front_connector
|
||||||
|
docker compose up (ewentualnie można też z GUI)
|
||||||
|
|
||||||
# uruchomienie:
|
# uruchomienie:
|
||||||
(Z directory macierzystego)
|
(Z directory macierzystego)
|
||||||
|
|
||||||
python3 ai_front_connector/frontend_AI_connector.py
|
python3 ai_front_connector/frontend_AI_connector.py
|
||||||
|
|
||||||
# gadanie z endpointami
|
# gadanie z endpointami
|
||||||
|
|||||||
@ -1,14 +1,14 @@
|
|||||||
from flask import Flask, request, jsonify
|
from flask import Flask, request, jsonify
|
||||||
import tinydb
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
import pandas
|
||||||
|
import json
|
||||||
|
from configparser import ConfigParser
|
||||||
|
|
||||||
files = []
|
|
||||||
|
|
||||||
DB_PATH = "ai_front_connector/mock_db/db.json"
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
db_connector = None
|
db_connector = None
|
||||||
conn = None
|
conn = None
|
||||||
|
movie_list = None
|
||||||
|
|
||||||
@app.route("/", methods=["GET"])
|
@app.route("/", methods=["GET"])
|
||||||
def hello():
|
def hello():
|
||||||
@ -23,8 +23,6 @@ def access_user(username):
|
|||||||
#id z oautha oraz login
|
#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):
|
||||||
# res = db_connector.search(tinydb.where('username') == username)
|
|
||||||
|
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("select * from users where username='{}';".format(username))
|
cursor.execute("select * from users where username='{}';".format(username))
|
||||||
res = cursor.fetchall()
|
res = cursor.fetchall()
|
||||||
@ -32,9 +30,8 @@ def add_user(oauth_ID, username):
|
|||||||
if len(res):
|
if len(res):
|
||||||
return jsonify({"status": "User already exists"}), 500
|
return jsonify({"status": "User already exists"}), 500
|
||||||
|
|
||||||
# db_connector.insert({"ID": oauth_ID, "username": username})
|
|
||||||
cursor.execute("INSERT INTO users (username, oauth_ID) VALUES ('{}','{}');".format(
|
cursor.execute("INSERT INTO users (username, oauth_ID) VALUES ('{}','{}');".format(
|
||||||
oauth_ID, username
|
username, oauth_ID
|
||||||
))
|
))
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
@ -51,15 +48,36 @@ def get_recommendations(oauth_ID):
|
|||||||
#przesłanie danych do
|
#przesłanie danych do
|
||||||
return jsonify({"movies": ["3", "Wiedźmin 3", "Najlepszy."]}), 200
|
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]
|
||||||
|
if movie_info.empty:
|
||||||
|
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('\\"','"'))
|
||||||
|
|
||||||
|
output_json = {"movie_id": movie_ID,
|
||||||
|
"title": movie_info["title"][0],
|
||||||
|
"cast": cast,
|
||||||
|
"crew": crew}
|
||||||
|
|
||||||
|
return jsonify(output_json), 200
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
db_connector = tinydb.TinyDB(DB_PATH)
|
config = ConfigParser()
|
||||||
|
config.read("ai_front_connector/init_scripts/constants.ini")
|
||||||
|
|
||||||
conn = psycopg2.connect(
|
conn = psycopg2.connect(
|
||||||
host="localhost",
|
host=config["postgres"]["host"],
|
||||||
database="test_db",
|
database=config["postgres"]["database"],
|
||||||
user="root",
|
user=config["postgres"]["user"],
|
||||||
password="root",
|
password=config["postgres"]["password"],
|
||||||
port=5432
|
port=int(config["postgres"]["port"])
|
||||||
)
|
)
|
||||||
|
|
||||||
|
movie_list = pandas.read_csv(config["movie"]["csv_path"])
|
||||||
|
|
||||||
app.run(port=8080, debug=True)
|
app.run(port=8080, debug=True)
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|||||||
9
ai_front_connector/init_scripts/constants.ini
Normal file
9
ai_front_connector/init_scripts/constants.ini
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[postgres]
|
||||||
|
host=localhost
|
||||||
|
database=test_db
|
||||||
|
user=root
|
||||||
|
password=root
|
||||||
|
port=5432
|
||||||
|
|
||||||
|
[movie]
|
||||||
|
csv_path=ai_front_connector/init_scripts/movies.csv
|
||||||
@ -7,4 +7,4 @@ CREATE TABLE users (
|
|||||||
INSERT INTO users (username, oauth_ID) VALUES
|
INSERT INTO users (username, oauth_ID) VALUES
|
||||||
('Mkyong', 40),
|
('Mkyong', 40),
|
||||||
('Ali', 28),
|
('Ali', 28),
|
||||||
('Teoh', 18);
|
('Teoh', 18);
|
||||||
|
|||||||
4804
ai_front_connector/init_scripts/movies.csv
Normal file
4804
ai_front_connector/init_scripts/movies.csv
Normal file
File diff suppressed because one or more lines are too long
@ -1,3 +1,3 @@
|
|||||||
tinydb==4.8.0
|
|
||||||
flask==3.0.3
|
flask==3.0.3
|
||||||
psycopg2==2.9.9
|
psycopg2==2.9.9
|
||||||
|
pandas==2.2.2
|
||||||
Loading…
Reference in New Issue
Block a user