2019-08-23 21:34:42 -07:00
|
|
|
from flask import Blueprint, render_template, request, make_response, current_app
|
2019-07-23 15:49:57 -07:00
|
|
|
from flask_login import login_required
|
|
|
|
|
|
|
|
from urllib import parse
|
|
|
|
from io import BytesIO
|
|
|
|
from wand.image import Image
|
|
|
|
import os, pytz, datetime
|
2019-08-22 10:43:14 -07:00
|
|
|
import inspect
|
2019-07-23 15:49:57 -07:00
|
|
|
|
|
|
|
from scripts import database, func
|
|
|
|
|
|
|
|
Comics = Blueprint("comics", __name__, template_folder="templates")
|
|
|
|
|
|
|
|
MOBILE_DEVICES = ["android", "blackberry", "ipad", "iphone"]
|
|
|
|
|
|
|
|
|
|
|
|
@Comics.route("/comics")
|
|
|
|
@login_required
|
|
|
|
def index():
|
|
|
|
try:
|
2019-07-25 00:02:54 -07:00
|
|
|
page = request.args.get("page", 1, type=int)
|
|
|
|
max_items = request.args.get("max_items", 30, type=int)
|
|
|
|
publishers = database.get_publishers()
|
|
|
|
start = (max_items*(page-1))
|
|
|
|
end = len(publishers) if len(publishers) < max_items*page else max_items*page
|
|
|
|
return render_template("comics/index.html", title="Comics", publishers=publishers, page=page, max_items=max_items, start=start, end=end, item_count=len(publishers))
|
2019-07-23 15:49:57 -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-25 00:02:54 -07:00
|
|
|
return str(type(e)) + " " + str(e)
|
2019-07-23 15:49:57 -07:00
|
|
|
|
|
|
|
|
|
|
|
@Comics.route("/comics/search")
|
|
|
|
@login_required
|
|
|
|
def search():
|
|
|
|
try:
|
2019-07-25 00:02:54 -07:00
|
|
|
page = request.args.get("page", 1, type=int)
|
|
|
|
max_items = request.args.get("max_items", 30, type=int)
|
|
|
|
publisher_end = 0
|
|
|
|
series_end = 0
|
|
|
|
comics_end = 0
|
|
|
|
publisher_start = 0
|
|
|
|
series_start = 0
|
|
|
|
comics_start = 0
|
|
|
|
item_count = 0
|
2019-07-23 15:49:57 -07:00
|
|
|
query = request.args.get("q")
|
|
|
|
results = {
|
|
|
|
"publisher": [],
|
|
|
|
"series": [],
|
|
|
|
"comics": []
|
|
|
|
}
|
|
|
|
if query:
|
|
|
|
results = database.db_search_comics(query)
|
2019-07-25 00:02:54 -07:00
|
|
|
item_count = len(results["publisher"]) + len(results["series"]) + len(results["comics"])
|
|
|
|
for temp_page in range(1, page+1):
|
|
|
|
publisher_start = publisher_end
|
|
|
|
series_start = series_end
|
|
|
|
comics_start = comics_end
|
|
|
|
items = 0
|
|
|
|
publisher_end = len(results["publisher"]) if len(results["publisher"]) < max_items*temp_page else max_items*temp_page
|
|
|
|
items += publisher_end - publisher_start
|
|
|
|
if items < max_items:
|
|
|
|
series_end = len(results["series"]) if len(results["series"]) < (max_items*temp_page)-items else (max_items*temp_page)-items
|
|
|
|
items += series_end-series_start
|
|
|
|
if items < max_items:
|
|
|
|
comics_end = len(results["comics"]) if len(results["comics"]) < (max_items*temp_page)-series_end-publisher_end else (max_items*temp_page)-series_end-publisher_end
|
|
|
|
return render_template("comics/search.html", title="Comics: Search", publishers=results["publisher"], publisher_series=results["series"],
|
|
|
|
comics=results["comics"], page=page, max_items=max_items, item_count=item_count,
|
|
|
|
publisher_start=publisher_start, series_start=series_start, comics_start=comics_start,
|
|
|
|
publisher_end=publisher_end, series_end=series_end, comics_end=comics_end)
|
2019-07-23 15:49:57 -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-23 15:49:57 -07:00
|
|
|
return str(type(e))+" "+str(e)
|
|
|
|
|
|
|
|
|
|
|
|
@Comics.route("/comics/<publisher>")
|
|
|
|
@login_required
|
|
|
|
def comics_publisher(publisher):
|
2019-07-25 00:02:54 -07:00
|
|
|
try:
|
|
|
|
publisher = parse.unquote(publisher)
|
|
|
|
series = request.args.get("series")
|
|
|
|
series_year = request.args.get("seriesYear")
|
|
|
|
issue = request.args.get("issue")
|
|
|
|
page_number = request.args.get("pageNumber")
|
|
|
|
page = request.args.get("page", 1, type=int)
|
|
|
|
max_items = request.args.get("max_items", 30, type=int)
|
|
|
|
publisher_series = database.db_get_series_by_publisher(publisher)
|
|
|
|
start = (max_items*(page-1))
|
|
|
|
end = len(publisher_series) if len(publisher_series) < max_items*page else max_items*page
|
|
|
|
if series:
|
|
|
|
comics = database.db_get_comics_in_series(series, publisher, series_year)
|
|
|
|
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,
|
|
|
|
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,
|
|
|
|
start=start, end=end, page=page, max_items=max_items, item_count=len(publisher_series))
|
|
|
|
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-25 00:02:54 -07:00
|
|
|
return str(type(e)) + " " + str(e)
|
2019-07-23 15:49:57 -07:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2020-03-28 12:37:11 -07:00
|
|
|
page_count = int(meta.pagecount)
|
2019-07-23 15:49:57 -07:00
|
|
|
|
|
|
|
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)
|
2020-03-28 12:37:11 -07:00
|
|
|
title = "Comics: "+meta.series+": #"+meta.issuetext+" "+(meta.title or "")
|
2019-07-23 15:49:57 -07:00
|
|
|
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:
|
2019-08-23 21:34:42 -07:00
|
|
|
current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))
|
2019-07-25 00:02:54 -07:00
|
|
|
return str(type(e)) + " " + str(e)
|
2019-07-23 15:49:57 -07:00
|
|
|
|
|
|
|
|
|
|
|
def comic_gallery(publisher, series, series_year, issue):
|
|
|
|
try:
|
2019-07-25 00:02:54 -07:00
|
|
|
page = request.args.get("page", 1, type=int)
|
|
|
|
max_items = request.args.get("max_items", 30, type=int)
|
2019-07-23 15:49:57 -07:00
|
|
|
meta = database.db_get_comic(publisher, series, series_year, issue)
|
2019-07-25 00:02:54 -07:00
|
|
|
start = (max_items*(page-1))
|
2020-03-28 12:37:11 -07:00
|
|
|
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)
|
2019-07-23 15:49:57 -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-25 00:02:54 -07:00
|
|
|
return str(type(e)) + " " + str(e)
|
2019-07-23 15:49:57 -07:00
|
|
|
|
|
|
|
|
|
|
|
@Comics.route("/comics/get_comic/<int:comic_id>/<int:page_number>")
|
|
|
|
@login_required
|
|
|
|
def get_comic_page(comic_id, page_number):
|
|
|
|
meta = database.db_get_comic_by_id(comic_id)
|
2020-03-19 19:25:44 -07:00
|
|
|
comic = func.open_comic(meta.path)
|
2019-07-30 15:19:03 -07:00
|
|
|
byte_image = BytesIO(comic.getPage(page_number))
|
|
|
|
image = Image(file=byte_image)
|
2019-07-23 15:49:57 -07:00
|
|
|
response = make_response(image.make_blob())
|
|
|
|
response.headers["cache-control"] = "public"
|
2020-03-19 19:25:44 -07:00
|
|
|
date = pytz.utc.localize(datetime.datetime.utcfromtimestamp(os.path.getmtime(meta.path)))
|
2019-07-23 15:49:57 -07:00
|
|
|
response.headers["last-modified"] = date.strftime('%a, %d %b %Y %H:%M:%S %Z')
|
|
|
|
response.headers["content-type"] = "image/" + image.format
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@Comics.route("/comics/get_comic/<int:comic_id>/<int:page_number>/thumbnail")
|
|
|
|
@login_required
|
|
|
|
def get_comic_thumbnail(comic_id, page_number):
|
|
|
|
meta = database.db_get_comic_by_id(comic_id)
|
|
|
|
thumb = database.db_get_thumbnail_by_id_page(comic_id, page_number)
|
2020-03-19 19:25:44 -07:00
|
|
|
response = make_response(thumb.image)
|
2019-07-23 15:49:57 -07:00
|
|
|
response.headers["cache-control"] = "public"
|
2020-03-19 19:25:44 -07:00
|
|
|
date = pytz.utc.localize(datetime.datetime.utcfromtimestamp(os.path.getmtime(meta.path)))
|
2019-07-23 15:49:57 -07:00
|
|
|
response.headers["last-modified"] = date.strftime('%a, %d %b %Y %H:%M:%S %Z')
|
2020-03-19 19:25:44 -07:00
|
|
|
response.headers["content-type"] = thumb.type
|
2019-07-23 15:49:57 -07:00
|
|
|
return response
|