rpiwebapp-public/tv/tv.py

67 lines
2.3 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
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:
print(type(e), 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:
print(type(e), e)
return str(type(e))+" "+str(e)
@TV.route("/tv/<imdb_id>")
@login_required
def episode_viewer(imdb_id):
try:
episodes = database.get_tv_show_episodes_by_imdb_id(imdb_id)
season = request.args.get("season", type=int, default=episodes[0]["season"])
episode = request.args.get("episode", type=int, default=episodes[0]["episode"])
return render_template("tv/episodeViewer.html", title="Tv", episodes=episodes, season_num=season, episode_num=episode)
except Exception as e:
print(type(e), e)
return str(type(e)) + " " + str(e)
@TV.route("/tv/get_episode/<imdb_id>")
@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