from flask import Blueprint, render_template, request, make_response, send_from_directory, current_app from flask_login import login_required from scripts import database import inspect TV = Blueprint("tv", __name__, template_folder="templates") @TV.route("/tv") @login_required def index(): try: page = request.args.get("page", 1, type=int) max_items = request.args.get("max_items", 30, type=int) tv_shows = database.get_all_tv_shows() start = (max_items * (page - 1)) end = len(tv_shows) if len(tv_shows) < max_items * page else max_items * page return render_template("tv/index.html", title="Tv", tv_shows=tv_shows, page=page, max_items=max_items, start=start, end=end, item_count=len(tv_shows)) except Exception as e: current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(type(e))+" "+str(e) @TV.route("/tv/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_shows = [] if query: tv_shows = database.db_search_tv_shows(query) start = (max_items * (page - 1)) end = len(tv_shows) if len(tv_shows) < max_items * page else max_items * page return render_template("tv/search.html", title="Tv", tv_shows=tv_shows, page=page, max_items=max_items, start=start, end=end, item_count=len(tv_shows)) except Exception as e: current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(type(e))+" "+str(e) @TV.route("/tv/", methods=["GET", "POST"]) @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)) database.update_user_tv_movie_data(imdb_id, parent_id, time, length, True if length-time <= 15 else False) return make_response("", 201) else: episodes = database.get_tv_show_episodes_by_imdb_id(imdb_id) season_num = request.args.get("season", type=int, default=episodes[0].season) episode_num = request.args.get("episode", type=int, default=episodes[0].episode) user_tv_show_data = database.db_get_user_tv_show_episodes_data(imdb_id) current_episode = episodes[0] 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) return render_template("tv/episodeViewer.html", title="Tv", 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) except Exception as e: current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(type(e)) + " " + str(e) @TV.route("/tv/get_episode/") @login_required def get_episode(imdb_id): episode_data = database.db_get_episode_by_imdb_id(imdb_id) filename = "S{:02d}E{:02d} - {}.mkv".format(episode_data.season, episode_data.episode, episode_data.title) dir = episode_data.path.replace(filename, "") response = make_response(send_from_directory(dir, filename)) response.headers["content-type"] = "video/webm" return response