2025-05-07 20:19:03 +03:00
|
|
|
from flask import Flask
|
|
|
|
import psycopg2 as psql
|
|
|
|
from os import environ as env
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
db_params = {
|
|
|
|
'database': env.get('DB_NAME'),
|
|
|
|
'user': env.get('DB_USER'),
|
|
|
|
'password': env.get('DB_PASS'),
|
|
|
|
'host': env.get('DB_HOST'),
|
|
|
|
'port': int(env.get('DB_PORT'))
|
|
|
|
}
|
|
|
|
|
|
|
|
db = psql.connect(**db_params)
|
|
|
|
|
2025-05-07 21:03:55 +03:00
|
|
|
@app.route("/movies")
|
|
|
|
def movies():
|
2025-05-07 20:19:03 +03:00
|
|
|
c = db.cursor()
|
2025-05-07 21:03:55 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
c.execute('SELECT uuid, name, scheduled_datetime, movie_details, image_url, max_passes FROM movies')
|
|
|
|
except Exception:
|
|
|
|
return {
|
|
|
|
"error": "Failed to list movies",
|
|
|
|
"result": None
|
|
|
|
}, 503
|
|
|
|
|
|
|
|
res = c.fetchall()
|
|
|
|
c.close()
|
|
|
|
|
|
|
|
return {
|
|
|
|
"error": "",
|
|
|
|
"result": [
|
|
|
|
{
|
|
|
|
"uuid": i[0],
|
|
|
|
"name": i[1],
|
|
|
|
"scheduled_datetime": i[2].strftime("%Y-%m-%d %H:%M:%S"),
|
|
|
|
"movie_details": i[3],
|
|
|
|
"image_url": i[4],
|
|
|
|
"max_passes": i[5]
|
|
|
|
} for i in res
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
@app.route("/movie/<movie_uuid>")
|
|
|
|
def movie_byid(movie_uuid):
|
|
|
|
c = db.cursor()
|
|
|
|
|
2025-05-07 20:19:03 +03:00
|
|
|
try:
|
2025-05-07 21:03:55 +03:00
|
|
|
c.execute(f"SELECT name, scheduled_datetime, movie_details, image_url, max_passes FROM movies WHERE uuid='{movie_uuid}'")
|
2025-05-07 20:19:03 +03:00
|
|
|
except Exception:
|
2025-05-07 21:03:55 +03:00
|
|
|
return {
|
|
|
|
"error": "Failed to search for a movie",
|
|
|
|
"result": None
|
|
|
|
}, 503
|
2025-05-07 20:19:03 +03:00
|
|
|
|
2025-05-07 21:03:55 +03:00
|
|
|
res = c.fetchone()
|
2025-05-07 20:19:03 +03:00
|
|
|
c.close()
|
2025-05-07 21:03:55 +03:00
|
|
|
|
|
|
|
if res:
|
|
|
|
return {
|
|
|
|
"error": "",
|
|
|
|
"result": {
|
|
|
|
"name": res[0],
|
|
|
|
"scheduled_datetime": res[1].strftime("%Y-%m-%d %H:%M:%S"),
|
|
|
|
"movie_details": res[2],
|
|
|
|
"image_url": res[3],
|
|
|
|
"max_passes": res[4]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
return {
|
|
|
|
"error": f"No movie with uuid={movie_uuid} has been found",
|
|
|
|
"result": None
|
|
|
|
}, 404
|