rpiwebapp-public/tv/tv.py

92 lines
3.8 KiB
Python
Raw Normal View History

from flask import Blueprint, render_template, request, make_response, send_from_directory, current_app
2019-07-30 15:19:03 -07:00
from flask_login import login_required
from scripts import database
import inspect
import datetime
2019-07-30 15:19:03 -07:00
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:
2019-08-23 21:34:42 -07:00
current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))
2019-07-30 15:19:03 -07:00
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:
2019-08-23 21:34:42 -07:00
current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))
2019-07-30 15:19:03 -07:00
return str(type(e))+" "+str(e)
@TV.route("/tv/<imdb_id>", methods=["GET", "POST"])
2019-07-30 15:19:03 -07:00
@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=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
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.now()))
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)
2019-07-30 15:19:03 -07:00
except Exception as e:
2019-08-23 21:34:42 -07:00
current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))
2019-07-30 15:19:03 -07:00
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, "")
2019-07-30 15:19:03 -07:00
response = make_response(send_from_directory(dir, filename))
response.headers["content-type"] = "video/webm"
return response