From 137ae1b71264411810274033a04ca749dcfc19ba Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Fri, 6 Dec 2019 23:08:27 +0000 Subject: [PATCH] Add script to serve single archive --- .gitignore | 1 + local_reader.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100755 local_reader.py diff --git a/.gitignore b/.gitignore index f8260f9..5c8f09b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ cache +__pycache__ node_modules /frontend/dist diff --git a/local_reader.py b/local_reader.py new file mode 100755 index 0000000..7549587 --- /dev/null +++ b/local_reader.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +from flask import Flask, jsonify, render_template, Response +from mimetypes import types_map as mimetypes +from zipfile import ZipFile +import logging +import os +import sys +import webbrowser +import werkzeug.exceptions as exceptions + +# disable access log +log = logging.getLogger('werkzeug') +log.setLevel(logging.ERROR) +# disable flask startup message +os.environ['WERKZEUG_RUN_MAIN'] = 'true' + +mimetypes['.webp'] = 'image/webp' +archive = ZipFile(sys.argv[1], 'r') +app = Flask(__name__, static_folder='frontend/dist/static', template_folder='frontend/dist') + +# kind of redundant, but avoids returning 200 if page does not exist +@app.route('/') +@app.route('/series/0') +@app.route('/volume/0') +def serve_app(**kwargs): + return render_template('index.html') + + +@app.route('/api/volume/0') +def get_volume_info(): + return jsonify({ + 'pages': len(archive.filelist), + 'series': 0, + 'title': 'local' + }) + + +@app.route('/api/volume/0/page/') +def get_volume_page(page_number): + try: + page = archive.filelist[page_number - 1] + except IndexError: + raise exceptions.NotFound() from None + mimetype = mimetypes[os.path.splitext(page.filename)[1]] + return Response(archive.read(page.filename), mimetype=mimetype) + + +if __name__ == '__main__': + webbrowser.open('http://localhost:5051/volume/0') + app.run(host='127.0.0.1', port=5051)