This repository has been archived on 2020-11-22. You can view files and clone it, but cannot push or open issues or pull requests.
mangareader/local_reader.py

51 lines
1.4 KiB
Python
Raw Normal View History

2019-12-07 00:08:27 +01:00
#!/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': sys.argv[1]
2019-12-07 00:08:27 +01:00
})
@app.route('/api/volume/0/page/<int:page_number>')
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)