Started working on #15. Added status page for Generate

This commit is contained in:
Frederik Baerentsen 2024-02-03 18:09:31 -05:00
parent ace4e9bcba
commit 41382cf1cd
3 changed files with 113 additions and 4 deletions

View File

@ -1,6 +1,7 @@
import os import os
from werkzeug.security import generate_password_hash from werkzeug.security import generate_password_hash
from sys import platform from sys import platform
import sys
CONTENT_BASE_DIR = os.getenv("CONTENT_BASE_DIR", "/library") #docker CONTENT_BASE_DIR = os.getenv("CONTENT_BASE_DIR", "/library") #docker
@ -32,7 +33,7 @@ MAXSIZE = (500,500)
def _print(arg): def _print(arg):
if DEBUG: if DEBUG:
print(arg) print(arg,file=sys.stderr)
TEENYOPDS_ADMIN_PASSWORD = os.getenv("TEENYOPDS_ADMIN_PASSWORD", None) TEENYOPDS_ADMIN_PASSWORD = os.getenv("TEENYOPDS_ADMIN_PASSWORD", None)
users = {} users = {}

39
main.py
View File

@ -13,14 +13,18 @@ import re
import datetime import datetime
import sys import sys
import time import time
import json
import numpy as np import numpy as np
from pathlib import Path from pathlib import Path
from io import BytesIO from io import BytesIO
from threading import Thread
# for debugging # for debugging
from pprint import pprint from pprint import pprint
#### ####
generated = None
from opds import fromdir from opds import fromdir
import config,extras import config,extras
@ -57,7 +61,8 @@ def startpage():
return redirect(url_for('import2sql')) return redirect(url_for('import2sql'))
elif request.form.get('Generate') == 'Generate': elif request.form.get('Generate') == 'Generate':
config._print("Generate Covers from Start page") config._print("Generate Covers from Start page")
return redirect(url_for('generate'))
return redirect(url_for('generate2'))
else: else:
# pass # unknown # pass # unknown
return render_template("first.html") return render_template("first.html")
@ -136,19 +141,30 @@ def search():
config._print(e) config._print(e)
return str(result) return str(result)
@app.route("/generate") total = None
#@app.route("/generate")
def generate(): def generate():
force = request.args.get('force') config._print('GENERATES NOW!!!')
force = 'True' #request.args.get('force')
global generated
global total
total = 0
generated = 0 generated = 0
comiccount = 0 comiccount = 0
files_without_comicinfo = 0 files_without_comicinfo = 0
errorcount = 0 errorcount = 0
skippedcount = 0 skippedcount = 0
errormsg = "" errormsg = ""
for root, dirs, files in os.walk(os.path.abspath(config.CONTENT_BASE_DIR)):
for file in files:
f = os.path.join(root,file)
if f.endswith('.cbz'):
total = total + 1
for root, dirs, files in os.walk(os.path.abspath(config.CONTENT_BASE_DIR)): for root, dirs, files in os.walk(os.path.abspath(config.CONTENT_BASE_DIR)):
for file in files: for file in files:
f = os.path.join(root, file) f = os.path.join(root, file)
if f.endswith('.cbz'): if f.endswith('.cbz'):
config._print(generated)
try: try:
comiccount = comiccount + 1 comiccount = comiccount + 1
s = zipfile.ZipFile(f) s = zipfile.ZipFile(f)
@ -199,8 +215,25 @@ def generate():
config._print(f) config._print(f)
errormsg = str(e) errormsg = str(e)
return "Forced generation: " + str(force) + "<br>Comics: " + str(comiccount) + "<br>Generated: " + str(generated) + "<br>CBZ files without ComicInfo.xml: " + str(files_without_comicinfo) + "<br>Errors: " + str(errorcount) + "<br>Skipped: " + str(skippedcount) + "<br>" + errormsg return "Forced generation: " + str(force) + "<br>Comics: " + str(comiccount) + "<br>Generated: " + str(generated) + "<br>CBZ files without ComicInfo.xml: " + str(files_without_comicinfo) + "<br>Errors: " + str(errorcount) + "<br>Skipped: " + str(skippedcount) + "<br>" + errormsg
config._print( "Forced generation: " + str(force) + "<br>Comics: " + str(comiccount) + "<br>Generated: " + str(generated) + "<br>CBZ files without ComicInfo.xml: " + str(files_without_comicinfo) + "<br>Errors: " + str(errorcount) + "<br>Skipped: " + str(skippedcount) + "<br>" + errormsg)
@app.route("/generate2")
def generate2():
t1 = Thread(target=generate)
t1.start()
return render_template('status.html')
@app.route("/t2")
def index():
t1 = Thread(target=generate)
t1.start()
return render_template('status.html')
@app.route('/status',methods=['GET'])
def getStatus():
statusList = {'status':generated,'total':total}
return json.dumps(statusList)
@app.route('/import') @app.route('/import')
def import2sql(): def import2sql():

75
templates/status.html Normal file
View File

@ -0,0 +1,75 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<style>
body {
background-color: #D64F2A;
}
.progress {
display: flex;
position: absolute;
height: 100%;
width: 100%;
}
.status {
color: white;
margin: auto;
}
.status h2 {
padding: 50px;
font-size: 80px;
font-weight: bold;
}
</style>
<title>Status Update</title>
</head>
<body>
<div class="progress">
<div class="status">
<h2 id="innerStatus">Loading...</h2>
</div>
</div>
</body>
<script>
var timeout;
async function getStatus() {
let get;
try {
const res = await fetch("/status");
get = await res.json();
} catch (e) {
console.error("Error: ", e);
}
document.getElementById("innerStatus").innerHTML = Math.round(get.status / get.total * 100,0) + "&percnt;";
if (get.status == get.total){
document.getElementById("innerStatus").innerHTML += " Done.";
clearTimeout(timeout);
// Simulate a mouse click:
window.location.href = "/";
return false;
}
timeout = setTimeout(getStatus, 1000);
}
getStatus();
</script>
</html>