rpiwebapp-public/movies/movies.py

105 lines
3.6 KiB
Python

from flask import Blueprint, render_template, request, make_response, send_file, send_from_directory
from flask_login import login_required
from scripts import database, func
Movies = Blueprint("movies", __name__, template_folder="templates")
@Movies.route("/movies")
@login_required
def index():
try:
page = request.args.get("page", 1, type=int)
max_items = request.args.get("max_items", 30, type=int)
movies = database.db_get_all_movies()
start = (max_items*(page-1))
end = len(movies) if len(movies) < max_items*page else max_items*page
return render_template("movies/index.html", title="Movies", movies=movies, page=page, max_items=max_items, start=start, end=end, item_count=len(movies))
except Exception as e:
print(type(e), e)
return str(type(e)) + " " + str(e)
@Movies.route("/movies/search")
@login_required
def search():
try:
page = request.args.get("page", 1, type=int)
max_items = request.args.get("max_items", 30, type=int)
start = 0
end = 0
query = request.args.get("q")
results = []
if query:
results = database.db_search_movies(query)
start = (max_items*(page-1))
end = len(results) if len(results) < max_items*page else max_items*page
return render_template("movies/search.html", title="Movies", movies=results, page=page, max_items=max_items, start=start, end=end, item_count=len(results))
except Exception as e:
print(type(e), e)
return str(type(e)) + " " + str(e)
@Movies.route("/movies/<imdb_id>")
@login_required
def movie_view(imdb_id):
try:
movie_data = database.db_get_movie_by_imdb_id(imdb_id)
return render_template("movies/movieViewer.html", title="Movies: " + movie_data["title"], movie=movie_data)
except Exception as e:
print(type(e), e)
return str(type(e)) + " " + str(e)
@Movies.route("/movies/<imdb_id>/extended")
@login_required
def movie_view_extended(imdb_id):
try:
movie_data = database.db_get_movie_by_imdb_id(imdb_id, extended=1)
return render_template("movies/movieViewer.html", title="Movies: " + movie_data["title"], movie=movie_data)
except Exception as e:
print(type(e), e)
return str(type(e)) + " " + str(e)
@Movies.route("/movies/<imdb_id>/directors_cut")
@login_required
def movie_view_directors_cut(imdb_id):
try:
movie_data = database.db_get_movie_by_imdb_id(imdb_id, directors_cut=1)
return render_template("movies/movieViewer.html", title="Movies: " + movie_data["title"], movie=movie_data)
except Exception as e:
print(type(e), e)
return str(type(e)) + " " + str(e)
@Movies.route("/movies/get_movie/<imdb_id>")
@login_required
def get_movie(imdb_id):
movie_data = database.db_get_movie_by_imdb_id(imdb_id)
filename = movie_data["title"]+" ("+str(movie_data["year"])+").mkv"
response = make_response(send_from_directory(func.MOVIES_DIRECTORY, filename))
response.headers["content-type"] = "video/webm"
return response
@Movies.route("/movies/get_movie/<imdb_id>/extended")
@login_required
def get_movie_extended(imdb_id):
movie_data = database.db_get_movie_by_imdb_id(imdb_id, extended=1)
filename = movie_data["title"]+" ("+str(movie_data["year"])+").mkv"
response = make_response(send_from_directory(func.MOVIES_DIRECTORY, filename))
response.headers["content-type"] = "video/webm"
return response
@Movies.route("/movies/get_movie/<imdb_id>/directors_cut")
@login_required
def get_movie_directors_cut(imdb_id):
movie_data = database.db_get_movie_by_imdb_id(imdb_id, directors_cut=1)
filename = movie_data["title"]+" ("+str(movie_data["year"])+").mkv"
response = make_response(send_from_directory(func.MOVIES_DIRECTORY, filename))
response.headers["content-type"] = "video/webm"
return response