mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 14:43:08 +02:00
little cleanup + endpoint for sending info about movies
This commit is contained in:
parent
903a4ccf05
commit
9808014e53
@ -1,9 +1,12 @@
|
||||
# instalacja:
|
||||
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:
|
||||
(Z directory macierzystego)
|
||||
|
||||
python3 ai_front_connector/frontend_AI_connector.py
|
||||
|
||||
# gadanie z endpointami
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
from flask import Flask, request, jsonify
|
||||
import tinydb
|
||||
import psycopg2
|
||||
import pandas
|
||||
import json
|
||||
from configparser import ConfigParser
|
||||
|
||||
files = []
|
||||
|
||||
DB_PATH = "ai_front_connector/mock_db/db.json"
|
||||
|
||||
app = Flask(__name__)
|
||||
db_connector = None
|
||||
conn = None
|
||||
movie_list = None
|
||||
|
||||
@app.route("/", methods=["GET"])
|
||||
def hello():
|
||||
@ -23,8 +23,6 @@ def access_user(username):
|
||||
#id z oautha oraz login
|
||||
@app.route("/api/v3/add/<string:oauth_ID>/<string:username>", methods=["POST"])
|
||||
def add_user(oauth_ID, username):
|
||||
# res = db_connector.search(tinydb.where('username') == username)
|
||||
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select * from users where username='{}';".format(username))
|
||||
res = cursor.fetchall()
|
||||
@ -32,9 +30,8 @@ def add_user(oauth_ID, username):
|
||||
if len(res):
|
||||
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(
|
||||
oauth_ID, username
|
||||
username, oauth_ID
|
||||
))
|
||||
|
||||
conn.commit()
|
||||
@ -51,15 +48,36 @@ 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]
|
||||
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__":
|
||||
db_connector = tinydb.TinyDB(DB_PATH)
|
||||
config = ConfigParser()
|
||||
config.read("ai_front_connector/init_scripts/constants.ini")
|
||||
|
||||
conn = psycopg2.connect(
|
||||
host="localhost",
|
||||
database="test_db",
|
||||
user="root",
|
||||
password="root",
|
||||
port=5432
|
||||
host=config["postgres"]["host"],
|
||||
database=config["postgres"]["database"],
|
||||
user=config["postgres"]["user"],
|
||||
password=config["postgres"]["password"],
|
||||
port=int(config["postgres"]["port"])
|
||||
)
|
||||
|
||||
movie_list = pandas.read_csv(config["movie"]["csv_path"])
|
||||
|
||||
app.run(port=8080, debug=True)
|
||||
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
|
||||
('Mkyong', 40),
|
||||
('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
|
||||
psycopg2==2.9.9
|
||||
psycopg2==2.9.9
|
||||
pandas==2.2.2
|
||||
Loading…
Reference in New Issue
Block a user