diff --git a/app.py b/app.py index 40a11ec..552e758 100644 --- a/app.py +++ b/app.py @@ -1,9 +1,9 @@ from flask import Flask -from flask import render_template -from flask import request -from flask import g -import threading +from flask import render_template, request, g, redirect, url_for from urllib import parse +import threading +import base64 + import scripts.func as func from scripts import database @@ -39,7 +39,7 @@ def close_connection(exception): def update_comic_db(sender, **kw): try: - database.add_comics(kw["meta"]) + database.add_comics(kw["meta"], kw["thumbnails"]) except Exception as e: print(e) @@ -76,11 +76,17 @@ def comics(): return render_template("publisherList.html", comics=database.get_publishers()) except Exception as e: print(e) - return "Sucks to be you" + return e try: return render_template("publisherView.html", title="Comics", comics=database.get_publishers()) except Exception as e: print(e) + return e + + +@app.route("/comics/") +def comic_reroute(): + return redirect(url_for("comics")) @app.route("/comics/") @@ -88,10 +94,54 @@ def comics_publisher(publisher): publisher = parse.unquote(publisher) series = request.args.get("series") series_year = request.args.get("seriesYear") - comic = request.args.get("comic") + comic_path = request.args.get("path") if series: - return render_template("comicView.html", title="Comics", comics=database.db_get_comics_in_series(series, publisher, series_year)) - return render_template("seriesView.html", title="Comics", comics=database.db_get_series_by_publisher(publisher)) + if comic_path: + comic_path = parse.unquote(comic_path) + return comic_viewer(comic_path, publisher, series, series_year) + return render_template("seriesView.html", title="Comics", publisher=publisher, seriesYear=series_year, comics=database.db_get_comics_in_series(series, publisher, series_year)) + return render_template("publisherSeriesView.html", title="Comics", comics=database.db_get_series_by_publisher(publisher)) + + +def comic_viewer(comic_path, publisher, series, series_year): + try: + comic_path_parsed = parse.quote(comic_path) + publisher_parsed = parse.quote(publisher) + series_parsed = parse.quote(series) + page_number = int(request.args.get("pageNumber")) + comic = func.open_comic(comic_path) + page_count = comic.getNumberOfPages() + + 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 + + page = comic.getPage(page_number) + page = str(base64.b64encode(page))[2:-1] + prev_url = "/comics/{}?series={}&seriesYear={}&path={}&pageNumber={}".format(publisher_parsed, series_parsed, series_year, comic_path_parsed, prev_page) + next_url = "/comics/{}?series={}&seriesYear={}&path={}&pageNumber={}".format(publisher_parsed, series_parsed, series_year, comic_path_parsed, next_page) + return render_template("comicView.html", title="Comics", page=page, page_count=page_count, prev_url=prev_url, next_url=next_url) + except Exception as e: + print(e) + return e + + +@app.route("/comics/getPage") +def get_comic_page(): + path = parse.unquote(request.args.get("path")) + page_number = int(request.args.get("pageNumber")) + comic = func.open_comic(path) + if page_number > comic.getNumberOfPages()-1: + page_number = 1 + if page_number <= 0: + page_number = comic.getNumberOfPages()-1 + page = comic.getPage(page_number) + + page = base64.b64encode(page) + return page if __name__ == '__main__': diff --git a/scripts/database.py b/scripts/database.py index 73b2232..54abb08 100644 --- a/scripts/database.py +++ b/scripts/database.py @@ -60,6 +60,11 @@ def initialize_db(): "role" TEXT, "name" TEXT, "primary" INTEGER NOT NULL +)""") + get_db().execute("""CREATE TABLE IF NOT EXISTS "comic_thumbnails" ( + "id" INTEGER, + "pageNumber" INTEGER, + "image" BLOB )""") get_db().execute("CREATE INDEX IF NOT EXISTS path_index ON comics(path);") get_db().commit() @@ -73,7 +78,7 @@ def table_exists(table_name): return True -def add_comics(meta): +def add_comics(meta, thumbnails): data = [] for info in meta: issue = IssueString(info[1].issue).asFloat() @@ -84,14 +89,32 @@ def add_comics(meta): info[1].notes, info[1].webLink, info[1].format, info[1].manga, info[1].blackAndWhite, info[1].pageCount, info[1].maturityRating, info[1].storyArc, info[1].seriesGroup, info[1].scanInfo, info[1].characters, info[1].teams, info[1].locations)) - get_db().executemany( - """INSERT into comics(path, tagOrigin, series, issue, issueText, title, publisher, + for comic, images in zip(data, thumbnails): + get_db().execute("""INSERT INTO comics(path, tagOrigin, series, issue, issueText, title, publisher, month, year, day, seriesYear, issueCount, volume, genre, language, comments, volumeCount, criticalRating, country, alternateSeries, alternateNumber, alternateCount, imprint, notes, webLink, format, manga, blackAndWhite, pageCount, maturityRating, storyArc, seriesGroup, scanInfo, characters, teams, locations) - VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""", - data) + VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""", comic) + get_db().commit() + comic_id = get_db().execute("SELECT id from comics ORDER BY id DESC LIMIT 1").fetchone()[0] + for index in range(len(images)): + get_db().execute("INSERT INTO comic_thumbnails(id, pageNumber, image) VALUES (?,?,?)", (comic_id, index, images[index])) + get_db().commit() + + +def add_comic_thumbnails(thumbnails): + comic_id = 1 + row = get_db().execute("SELECT id from comic_thumbnails ORDER BY id DESC LIMIT 1").fetchone() + if row: + comic_id = row[0]+1 + print("start comics added:", len(thumbnails)) + for images in thumbnails: + print("pages in comic:", len(images)) + for index in range(len(images)): + print("comic page added:", index) + get_db().execute("INSERT INTO comic_thumbnails(id, pageNumber, image) VALUES (?,?,?)", (comic_id, index, images[index])) + comic_id += 1 get_db().commit() @@ -120,7 +143,7 @@ def db_get_series_by_publisher(publisher): def db_get_comics_in_series(series, publisher, series_year): comics = [] - rows = get_db().execute("SELECT series, issue, title FROM comics WHERE series LIKE ? AND publisher LIKE ? AND seriesYear LIKE ? ORDER BY series, issue", [series, publisher, series_year]).fetchall() + rows = get_db().execute("SELECT series, issue, title, path FROM comics WHERE series LIKE ? AND publisher LIKE ? AND seriesYear LIKE ? ORDER BY series, issue", [series, publisher, series_year]).fetchall() get_db().commit() for row in rows: comics.append(row) diff --git a/scripts/func.py b/scripts/func.py index fe4fea1..7e1f49f 100644 --- a/scripts/func.py +++ b/scripts/func.py @@ -1,5 +1,8 @@ from comicapi import comicarchive from blinker import Namespace +from io import BytesIO +from PIL import Image + import os, sys from scripts import database @@ -20,21 +23,58 @@ MUSIC_DIRECTORY = "/usb/storage/media/Music/" def get_comics(): + total_comics = 0 + comics_in_db = 0 + comics_added = 0 meta = [] + thumbnails = [] i = 0 for root, dirs, files in os.walk(COMICS_DIRECTORY): for f in files: if f.endswith(".cbr"): + total_comics += 1 path = os.path.join(root, f) if not database.comic_path_in_db(path): - archive = comicarchive.ComicArchive(path, default_image_path="/usb/www/matthew/rpiWebApp/static/icon.png") + try: + test_path = path.encode("utf8") + except Exception as e: + print("encoding failed on:", path) + print(e) + continue + print(path) + archive = open_comic(path) + thumbnails.append(get_comic_thumbnails(archive)) meta.append((path, archive.readCIX())) + comics_added += 1 + comics_in_db += 1 i += 1 - if i > 20: - comic_loaded.send("anonymous", meta=meta.copy()) + if i >= 20: + comic_loaded.send("anonymous", meta=meta.copy(), thumbnails=thumbnails.copy()) meta.clear() + thumbnails.clear() i = 0 - comic_loaded.send("anonymous", meta=meta) + else: + comics_in_db += 1 + print("total number of comics:", total_comics) + print("comics in database:", comics_in_db) + print("number of comics added:", comics_added) + comic_loaded.send("anonymous", meta=meta, thumbnails=thumbnails) + + +def get_comic_thumbnails(comic): + thumbnails = [] + size = 128, 128 + for page in range(comic.getNumberOfPages()): + image_bytes = BytesIO(comic.getPage(page)) + image = Image.open(image_bytes) + image.thumbnail(size) + thumbnails.append(image.tobytes()) + return thumbnails + + +def open_comic(path): + archive = comicarchive.ComicArchive(path, default_image_path="/usb/www/matthew/rpiWebApp/static/icon.png") + return archive def bytestring_path(path): diff --git a/templates/PublisherSeriesList.html b/templates/PublisherSeriesList.html new file mode 100644 index 0000000..d95d7ef --- /dev/null +++ b/templates/PublisherSeriesList.html @@ -0,0 +1,3 @@ +{% for comic in comics %} +
  • {{ comic[0] }} {{ comic[1] }}
  • +{% endfor %} diff --git a/templates/base.html b/templates/base.html index 6727e58..7268bbf 100644 --- a/templates/base.html +++ b/templates/base.html @@ -3,7 +3,7 @@ {{ title }} - + {% block head %} {% endblock %} diff --git a/templates/comicList.html b/templates/comicList.html deleted file mode 100644 index cd833fc..0000000 --- a/templates/comicList.html +++ /dev/null @@ -1,5 +0,0 @@ -{% for comic in comics %} -
  • - {{ comic[0] }} {% if comic[1] > 0 %}{{ "#{0:g}".format(comic[1]) }}{% endif %} {% if comic[2] != None %}{{ comic[2] }} {% endif %} -
  • -{% endfor %} diff --git a/templates/comicView.html b/templates/comicView.html index 0840532..34956e4 100644 --- a/templates/comicView.html +++ b/templates/comicView.html @@ -1,7 +1,9 @@ {% extends "base.html" %} {% block content %} - + + + + + {% endblock %} diff --git a/templates/publisherSeriesView.html b/templates/publisherSeriesView.html new file mode 100644 index 0000000..eed77d3 --- /dev/null +++ b/templates/publisherSeriesView.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} + +{% block content %} + +{% endblock %} diff --git a/templates/publisherView.html b/templates/publisherView.html index 8599678..c87454a 100644 --- a/templates/publisherView.html +++ b/templates/publisherView.html @@ -1,26 +1,6 @@ {% extends "base.html" %} {% block content %} - - - - - - diff --git a/templates/seriesList.html b/templates/seriesList.html index d95d7ef..02d6d89 100644 --- a/templates/seriesList.html +++ b/templates/seriesList.html @@ -1,3 +1,7 @@ {% for comic in comics %} -
  • {{ comic[0] }} {{ comic[1] }}
  • +
  • + + {{ comic[0] }} {% if comic[1] > 0 %}{{ "#{0:g}".format(comic[1]) }}{% endif %} {% if comic[2] != None %}{{ comic[2] }} {% endif %} + +
  • {% endfor %} diff --git a/templates/seriesView.html b/templates/seriesView.html index 31e5e1f..4472f8b 100644 --- a/templates/seriesView.html +++ b/templates/seriesView.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% block content %} -