From 2a5f7ae67fe669e14f181734800c766c937579c2 Mon Sep 17 00:00:00 2001 From: Matthew Welch Date: Sat, 11 Apr 2020 17:14:06 -0700 Subject: [PATCH] updated the comics viewer to dynamically load all the comics pages moved pagination code from python to javascript temporarily removed comics searched until it can be re-written for the new pagination --- comics/comics.py | 66 +++--- .../templates/comics/PublisherSeriesList.html | 12 -- comics/templates/comics/comicGallery.html | 203 +++++++++++++++++- comics/templates/comics/index.html | 45 ++-- comics/templates/comics/publisherList.html | 12 -- .../templates/comics/publisherSeriesView.html | 27 ++- comics/templates/comics/seriesList.html | 12 -- comics/templates/comics/seriesView.html | 28 ++- rpiWebApp.py | 2 +- scripts/database.py | 34 ++- scripts/func.py | 70 ++++++ templates/base.html | 76 +++++++ templates/pagination.html | 48 +---- .../templates/tv_movies/episodeViewer.html | 37 +++- tv_movies/templates/tv_movies/index.html | 41 +++- .../templates/tv_movies/movieViewer.html | 20 +- tv_movies/templates/tv_movies/search.html | 41 +++- .../templates/tv_movies/tvMovieList.html | 25 --- tv_movies/tv_movies.py | 39 +++- 19 files changed, 629 insertions(+), 209 deletions(-) delete mode 100644 comics/templates/comics/PublisherSeriesList.html delete mode 100644 comics/templates/comics/publisherList.html delete mode 100644 comics/templates/comics/seriesList.html delete mode 100644 tv_movies/templates/tv_movies/tvMovieList.html diff --git a/comics/comics.py b/comics/comics.py index 7af1b6c..dd80ad2 100644 --- a/comics/comics.py +++ b/comics/comics.py @@ -2,8 +2,7 @@ from flask import Blueprint, render_template, request, make_response, current_ap from flask_login import login_required from urllib import parse -from io import BytesIO -from wand.image import Image +import filetype import os, pytz, datetime import inspect @@ -91,52 +90,41 @@ def comics_publisher(publisher): start = (max_items * (page - 1)) end = len(comics) if len(comics) < max_items * page else max_items * page if issue: - if page_number: - return comic_viewer(publisher, series, series_year, issue) return comic_gallery(publisher, series, series_year, issue) - return render_template("comics/seriesView.html", title="Comics", comics=comics, + comics_dict = [] + for i in comics: + item = i.__dict__ + item.pop('_sa_instance_state', None) + item.pop('path', None) + comics_dict.append(item) + return render_template("comics/seriesView.html", title="Comics", comics=comics_dict, start=start, end=end, page=page, max_items=max_items, item_count=len(comics)) - return render_template("comics/publisherSeriesView.html", title="Comics", publisher_series=publisher_series, + pub_series_dict = [] + for i in publisher_series: + item = i.__dict__ + item.pop('_sa_instance_state', None) + item.pop('path', None) + pub_series_dict.append(item) + return render_template("comics/publisherSeriesView.html", title="Comics", publisher_series=pub_series_dict, start=start, end=end, page=page, max_items=max_items, item_count=len(publisher_series)) except Exception as e: current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(type(e)) + " " + str(e) -def comic_viewer(publisher, series, series_year, issue): - try: - on_mobile = False - if request.user_agent.platform in MOBILE_DEVICES: - on_mobile = True - publisher_parsed = parse.quote(publisher) - series_parsed = parse.quote(series) - page_number = int(request.args.get("pageNumber")) - meta = database.db_get_comic(publisher, series, series_year, issue) - page_count = int(meta.pagecount) - - prev_page = page_number - 1 - next_page = page_number + 1 - if next_page >= page_count: - next_page = 0 - if prev_page < 0: - prev_page = page_count - 1 - prev_url = "/comics/{}?series={}&seriesYear={}&issue={}&pageNumber={}".format(publisher_parsed, series_parsed, series_year, issue, prev_page) - next_url = "/comics/{}?series={}&seriesYear={}&issue={}&pageNumber={}".format(publisher_parsed, series_parsed, series_year, issue, next_page) - title = "Comics: "+meta.series+": #"+meta.issuetext+" "+(meta.title or "") - return render_template("comics/comicView.html", title=title, on_mobile=on_mobile, prev_url=prev_url, next_url=next_url, comic=meta, page_number=page_number) - except Exception as e: - current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) - return str(type(e)) + " " + str(e) - - -def comic_gallery(publisher, series, series_year, issue): +@Comics.route("/comics/") +@login_required +def comic_gallery(comic_id): try: page = request.args.get("page", 1, type=int) max_items = request.args.get("max_items", 30, type=int) - meta = database.db_get_comic(publisher, series, series_year, issue) + meta = database.db_get_comic(comic_id) start = (max_items*(page-1)) end = meta.pagecount if meta.pagecount < max_items*page else max_items*page - return render_template("comics/comicGallery.html", title="Comics", comic=meta, start=start, end=end, page=page, max_items=max_items, item_count=meta.pagecount) + comic_dict = meta.__dict__ + comic_dict.pop('_sa_instance_state', None) + comic_dict.pop('path', None) + return render_template("comics/comicGallery.html", title="Comics", comic=comic_dict, start=start, end=end, page=page, max_items=max_items, item_count=meta.pagecount) except Exception as e: current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(type(e)) + " " + str(e) @@ -147,13 +135,13 @@ def comic_gallery(publisher, series, series_year, issue): def get_comic_page(comic_id, page_number): meta = database.db_get_comic_by_id(comic_id) comic = func.open_comic(meta.path) - byte_image = BytesIO(comic.getPage(page_number)) - image = Image(file=byte_image) - response = make_response(image.make_blob()) + byte_image = comic.getPage(page_number) + type = filetype.guess(byte_image).mime + response = make_response(byte_image) + response.headers["content-type"] = type response.headers["cache-control"] = "public" date = pytz.utc.localize(datetime.datetime.utcfromtimestamp(os.path.getmtime(meta.path))) response.headers["last-modified"] = date.strftime('%a, %d %b %Y %H:%M:%S %Z') - response.headers["content-type"] = "image/" + image.format return response diff --git a/comics/templates/comics/PublisherSeriesList.html b/comics/templates/comics/PublisherSeriesList.html deleted file mode 100644 index 5e1881e..0000000 --- a/comics/templates/comics/PublisherSeriesList.html +++ /dev/null @@ -1,12 +0,0 @@ -{% for i in range(start, end) %} - -{% endfor %} diff --git a/comics/templates/comics/comicGallery.html b/comics/templates/comics/comicGallery.html index f5f5a12..c7916b2 100644 --- a/comics/templates/comics/comicGallery.html +++ b/comics/templates/comics/comicGallery.html @@ -1,22 +1,205 @@ {% extends "base.html" %} +{% block head %} + + +{% endblock %} + {% block content %}
{% include "pagination.html" %}
-
- {% for page_number in range(start, end) %} - - {% endfor %} -
+
{% include "pagination.html" %}
+ +
+ × +
+ + +
+
+{% endblock %} + +{% block footer %} + + {% endblock %} diff --git a/comics/templates/comics/index.html b/comics/templates/comics/index.html index f72aebf..07af716 100644 --- a/comics/templates/comics/index.html +++ b/comics/templates/comics/index.html @@ -1,15 +1,15 @@ {% extends "base.html" %} {% block nav %} - +{# #} {% endblock %} {% block content %} @@ -17,11 +17,32 @@ {% include "pagination.html" %}
-
- {% include "comics/publisherList.html" %} -
+
{% include "pagination.html" %}
{% endblock %} + +{% block footer %} + +{% endblock %} diff --git a/comics/templates/comics/publisherList.html b/comics/templates/comics/publisherList.html deleted file mode 100644 index 1b183e0..0000000 --- a/comics/templates/comics/publisherList.html +++ /dev/null @@ -1,12 +0,0 @@ -{% for i in range(start, end) %} - -{% endfor %} diff --git a/comics/templates/comics/publisherSeriesView.html b/comics/templates/comics/publisherSeriesView.html index 2627160..2a98e14 100644 --- a/comics/templates/comics/publisherSeriesView.html +++ b/comics/templates/comics/publisherSeriesView.html @@ -5,11 +5,32 @@ {% include "pagination.html" %}
-
- {% include "comics/PublisherSeriesList.html" %} -
+
{% include "pagination.html" %}
{% endblock %} + +{% block footer %} + +{% endblock %} diff --git a/comics/templates/comics/seriesList.html b/comics/templates/comics/seriesList.html deleted file mode 100644 index 1028ee2..0000000 --- a/comics/templates/comics/seriesList.html +++ /dev/null @@ -1,12 +0,0 @@ -{% for i in range(start, end) %} - -{% endfor %} diff --git a/comics/templates/comics/seriesView.html b/comics/templates/comics/seriesView.html index c321ee7..0ade634 100644 --- a/comics/templates/comics/seriesView.html +++ b/comics/templates/comics/seriesView.html @@ -5,11 +5,33 @@ {% include "pagination.html" %}
-
- {% include "comics/seriesList.html" %} -
+
{% include "pagination.html" %}
{% endblock %} + +{% block footer %} + +{% endblock %} diff --git a/rpiWebApp.py b/rpiWebApp.py index ac9ace0..fee1d00 100644 --- a/rpiWebApp.py +++ b/rpiWebApp.py @@ -261,7 +261,7 @@ def root(): @login_required def home(): try: - return render_template("home.html", title="Hello World", current_user=current_user) + return render_template("home.html", title="Home", current_user=current_user) except Exception as e: current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) return str(e) diff --git a/scripts/database.py b/scripts/database.py index 9f06ca3..b11c45c 100644 --- a/scripts/database.py +++ b/scripts/database.py @@ -7,7 +7,7 @@ from wand.image import Image from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import create_engine from sqlalchemy.exc import IntegrityError -from sqlalchemy import Column, Integer, String, BLOB, Boolean, DateTime, Numeric, func, over +from sqlalchemy import Column, Integer, String, BLOB, Boolean, DateTime, Numeric, func, over, ARRAY, TIMESTAMP from sqlalchemy.orm import sessionmaker, scoped_session from sqlalchemy.pool import NullPool from sqlalchemy.sql.expression import cast @@ -295,6 +295,28 @@ class UserTvMovieData(Base): current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) +class TvMovieKeywords(Base): + __tablename__ = "tv_movie_keywords" + + imdb_id = Column(String) + extended = Column(Boolean, default=False) + directors_cut = Column(Boolean, default=False) + key_words = Column(ARRAY(String)) + id = Column(Integer, primary_key=True) + date = Column(TIMESTAMP) + + def __init__(self, data): + i = 0 + try: + for column in self.__table__.columns: + if column.name == "id" or column.name == "date": + continue + setattr(self, column.name, data[i]) + i += 1 + except Exception as e: + current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + + """class UserComicData(Base): __tablename__ = "user_comic_data" @@ -447,7 +469,6 @@ def db_get_series_by_publisher(publisher): def db_get_comics_in_series(series, publisher, series_year): session = Session() - current_app.logger.info(str(session.query(Comic).filter(Comic.publisher == publisher, Comic.series == series, Comic.seriesyear == series_year).order_by(Comic.issue))) result = session.query(Comic).filter(Comic.publisher == publisher, Comic.series == series, Comic.seriesyear == series_year).order_by(Comic.issue).all() comics = result return comics @@ -472,9 +493,10 @@ def db_get_thumbnail_by_id_page(comic_id, pageNumber): return thumbnail -def db_get_comic(publisher, series, series_year, issue): +def db_get_comic(comic_id): session = Session() - comic = session.query(Comic).filter(Comic.publisher == publisher, Comic.series == series, Comic.seriesyear == series_year, Comic.issue == issue).one_or_none() + comic = session.query(Comic).filter(Comic.id == comic_id).one_or_none() + #comic = session.query(Comic).filter(Comic.publisher == publisher, Comic.series == series, Comic.seriesyear == series_year, Comic.issue == issue).one_or_none() return comic @@ -615,6 +637,10 @@ def get_tv_movie_by_imdb_id(imdb_id, extended=False, directors_cut=False): return result +def get_currently_watching(): + pass + + def get_all_tv_shows(): session = Session() result = session.query(TvShow).order_by(TvShow.title, TvShow.year).all() diff --git a/scripts/func.py b/scripts/func.py index f1665f8..0a5d64e 100644 --- a/scripts/func.py +++ b/scripts/func.py @@ -1,12 +1,14 @@ from flask import current_app from comicapi import comicarchive from blinker import Namespace +from datetime import timedelta from io import BytesIO from wand.image import Image import os, re import inspect import json +import enzyme from scripts import database @@ -353,3 +355,71 @@ def get_tv_episode(path): tv_episodes_loaded.send("anonymous", tv_episodes=episodes) current_app.logger.info("finished load tv episode") + +def get_chapters(path): + try: + with open(path, 'rb') as f: + mkv = enzyme.MKV(f) + except Exception as e: + current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + mkv_info = {} + for chapter in mkv.chapters: + if chapter.string == "Intro": + mkv_info["intro"] = { + "start": chapter.start.seconds, + "end": timedelta(microseconds=chapter.end//1000).seconds + } + if chapter.string == "Credits": + mkv_info["credits"] = {"start": chapter.start.seconds} + if chapter.string == "end-credit scene": + if "end-credit scene" not in mkv_info.keys(): + mkv_info["end-credit scene"] = [] + end_credit = {"start": chapter.start.seconds} + if chapter.end: + end_credit["end"] = timedelta(microseconds=chapter.end//1000).seconds + mkv_info["end-credit scene"].append(end_credit) + return mkv_info + + +def get_tags(path): + try: + with open(path, 'rb') as f: + mkv = enzyme.MKV(f) + except Exception as e: + current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e)) + mkv_info = {} + for tag in mkv.tags: + if tag.targets.data[0].data == 70: + mkv_info["collection"] = {} + for simple in tag.simpletags: + if simple.name == "TITLE": + mkv_info["collection"]["title"] = simple.string + if simple.name == "TOTAL_PARTS": + mkv_info["collection"]["episodes"] = int(simple.string) + if simple.name == "KEYWORDS": + mkv_info["collection"]["key_words"] = simple.string.split(",") + if simple.name == "DATE_RELEASED": + mkv_info["collection"]["year"] = int(simple.string) + if simple.name == "SUMMARY": + mkv_info["collection"]["summary"] = simple.string + if tag.targets.data[0].data == 60: + mkv_info["season"] = {} + for simple in tag.simpletags: + if simple.name == "TITLE": + mkv_info["season"]["title"] = simple.string + if simple.name == "TOTAL_PARTS": + mkv_info["season"]["episodes"] = int(simple.string) + if tag.targets.data[0].data == 50: + mkv_info["movie"] = {} + for simple in tag.simpletags: + if simple.name == "TITLE": + mkv_info["movie"]["title"] = simple.string + if simple.name == "DATE_RELEASED": + mkv_info["movie"]["year"] = int(simple.string) + if simple.name == "PART_NUMBER": + mkv_info["movie"]["episode"] = int(simple.string) + if simple.name == "KEYWORDS": + mkv_info["movie"]["key_words"] = simple.string.split(",") + if simple.name == "SUMMARY": + mkv_info["movie"]["summary"] = simple.string + return mkv_info diff --git a/templates/base.html b/templates/base.html index bc5d882..e4c5edd 100644 --- a/templates/base.html +++ b/templates/base.html @@ -57,11 +57,87 @@ {% endblock %} + {% block footer %}
{% block footer_content %} {% endblock footer_content %}
{% endblock footer %} + diff --git a/templates/pagination.html b/templates/pagination.html index 9c702f6..61e2144 100644 --- a/templates/pagination.html +++ b/templates/pagination.html @@ -1,48 +1,4 @@ -{% with page_count=(item_count/max_items)|round(method="ceil")|int %} -{% if page_count > 1 %} -{% endif %} -{% endwith %} diff --git a/tv_movies/templates/tv_movies/episodeViewer.html b/tv_movies/templates/tv_movies/episodeViewer.html index 3cb56ac..5f90119 100644 --- a/tv_movies/templates/tv_movies/episodeViewer.html +++ b/tv_movies/templates/tv_movies/episodeViewer.html @@ -51,12 +51,12 @@ {% endwith %} -
+
{% for episode in episodes %} {% if episode.season == season_num %} -