rpiwebapp-public/tv_movies/tv_movies.py

165 lines
7.0 KiB
Python
Raw Normal View History

from flask import Blueprint, render_template, request, make_response, send_from_directory, current_app
from flask_login import login_required
from scripts import database, func
import inspect
import datetime
TV_Movies = Blueprint("tv_movies", __name__, template_folder="templates")
@TV_Movies.route("/tv_movies")
@login_required
def index():
try:
page = request.args.get("page", 1, type=int)
max_items = request.args.get("max_items", 30, type=int)
tv = database.get_all_tv_movies()
start = (max_items * (page - 1))
end = len(tv) if len(tv) < max_items * page else max_items * page
tv_dict = []
for tv_item in tv:
item = tv_item[0].__dict__
item.pop('_sa_instance_state', None)
item.pop('path', None)
tv_dict.append((item, tv_item[1]))
return render_template("tv_movies/index.html", title="tv & movies", tv_shows=tv_dict, page=page, max_items=max_items, start=start, end=end, item_count=len(tv))
except Exception as e:
current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))
return str(type(e))+" "+str(e)
@TV_Movies.route("/tv_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")
tv = []
if query:
tv = database.db_search_tv_movie(query)
start = (max_items * (page - 1))
end = len(tv) if len(tv) < max_items * page else max_items * page
tv_dict = []
for tv_item in tv:
item = tv_item[0].__dict__
item.pop('_sa_instance_state', None)
item.pop('path', None)
tv_dict.append((item, tv_item[1]))
return render_template("tv_movies/search.html", title="tv_movies", tv_shows=tv_dict, page=page, max_items=max_items,
start=start, end=end, item_count=len(tv))
except Exception as e:
current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))
return str(type(e))+" "+str(e)
@TV_Movies.route("/tv_movies/<imdb_id>", methods=["GET", "POST"])
@login_required
def tv_movie_viewer(imdb_id):
try:
tv_movie = database.get_tv_movie_by_imdb_id(imdb_id)
if type(tv_movie) is database.Movie:
extended = request.args.get("extended", default=False, type=bool)
directors_cut = request.args.get("directors_cut", default=False, type=bool)
return movie_view(imdb_id, extended=extended, directors_cut=directors_cut)
else:
return episode_viewer(imdb_id)
except Exception as e:
current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))
return str(type(e))+" "+str(e)
@login_required
def movie_view(imdb_id, extended=False, directors_cut=False):
try:
if request.method == "POST":
time = int(request.args.get("time", type=float))
length = int(request.args.get("length", type=float, default=0))
finished = True if request.args.get("finished", default="false") == "true" else False
database.update_user_tv_movie_data(imdb_id, None, time, length, finished, extended, directors_cut)
return make_response("", 201)
else:
movie_data = database.db_get_movie_by_imdb_id(imdb_id, extended=extended, directors_cut=directors_cut)
user_data = database.db_get_user_tv_movie_data(movie_data.imdb_id, extended=extended, directors_cut=directors_cut)
chapters = func.get_chapters(movie_data.path)
if not user_data:
user_data = database.update_user_tv_movie_data(movie_data.imdb_id, None, 0, 0)
return render_template("tv_movies/movieViewer.html", title="Movies: " + movie_data.title, movie=movie_data, user_data=user_data, chapters=chapters)
except Exception as e:
current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))
return str(type(e)) + " " + str(e)
@login_required
def episode_viewer(imdb_id):
try:
if request.method == "POST":
time = int(request.args.get("time", type=float))
parent_id = request.args.get("parent", type=str)
length = int(request.args.get("length", type=float, default=0))
finished = True if request.args.get("finished", default="false") == "true" else False
database.update_user_tv_movie_data(imdb_id, parent_id, time, length, finished)
return make_response("", 201)
else:
tv_show = database.get_tv_show(imdb_id)
episodes = database.get_tv_show_episodes_by_imdb_id(imdb_id)
season_num = request.args.get("season", type=int, default=None)
episode_num = request.args.get("episode", type=int, default=None)
user_tv_show_data = database.db_get_user_tv_show_episodes_data(imdb_id)
if not season_num and not episode_num:
(current_episode, user_data) = database.db_get_current_tv_show_episode_and_data(imdb_id, episodes)
season_num = current_episode.season
episode_num = current_episode.episode
chapters = func.get_chapters(current_episode.path)
if not user_data:
user_data = database.update_user_tv_movie_data(current_episode.imdb_id, imdb_id, 0, 0)
else:
current_episode = episodes[0]
user_data = database.UserTvMovieData(("", "", "", 0, 0, False, datetime.datetime.min))
for episode in episodes:
if episode.season == season_num and episode.episode == episode_num:
current_episode = episode
user_data = database.db_get_user_tv_movie_data(current_episode.imdb_id)
if not user_data:
user_data = database.update_user_tv_movie_data(current_episode.imdb_id, imdb_id, 0, 0)
break
else:
for episode in episodes:
if episode.season == season_num:
current_episode = episode
episode_num = episode.episode
user_data = database.db_get_user_tv_movie_data(current_episode.imdb_id)
if not user_data:
user_data = database.update_user_tv_movie_data(current_episode.imdb_id, imdb_id, 0, 0)
break
chapters = func.get_chapters(current_episode.path)
return render_template("tv_movies/episodeViewer.html", title="Tv: " + tv_show.title, episodes=episodes, season_num=season_num, episode_num=episode_num, episode=current_episode, user_data=user_data, user_tv_show_data=user_tv_show_data, chapters=chapters)
except Exception as e:
current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))
return str(type(e)) + " " + str(e)
@TV_Movies.route("/tv_movies/get_movie/<imdb_id>")
@login_required
def get_movie(imdb_id):
extended = request.args.get("extended", default=False, type=bool)
directors_cut = request.args.get("directors_cut", default=False, type=bool)
movie_data = database.db_get_movie_by_imdb_id(imdb_id, extended=extended, directors_cut=directors_cut)
filename = movie_data.path.replace(func.MOVIES_DIRECTORY, "")
response = make_response(send_from_directory(func.MOVIES_DIRECTORY, filename))
response.headers["content-type"] = "video/webm"
return response
@TV_Movies.route("/tv_movies/get_episode/<imdb_id>")
@login_required
def get_episode(imdb_id):
episode_data = database.db_get_episode_by_imdb_id(imdb_id)
filename = episode_data.path.replace(func.TV_SHOWS_DIRECTORY, "")
response = make_response(send_from_directory(func.TV_SHOWS_DIRECTORY, filename))
response.headers["content-type"] = "video/webm"
return response