early version of OCR viewer now integrated

This commit is contained in:
nathan
2026-05-13 10:03:38 -04:00
parent 77323821cb
commit c663627594
17 changed files with 5734 additions and 1517 deletions

View File

@@ -12,6 +12,7 @@ import socket
import os
import sys
import json
revfrom urllib.parse import urlparse, parse_qs
PORT = 5044
ROOT = os.path.dirname(os.path.abspath(__file__))
@@ -32,7 +33,17 @@ class QuietHandler(http.server.SimpleHTTPRequestHandler):
super().end_headers()
def do_POST(self):
"""Handle POST /save-flags and POST /save-corrections."""
"""Handle POST /save-flags, /save-pass2-flags, /save-corrections, /clear-all-data.
All endpoints accept an optional ?diary=ID query param to use per-diary files.
"""
parsed = urlparse(self.path)
qs = parse_qs(parsed.query)
diary = qs.get('diary', ['unknown'])[0]
# Sanitize: only allow alphanumeric, hyphens, underscores
diary = ''.join(c for c in diary if c.isalnum() or c in '-_')
if not diary:
diary = 'unknown'
length = int(self.headers.get('Content-Length', 0))
body = self.rfile.read(length)
try:
@@ -41,8 +52,8 @@ class QuietHandler(http.server.SimpleHTTPRequestHandler):
self.send_response(400); self.end_headers()
return
if self.path == '/save-flags':
dest = os.path.join(ROOT, 'flags.json')
if parsed.path == '/save-flags':
dest = os.path.join(ROOT, 'flags-' + diary + '.json')
with open(dest, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2)
self.send_response(200)
@@ -50,9 +61,8 @@ class QuietHandler(http.server.SimpleHTTPRequestHandler):
self.end_headers()
self.wfile.write(b'{"ok":true}')
elif self.path == '/save-corrections':
dest = os.path.join(ROOT, 'corrections.json')
# Load existing, update single page entry, write back
elif parsed.path == '/save-corrections':
dest = os.path.join(ROOT, 'corrections-' + diary + '.json')
existing = {}
if os.path.exists(dest):
try:
@@ -60,10 +70,10 @@ class QuietHandler(http.server.SimpleHTTPRequestHandler):
existing = json.load(f)
except Exception:
pass
page = str(data.get('page', ''))
text = data.get('text', '')
if text.strip():
existing[page] = text
page = str(data.get('page', ''))
correction = data.get('correction')
if correction:
existing[page] = correction
else:
existing.pop(page, None)
with open(dest, 'w', encoding='utf-8') as f:
@@ -73,6 +83,32 @@ class QuietHandler(http.server.SimpleHTTPRequestHandler):
self.end_headers()
self.wfile.write(b'{"ok":true}')
elif parsed.path == '/save-pass2-flags':
dest = os.path.join(ROOT, 'pass2_flags-' + diary + '.json')
try:
with open(dest, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2)
except Exception:
self.send_response(500); self.end_headers()
return
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(b'{"ok":true}')
elif parsed.path == '/clear-all-data':
for prefix in ['flags-', 'corrections-', 'pass2_flags-']:
fpath = os.path.join(ROOT, prefix + diary + '.json')
if os.path.exists(fpath):
try:
os.remove(fpath)
except Exception:
pass
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(b'{"ok":true}')
else:
self.send_response(404); self.end_headers()
@@ -88,7 +124,7 @@ def start_server():
if __name__ == '__main__':
url = 'http://localhost:{}/p44-ocr-viewer.html'.format(PORT)
url = 'http://localhost:{}/p44-landing.html'.format(PORT)
if port_in_use(PORT):
print('\n P44 OCR Viewer (server already running)')