Matthew Welch
308b1e1791
The project structure has been changed to make it more manageable and modular. A search feature has been added to the comics page.
125 lines
4.4 KiB
Python
125 lines
4.4 KiB
Python
from flask import Blueprint, render_template, request, make_response
|
|
from flask_login import login_required
|
|
|
|
from urllib import parse
|
|
from io import BytesIO
|
|
from wand.image import Image
|
|
import os, pytz, datetime
|
|
|
|
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:
|
|
return render_template("comics/index.html", title="Comics", publishers=database.get_publishers())
|
|
except Exception as e:
|
|
print(e)
|
|
return str(e)
|
|
|
|
|
|
@Comics.route("/comics/search")
|
|
@login_required
|
|
def search():
|
|
try:
|
|
query = request.args.get("q")
|
|
results = {
|
|
"publisher": [],
|
|
"series": [],
|
|
"comics": []
|
|
}
|
|
if query:
|
|
results = database.db_search_comics(query)
|
|
return render_template("comics/search.html", title="Comics: Search",
|
|
publishers=results["publisher"], publisher_series=results["series"], comics=results["comics"])
|
|
except Exception as e:
|
|
print(type(e), e)
|
|
return str(type(e))+" "+str(e)
|
|
|
|
|
|
@Comics.route("/comics/<publisher>")
|
|
@login_required
|
|
def comics_publisher(publisher):
|
|
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")
|
|
if series:
|
|
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=database.db_get_comics_in_series(series, publisher, series_year))
|
|
return render_template("comics/publisherSeriesView.html", title="Comics",
|
|
publisher_series=database.db_get_series_by_publisher(publisher))
|
|
|
|
|
|
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:
|
|
print(e)
|
|
return str(e)
|
|
|
|
|
|
def comic_gallery(publisher, series, series_year, issue):
|
|
try:
|
|
meta = database.db_get_comic(publisher, series, series_year, issue)
|
|
return render_template("comics/comicGallery.html", title="Comics", comic=meta)
|
|
except Exception as e:
|
|
print(e)
|
|
return str(e)
|
|
|
|
|
|
@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)
|
|
comic = func.open_comic(meta["path"])
|
|
byteImage = BytesIO(comic.getPage(page_number))
|
|
image = Image(file=byteImage)
|
|
response = make_response(image.make_blob())
|
|
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
|
|
|
|
|
|
@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)
|
|
response = make_response(thumb["image"])
|
|
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"] = thumb["type"]
|
|
return response
|