#!/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)