Add chunked TTS audio generation and ranking search autocomplete on home page
- Split chapter TTS into up to 10 paragraph-aligned parts; part 0 is
generated synchronously so playback starts immediately, parts 1-9 and
the final merge happen in a background goroutine
- New routes: GET /ui/audio/{slug}/{n}/status (merge poll) and
GET /ui/audio-file/{slug}/{n}/part/{p} (serve individual part)
- JS polls status every 3 s and seamlessly swaps audio.src to the merged
file once ready, preserving playback position proportionally
- Home page scrape form replaced with a ranking-search autocomplete:
type a title/author to see matching ranking items (cover + metadata),
click or keyboard-select to inject the source URL, or paste a raw URL
directly; ranking data is embedded as JSON at page render time
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -89,7 +90,9 @@ func (s *Server) ListenAndServe(ctx context.Context) error {
|
||||
`{"error":"audio generation timed out"}`,
|
||||
)
|
||||
mux.Handle("POST /ui/audio/{slug}/{n}", audioGenHandler)
|
||||
mux.HandleFunc("GET /ui/audio/{slug}/{n}/status", s.handleAudioStatus)
|
||||
mux.HandleFunc("GET /ui/audio-file/{slug}/{n}", s.handleAudioFile)
|
||||
mux.HandleFunc("GET /ui/audio-file/{slug}/{n}/part/{p}", s.handleAudioFilePart)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: s.addr,
|
||||
@@ -138,15 +141,25 @@ func (s *Server) handleChapterText(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprint(w, stripMarkdown(raw))
|
||||
}
|
||||
|
||||
// handleAudioGenerate handles POST /ui/audio/{slug}/{n}.
|
||||
// It accepts an optional JSON body {voice, speed} (falling back to server
|
||||
// defaults). If the MP3 is already cached on disk it returns immediately;
|
||||
// otherwise it calls Kokoro-FastAPI to generate and save the file.
|
||||
// Response: JSON {"url": "/ui/audio-file/{slug}/{n}?voice=…&speed=…"}
|
||||
// ─── Chunked audio generation ────────────────────────────────────────────────
|
||||
//
|
||||
// Concurrent requests for the same (slug, n, voice, speed) are deduplicated:
|
||||
// the first caller does the work; subsequent callers block until it finishes
|
||||
// and then serve the cached file (or receive the same error).
|
||||
// handleAudioGenerate handles POST /ui/audio/{slug}/{n}.
|
||||
//
|
||||
// Flow:
|
||||
// 1. If the merged MP3 already exists on disk → return it immediately.
|
||||
// 2. Otherwise split the chapter text into up to audioParts equal-ish chunks
|
||||
// (by paragraph), generate part 0 synchronously (so the browser can start
|
||||
// playing right away), then launch a background goroutine that generates
|
||||
// parts 1…N and merges them into the final file.
|
||||
// 3. Response: {"url":"<part-0-url>","parts":N,"merged":false}
|
||||
// (or {"url":"<final-url>","parts":1,"merged":true} on a cache hit).
|
||||
//
|
||||
// Deduplication: if another request is already generating the *merged* file
|
||||
// for the same (slug,n,voice,speed) key, the new request blocks until it
|
||||
// finishes and then serves the cached result.
|
||||
const audioParts = 10
|
||||
|
||||
// handleAudioGenerate handles POST /ui/audio/{slug}/{n}.
|
||||
func (s *Server) handleAudioGenerate(w http.ResponseWriter, r *http.Request) {
|
||||
slug := r.PathValue("slug")
|
||||
n, err := strconv.Atoi(r.PathValue("n"))
|
||||
@@ -174,20 +187,17 @@ func (s *Server) handleAudioGenerate(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
audioPath := s.writer.AudioPath(slug, n, voice, speed)
|
||||
|
||||
// Idempotent: return immediately if already cached.
|
||||
// Fast path: merged file already on disk.
|
||||
if _, err := os.Stat(audioPath); err == nil {
|
||||
s.writeAudioURL(w, slug, n, voice, speed)
|
||||
s.writeAudioResponse(w, slug, n, voice, speed, 1, true)
|
||||
return
|
||||
}
|
||||
|
||||
// Deduplicate concurrent generation requests for the same file.
|
||||
// If another goroutine is already generating this file, wait for it and
|
||||
// then serve the (now-cached) result.
|
||||
// Deduplicate concurrent generation requests for the same merged file.
|
||||
cacheKey := fmt.Sprintf("%s/%d/%s/%.2f", slug, n, voice, speed)
|
||||
|
||||
s.audioMu.Lock()
|
||||
if ch, ok := s.audioInFlight[cacheKey]; ok {
|
||||
// Someone else is already generating — wait for them.
|
||||
s.audioMu.Unlock()
|
||||
select {
|
||||
case <-ch:
|
||||
@@ -195,20 +205,17 @@ func (s *Server) handleAudioGenerate(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, `{"error":"request cancelled"}`, http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
// Serve the cached file (or 404 if generation failed).
|
||||
if _, err := os.Stat(audioPath); err == nil {
|
||||
s.writeAudioURL(w, slug, n, voice, speed)
|
||||
s.writeAudioResponse(w, slug, n, voice, speed, 1, true)
|
||||
} else {
|
||||
http.Error(w, `{"error":"audio generation failed"}`, http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
// Register ourselves as the in-flight generator.
|
||||
ch := make(chan struct{})
|
||||
s.audioInFlight[cacheKey] = ch
|
||||
s.audioMu.Unlock()
|
||||
|
||||
// Always close the channel (unblocking waiters) and remove our entry.
|
||||
defer func() {
|
||||
s.audioMu.Lock()
|
||||
delete(s.audioInFlight, cacheKey)
|
||||
@@ -216,7 +223,7 @@ func (s *Server) handleAudioGenerate(w http.ResponseWriter, r *http.Request) {
|
||||
close(ch)
|
||||
}()
|
||||
|
||||
// Load chapter text.
|
||||
// Load and validate chapter text.
|
||||
raw, err := s.writer.ReadChapter(slug, n)
|
||||
if err != nil {
|
||||
http.Error(w, `{"error":"chapter not found"}`, http.StatusNotFound)
|
||||
@@ -227,13 +234,105 @@ func (s *Server) handleAudioGenerate(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, `{"error":"chapter text is empty"}`, http.StatusUnprocessableEntity)
|
||||
return
|
||||
}
|
||||
|
||||
if s.kokoroURL == "" {
|
||||
http.Error(w, `{"error":"kokoro not configured"}`, http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
|
||||
// Call Kokoro-FastAPI.
|
||||
// Ensure audio dir exists.
|
||||
if err := os.MkdirAll(s.writer.AudioDir(slug), 0o755); err != nil {
|
||||
http.Error(w, `{"error":"failed to create audio dir"}`, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
parts := splitTextIntoParts(text, audioParts)
|
||||
totalParts := len(parts)
|
||||
|
||||
// Generate part 0 synchronously so the browser can start playing immediately.
|
||||
if err := s.generateAudioPart(r.Context(), slug, n, voice, speed, 0, parts[0]); err != nil {
|
||||
s.log.Error("part 0 generation failed", "err", err)
|
||||
http.Error(w, `{"error":"part 0 generation failed"}`, http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
|
||||
if totalParts == 1 {
|
||||
// Only one part — rename it to the final path directly.
|
||||
partPath := s.writer.AudioPartPath(slug, n, voice, speed, 0)
|
||||
if err := os.Rename(partPath, audioPath); err != nil {
|
||||
http.Error(w, `{"error":"failed to save audio"}`, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
s.log.Info("audio generated (single part)", "slug", slug, "chapter", n)
|
||||
s.writeAudioResponse(w, slug, n, voice, speed, 1, true)
|
||||
return
|
||||
}
|
||||
|
||||
// Return part-0 URL immediately; generate the rest in the background.
|
||||
s.writeAudioResponse(w, slug, n, voice, speed, totalParts, false)
|
||||
|
||||
// Background: generate parts 1…N then merge.
|
||||
go func() {
|
||||
bgCtx := context.Background()
|
||||
for p := 1; p < totalParts; p++ {
|
||||
if err := s.generateAudioPart(bgCtx, slug, n, voice, speed, p, parts[p]); err != nil {
|
||||
s.log.Error("background part generation failed", "part", p, "err", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := s.mergeAudioParts(slug, n, voice, speed, totalParts); err != nil {
|
||||
s.log.Error("audio merge failed", "slug", slug, "chapter", n, "err", err)
|
||||
return
|
||||
}
|
||||
s.log.Info("audio merged", "slug", slug, "chapter", n, "parts", totalParts)
|
||||
}()
|
||||
}
|
||||
|
||||
// splitTextIntoParts divides text (paragraphs separated by blank lines) into
|
||||
// at most n equal-ish chunks. Returns at least 1 element.
|
||||
func splitTextIntoParts(text string, n int) []string {
|
||||
// Split into paragraphs on blank lines.
|
||||
raw := strings.Split(text, "\n\n")
|
||||
var paras []string
|
||||
for _, p := range raw {
|
||||
p = strings.TrimSpace(p)
|
||||
if p != "" {
|
||||
paras = append(paras, p)
|
||||
}
|
||||
}
|
||||
if len(paras) == 0 {
|
||||
return []string{text}
|
||||
}
|
||||
if n > len(paras) {
|
||||
n = len(paras)
|
||||
}
|
||||
if n < 1 {
|
||||
n = 1
|
||||
}
|
||||
|
||||
chunks := make([]string, n)
|
||||
chunkSize := (len(paras) + n - 1) / n // ceiling division
|
||||
for i := 0; i < n; i++ {
|
||||
start := i * chunkSize
|
||||
end := start + chunkSize
|
||||
if start >= len(paras) {
|
||||
// Fewer paragraphs than requested parts: return what we have.
|
||||
chunks = chunks[:i]
|
||||
break
|
||||
}
|
||||
if end > len(paras) {
|
||||
end = len(paras)
|
||||
}
|
||||
chunks[i] = strings.Join(paras[start:end], "\n\n")
|
||||
}
|
||||
if len(chunks) == 0 {
|
||||
return []string{text}
|
||||
}
|
||||
return chunks
|
||||
}
|
||||
|
||||
// generateAudioPart calls Kokoro for a single text chunk and writes the result
|
||||
// atomically to AudioPartPath(…, part).
|
||||
func (s *Server) generateAudioPart(ctx context.Context, slug string, n int, voice string, speed float64, part int, text string) error {
|
||||
reqBody, _ := json.Marshal(map[string]interface{}{
|
||||
"model": "kokoro",
|
||||
"input": text,
|
||||
@@ -242,67 +341,140 @@ func (s *Server) handleAudioGenerate(w http.ResponseWriter, r *http.Request) {
|
||||
"speed": speed,
|
||||
"stream": false,
|
||||
})
|
||||
kokoroReq, err := http.NewRequestWithContext(r.Context(), http.MethodPost,
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost,
|
||||
s.kokoroURL+"/v1/audio/speech", bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
http.Error(w, `{"error":"failed to build kokoro request"}`, http.StatusInternalServerError)
|
||||
return
|
||||
return fmt.Errorf("build request: %w", err)
|
||||
}
|
||||
kokoroReq.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := http.DefaultClient.Do(kokoroReq)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
s.log.Error("kokoro request failed", "err", err)
|
||||
http.Error(w, `{"error":"kokoro unavailable"}`, http.StatusBadGateway)
|
||||
return
|
||||
return fmt.Errorf("kokoro request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body2, _ := io.ReadAll(resp.Body)
|
||||
s.log.Error("kokoro returned error", "status", resp.StatusCode, "body", string(body2))
|
||||
http.Error(w, fmt.Sprintf(`{"error":"kokoro error %d"}`, resp.StatusCode), http.StatusBadGateway)
|
||||
return
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return fmt.Errorf("kokoro status %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
// Ensure the audio directory exists.
|
||||
if err := os.MkdirAll(s.writer.AudioDir(slug), 0o755); err != nil {
|
||||
http.Error(w, `{"error":"failed to create audio dir"}`, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Write to a temp file then rename atomically.
|
||||
tmpPath := audioPath + ".tmp"
|
||||
partPath := s.writer.AudioPartPath(slug, n, voice, speed, part)
|
||||
tmpPath := partPath + ".tmp"
|
||||
f, err := os.Create(tmpPath)
|
||||
if err != nil {
|
||||
http.Error(w, `{"error":"failed to create temp file"}`, http.StatusInternalServerError)
|
||||
return
|
||||
return fmt.Errorf("create temp file: %w", err)
|
||||
}
|
||||
if _, err := io.Copy(f, resp.Body); err != nil {
|
||||
f.Close()
|
||||
os.Remove(tmpPath)
|
||||
http.Error(w, `{"error":"failed to write audio"}`, http.StatusInternalServerError)
|
||||
return
|
||||
return fmt.Errorf("write audio: %w", err)
|
||||
}
|
||||
f.Close()
|
||||
if err := os.Rename(tmpPath, audioPath); err != nil {
|
||||
if err := os.Rename(tmpPath, partPath); err != nil {
|
||||
os.Remove(tmpPath)
|
||||
http.Error(w, `{"error":"failed to save audio"}`, http.StatusInternalServerError)
|
||||
return
|
||||
return fmt.Errorf("rename temp file: %w", err)
|
||||
}
|
||||
|
||||
s.log.Info("audio generated", "slug", slug, "chapter", n, "voice", voice, "speed", speed)
|
||||
s.writeAudioURL(w, slug, n, voice, speed)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) writeAudioURL(w http.ResponseWriter, slug string, n int, voice string, speed float64) {
|
||||
url := fmt.Sprintf("/ui/audio-file/%s/%d?voice=%s&speed=%.1f", slug, n, voice, speed)
|
||||
// mergeAudioParts concatenates totalParts part files in order into AudioPath,
|
||||
// then removes the individual part files.
|
||||
func (s *Server) mergeAudioParts(slug string, n int, voice string, speed float64, totalParts int) error {
|
||||
audioPath := s.writer.AudioPath(slug, n, voice, speed)
|
||||
tmpPath := audioPath + ".tmp"
|
||||
|
||||
out, err := os.Create(tmpPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create merged temp: %w", err)
|
||||
}
|
||||
|
||||
for p := 0; p < totalParts; p++ {
|
||||
partPath := s.writer.AudioPartPath(slug, n, voice, speed, p)
|
||||
data, err := os.ReadFile(partPath)
|
||||
if err != nil {
|
||||
out.Close()
|
||||
os.Remove(tmpPath)
|
||||
return fmt.Errorf("read part %d: %w", p, err)
|
||||
}
|
||||
if _, err := out.Write(data); err != nil {
|
||||
out.Close()
|
||||
os.Remove(tmpPath)
|
||||
return fmt.Errorf("write merged part %d: %w", p, err)
|
||||
}
|
||||
}
|
||||
out.Close()
|
||||
|
||||
if err := os.Rename(tmpPath, audioPath); err != nil {
|
||||
os.Remove(tmpPath)
|
||||
return fmt.Errorf("rename merged: %w", err)
|
||||
}
|
||||
|
||||
// Clean up part files (best-effort).
|
||||
for p := 0; p < totalParts; p++ {
|
||||
os.Remove(s.writer.AudioPartPath(slug, n, voice, speed, p))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) writeAudioResponse(w http.ResponseWriter, slug string, n int, voice string, speed float64, parts int, merged bool) {
|
||||
var url string
|
||||
if merged {
|
||||
url = fmt.Sprintf("/ui/audio-file/%s/%d?voice=%s&speed=%.1f", slug, n, voice, speed)
|
||||
} else {
|
||||
url = fmt.Sprintf("/ui/audio-file/%s/%d/part/0?voice=%s&speed=%.1f", slug, n, voice, speed)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]string{"url": url})
|
||||
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"url": url,
|
||||
"parts": parts,
|
||||
"merged": merged,
|
||||
})
|
||||
}
|
||||
|
||||
// writeAudioURL is kept for backward compatibility (used by dedup waiters).
|
||||
func (s *Server) writeAudioURL(w http.ResponseWriter, slug string, n int, voice string, speed float64) {
|
||||
s.writeAudioResponse(w, slug, n, voice, speed, 1, true)
|
||||
}
|
||||
|
||||
// handleAudioStatus handles GET /ui/audio/{slug}/{n}/status.
|
||||
// Returns {"merged":true/false,"url":"..."} so the browser can poll for the
|
||||
// merged file after receiving a parts response from handleAudioGenerate.
|
||||
func (s *Server) handleAudioStatus(w http.ResponseWriter, r *http.Request) {
|
||||
slug := r.PathValue("slug")
|
||||
n, err := strconv.Atoi(r.PathValue("n"))
|
||||
if err != nil || n < 1 {
|
||||
http.Error(w, `{"error":"invalid chapter"}`, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
voice := r.URL.Query().Get("voice")
|
||||
if voice == "" {
|
||||
voice = s.kokoroVoice
|
||||
}
|
||||
speedStr := r.URL.Query().Get("speed")
|
||||
speed := 1.0
|
||||
if speedStr != "" {
|
||||
if v, err := strconv.ParseFloat(speedStr, 64); err == nil && v > 0 {
|
||||
speed = v
|
||||
}
|
||||
}
|
||||
|
||||
audioPath := s.writer.AudioPath(slug, n, voice, speed)
|
||||
merged := false
|
||||
if _, err := os.Stat(audioPath); err == nil {
|
||||
merged = true
|
||||
}
|
||||
|
||||
mergedURL := fmt.Sprintf("/ui/audio-file/%s/%d?voice=%s&speed=%.1f", slug, n, voice, speed)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"merged": merged,
|
||||
"url": mergedURL,
|
||||
})
|
||||
}
|
||||
|
||||
// handleAudioFile handles GET /ui/audio-file/{slug}/{n}.
|
||||
// Serves the cached MP3 file identified by voice and speed query params.
|
||||
// Serves the cached merged MP3 file identified by voice and speed query params.
|
||||
func (s *Server) handleAudioFile(w http.ResponseWriter, r *http.Request) {
|
||||
slug := r.PathValue("slug")
|
||||
n, err := strconv.Atoi(r.PathValue("n"))
|
||||
@@ -333,6 +505,43 @@ func (s *Server) handleAudioFile(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, audioPath)
|
||||
}
|
||||
|
||||
// handleAudioFilePart handles GET /ui/audio-file/{slug}/{n}/part/{p}.
|
||||
// Serves a specific MP3 part file.
|
||||
func (s *Server) handleAudioFilePart(w http.ResponseWriter, r *http.Request) {
|
||||
slug := r.PathValue("slug")
|
||||
n, err := strconv.Atoi(r.PathValue("n"))
|
||||
if err != nil || n < 1 {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
p, err := strconv.Atoi(r.PathValue("p"))
|
||||
if err != nil || p < 0 {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
voice := r.URL.Query().Get("voice")
|
||||
if voice == "" {
|
||||
voice = s.kokoroVoice
|
||||
}
|
||||
speedStr := r.URL.Query().Get("speed")
|
||||
speed := 1.0
|
||||
if speedStr != "" {
|
||||
if v, err := strconv.ParseFloat(speedStr, 64); err == nil && v > 0 {
|
||||
speed = v
|
||||
}
|
||||
}
|
||||
|
||||
partPath := s.writer.AudioPartPath(slug, n, voice, speed, p)
|
||||
if _, err := os.Stat(partPath); err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "audio/mpeg")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
http.ServeFile(w, r, partPath)
|
||||
}
|
||||
|
||||
func (s *Server) handleScrapeCatalogue(w http.ResponseWriter, r *http.Request) {
|
||||
cfg := s.oCfg
|
||||
cfg.SingleBookURL = "" // full catalogue
|
||||
|
||||
@@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
@@ -137,15 +138,25 @@ const homeTmpl = `
|
||||
<!-- Scrape form -->
|
||||
<div class="mb-10 rounded-xl border border-zinc-800 bg-zinc-900 p-5">
|
||||
<h2 class="text-sm font-semibold text-zinc-300 mb-3">Scrape a new book</h2>
|
||||
<form hx-post="/ui/scrape/book"
|
||||
<form id="scrape-form"
|
||||
hx-post="/ui/scrape/book"
|
||||
hx-target="#scrape-status"
|
||||
hx-swap="innerHTML"
|
||||
class="flex gap-2">
|
||||
<input type="url"
|
||||
name="url"
|
||||
required
|
||||
placeholder="https://novelfire.net/book/some-book"
|
||||
class="flex-1 rounded-lg bg-zinc-800 border border-zinc-700 px-3 py-2 text-sm text-zinc-100 placeholder-zinc-500 focus:outline-none focus:border-amber-500 transition-colors" />
|
||||
<div class="flex-1 relative" id="scrape-search-wrap">
|
||||
<input type="text"
|
||||
id="scrape-search"
|
||||
autocomplete="off"
|
||||
spellcheck="false"
|
||||
placeholder="Search rankings or paste a URL…"
|
||||
class="w-full rounded-lg bg-zinc-800 border border-zinc-700 px-3 py-2 text-sm text-zinc-100 placeholder-zinc-500 focus:outline-none focus:border-amber-500 transition-colors" />
|
||||
<!-- hidden url field submitted to HTMX -->
|
||||
<input type="url" name="url" id="scrape-url" required class="hidden" />
|
||||
<!-- dropdown -->
|
||||
<ul id="scrape-dropdown"
|
||||
class="hidden absolute z-50 left-0 right-0 top-full mt-1 rounded-xl border border-zinc-700 bg-zinc-900 shadow-xl max-h-80 overflow-y-auto">
|
||||
</ul>
|
||||
</div>
|
||||
<button type="submit"
|
||||
class="px-4 py-2 rounded-lg bg-amber-600 hover:bg-amber-500 text-white text-sm font-medium transition-colors whitespace-nowrap">
|
||||
Scrape
|
||||
@@ -280,6 +291,183 @@ const homeTmpl = `
|
||||
}
|
||||
|
||||
if (input) input.addEventListener('input', filterCards);
|
||||
|
||||
/* ── ranking search / scrape autocomplete ──────────────────────────────── */
|
||||
(function () {
|
||||
var RANKING = {{.RankingJSON}};
|
||||
if (!RANKING || !RANKING.length) return;
|
||||
|
||||
var searchInput = document.getElementById('scrape-search');
|
||||
var urlInput = document.getElementById('scrape-url');
|
||||
var dropdown = document.getElementById('scrape-dropdown');
|
||||
var form = document.getElementById('scrape-form');
|
||||
if (!searchInput || !urlInput || !dropdown || !form) return;
|
||||
|
||||
var activeIdx = -1;
|
||||
|
||||
// Sync the hidden url field whenever the visible input looks like a URL.
|
||||
function syncURLField(val) {
|
||||
val = val.trim();
|
||||
if (/^https?:\/\//i.test(val)) {
|
||||
urlInput.value = val;
|
||||
} else {
|
||||
urlInput.value = '';
|
||||
}
|
||||
}
|
||||
|
||||
function closeDrop() {
|
||||
dropdown.classList.add('hidden');
|
||||
dropdown.innerHTML = '';
|
||||
activeIdx = -1;
|
||||
}
|
||||
|
||||
function buildItem(item, q) {
|
||||
var li = document.createElement('li');
|
||||
li.className = 'flex items-center gap-3 px-3 py-2 cursor-pointer hover:bg-zinc-800 transition-colors';
|
||||
li.dataset.url = item.source_url || '';
|
||||
|
||||
// Cover image
|
||||
if (item.cover) {
|
||||
var img = document.createElement('img');
|
||||
img.src = item.cover;
|
||||
img.alt = '';
|
||||
img.className = 'w-10 h-14 object-cover rounded flex-shrink-0 bg-zinc-800';
|
||||
li.appendChild(img);
|
||||
} else {
|
||||
var ph = document.createElement('div');
|
||||
ph.className = 'w-10 h-14 rounded flex-shrink-0 bg-zinc-800';
|
||||
li.appendChild(ph);
|
||||
}
|
||||
|
||||
// Text block
|
||||
var txt = document.createElement('div');
|
||||
txt.className = 'min-w-0 flex-1';
|
||||
|
||||
var title = document.createElement('p');
|
||||
title.className = 'text-sm font-medium text-zinc-100 truncate';
|
||||
title.textContent = item.title || '';
|
||||
txt.appendChild(title);
|
||||
|
||||
if (item.author) {
|
||||
var author = document.createElement('p');
|
||||
author.className = 'text-xs text-zinc-400 truncate mt-0.5';
|
||||
author.textContent = item.author;
|
||||
txt.appendChild(author);
|
||||
}
|
||||
|
||||
var meta = document.createElement('div');
|
||||
meta.className = 'flex gap-1.5 mt-1 flex-wrap';
|
||||
if (item.status) {
|
||||
var s = document.createElement('span');
|
||||
s.className = 'text-xs px-1.5 py-0.5 rounded-full bg-zinc-700 text-zinc-300';
|
||||
s.textContent = item.status;
|
||||
meta.appendChild(s);
|
||||
}
|
||||
if (item.rank) {
|
||||
var r = document.createElement('span');
|
||||
r.className = 'text-xs px-1.5 py-0.5 rounded-full bg-amber-900 text-amber-300';
|
||||
r.textContent = '#' + item.rank;
|
||||
meta.appendChild(r);
|
||||
}
|
||||
txt.appendChild(meta);
|
||||
li.appendChild(txt);
|
||||
|
||||
return li;
|
||||
}
|
||||
|
||||
function setActive(idx, items) {
|
||||
var lis = dropdown.querySelectorAll('li');
|
||||
lis.forEach(function (li, i) {
|
||||
if (i === idx) li.classList.add('bg-zinc-800');
|
||||
else li.classList.remove('bg-zinc-800');
|
||||
});
|
||||
activeIdx = idx;
|
||||
if (idx >= 0 && idx < items.length) {
|
||||
searchInput.value = items[idx].title || '';
|
||||
urlInput.value = items[idx].source_url || '';
|
||||
}
|
||||
}
|
||||
|
||||
function showDrop(items, q) {
|
||||
dropdown.innerHTML = '';
|
||||
activeIdx = -1;
|
||||
if (!items.length) { closeDrop(); return; }
|
||||
items.forEach(function (item, i) {
|
||||
var li = buildItem(item, q);
|
||||
li.addEventListener('mousedown', function (e) {
|
||||
e.preventDefault(); // keep focus on input
|
||||
searchInput.value = item.title || item.source_url || '';
|
||||
urlInput.value = item.source_url || '';
|
||||
closeDrop();
|
||||
});
|
||||
dropdown.appendChild(li);
|
||||
});
|
||||
dropdown.classList.remove('hidden');
|
||||
}
|
||||
|
||||
searchInput.addEventListener('input', function () {
|
||||
var q = searchInput.value.trim().toLowerCase();
|
||||
syncURLField(searchInput.value);
|
||||
if (!q) { closeDrop(); return; }
|
||||
// If it looks like a URL, no autocomplete needed.
|
||||
if (/^https?:\/\//i.test(q)) { closeDrop(); return; }
|
||||
var results = RANKING.filter(function (item) {
|
||||
var haystack = ((item.title || '') + ' ' + (item.author || '') + ' ' + (item.status || '')).toLowerCase();
|
||||
return haystack.indexOf(q) !== -1;
|
||||
}).slice(0, 8);
|
||||
showDrop(results, q);
|
||||
});
|
||||
|
||||
searchInput.addEventListener('keydown', function (e) {
|
||||
var lis = dropdown.querySelectorAll('li');
|
||||
var items = RANKING.filter(function (item) {
|
||||
var q = searchInput.value.trim().toLowerCase();
|
||||
var haystack = ((item.title || '') + ' ' + (item.author || '') + ' ' + (item.status || '')).toLowerCase();
|
||||
return haystack.indexOf(q) !== -1;
|
||||
}).slice(0, 8);
|
||||
|
||||
if (e.key === 'ArrowDown') {
|
||||
e.preventDefault();
|
||||
setActive(Math.min(activeIdx + 1, lis.length - 1), items);
|
||||
} else if (e.key === 'ArrowUp') {
|
||||
e.preventDefault();
|
||||
setActive(Math.max(activeIdx - 1, 0), items);
|
||||
} else if (e.key === 'Enter' && activeIdx >= 0) {
|
||||
e.preventDefault();
|
||||
if (items[activeIdx]) {
|
||||
searchInput.value = items[activeIdx].title || items[activeIdx].source_url || '';
|
||||
urlInput.value = items[activeIdx].source_url || '';
|
||||
}
|
||||
closeDrop();
|
||||
} else if (e.key === 'Escape') {
|
||||
closeDrop();
|
||||
}
|
||||
});
|
||||
|
||||
// Validate before submit: if url field empty, treat visible input as raw URL.
|
||||
form.addEventListener('htmx:configRequest', function (e) {
|
||||
var val = searchInput.value.trim();
|
||||
if (!urlInput.value && /^https?:\/\//i.test(val)) {
|
||||
urlInput.value = val;
|
||||
}
|
||||
});
|
||||
|
||||
// Also handle plain form submit (non-HTMX fallback).
|
||||
form.addEventListener('submit', function () {
|
||||
var val = searchInput.value.trim();
|
||||
if (!urlInput.value && /^https?:\/\//i.test(val)) {
|
||||
urlInput.value = val;
|
||||
}
|
||||
});
|
||||
|
||||
// Close dropdown when clicking outside.
|
||||
document.addEventListener('mousedown', function (e) {
|
||||
if (!document.getElementById('scrape-search-wrap').contains(e.target)) {
|
||||
closeDrop();
|
||||
}
|
||||
});
|
||||
}());
|
||||
|
||||
}());
|
||||
</script>`
|
||||
|
||||
@@ -295,9 +483,22 @@ func (s *Server) handleHome(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Load ranking items for the scrape-search autocomplete.
|
||||
// Failures are non-fatal — the form degrades to a plain URL input.
|
||||
rankingItems, _ := s.writer.ReadRankingItems()
|
||||
|
||||
// Encode ranking items as JSON for embedding in the template.
|
||||
rankingJSON, _ := json.Marshal(rankingItems)
|
||||
|
||||
t := template.Must(template.New("home").Parse(homeTmpl))
|
||||
var buf bytes.Buffer
|
||||
_ = t.Execute(&buf, struct{ Books interface{} }{Books: books})
|
||||
_ = t.Execute(&buf, struct {
|
||||
Books interface{}
|
||||
RankingJSON template.JS
|
||||
}{
|
||||
Books: books,
|
||||
RankingJSON: template.JS(rankingJSON),
|
||||
})
|
||||
|
||||
s.respond(w, r, "Home", buf.String())
|
||||
}
|
||||
@@ -1619,15 +1820,22 @@ const chapterTmpl = `
|
||||
}
|
||||
|
||||
// ── server-side audio generation ─────────────────────────────────────────────
|
||||
// POST /ui/audio/{slug}/{n} — idempotent; returns {url} immediately if cached.
|
||||
// cb(url) is called with the audio-file URL on success.
|
||||
// Returns the AbortController so the caller can cancel in-flight requests.
|
||||
// POST /ui/audio/{slug}/{n} — returns {url, parts, merged}.
|
||||
// When merged=false, url is part-0; polling /ui/audio/{slug}/{n}/status
|
||||
// detects when the full merged file is ready and swaps audio.src seamlessly.
|
||||
// cb(url) is called with the initial (part-0 or merged) URL on success.
|
||||
|
||||
var currentAudioCtrl = null; // AbortController for the active generateAudio fetch
|
||||
var mergePoller = null; // setInterval id for polling merged status
|
||||
|
||||
function clearMergePoller() {
|
||||
if (mergePoller !== null) { clearInterval(mergePoller); mergePoller = null; }
|
||||
}
|
||||
|
||||
function generateAudio(chapterN, cb) {
|
||||
// Cancel any previous in-flight generation.
|
||||
// Cancel any previous in-flight generation and polling.
|
||||
if (currentAudioCtrl) { currentAudioCtrl.abort(); }
|
||||
clearMergePoller();
|
||||
var ctrl = new AbortController();
|
||||
currentAudioCtrl = ctrl;
|
||||
|
||||
@@ -1645,8 +1853,35 @@ const chapterTmpl = `
|
||||
return res.json();
|
||||
})
|
||||
.then(function (data) {
|
||||
if (data && data.url) cb(data.url);
|
||||
else throw new Error('no url in response');
|
||||
if (!data || !data.url) throw new Error('no url in response');
|
||||
cb(data.url);
|
||||
// If the server is still generating remaining parts, start polling.
|
||||
if (!data.merged && data.parts > 1) {
|
||||
var statusURL = '/ui/audio/' + SLUG + '/' + chapterN + '/status'
|
||||
+ '?voice=' + encodeURIComponent(voiceSel.value)
|
||||
+ '&speed=' + parseFloat(speedSlider.value);
|
||||
mergePoller = setInterval(function () {
|
||||
fetch(statusURL)
|
||||
.then(function (r) { return r.ok ? r.json() : Promise.reject(r.status); })
|
||||
.then(function (s) {
|
||||
if (s.merged) {
|
||||
clearMergePoller();
|
||||
// Seamlessly swap to the full merged file.
|
||||
var ratio = (audio.duration && isFinite(audio.duration))
|
||||
? audio.currentTime / audio.duration : 0;
|
||||
audio.addEventListener('loadedmetadata', function onMeta() {
|
||||
audio.removeEventListener('loadedmetadata', onMeta);
|
||||
if (audio.duration && isFinite(audio.duration)) {
|
||||
audio.currentTime = ratio * audio.duration;
|
||||
}
|
||||
}, { once: true });
|
||||
audio.src = s.url;
|
||||
audio.load();
|
||||
}
|
||||
})
|
||||
.catch(function () { /* ignore transient poll errors */ });
|
||||
}, 3000);
|
||||
}
|
||||
})
|
||||
.catch(function (e) {
|
||||
if (e.name === 'AbortError') return; // silently cancelled
|
||||
@@ -1683,6 +1918,7 @@ const chapterTmpl = `
|
||||
// ── stop / cleanup ────────────────────────────────────────────────────────────
|
||||
function stop() {
|
||||
if (currentAudioCtrl) { currentAudioCtrl.abort(); currentAudioCtrl = null; }
|
||||
clearMergePoller();
|
||||
audio.pause();
|
||||
audio.src = '';
|
||||
prefetchFired = false;
|
||||
|
||||
@@ -417,15 +417,29 @@ func (w *Writer) AudioDir(slug string) string {
|
||||
// The filename is keyed by chapter number, voice, and speed so that different
|
||||
// settings never collide. Speed is formatted to one decimal place (e.g. "1.0").
|
||||
func (w *Writer) AudioPath(slug string, n int, voice string, speed float64) string {
|
||||
// Sanitise voice so it is safe as a filename component.
|
||||
safeVoice := strings.Map(func(r rune) rune {
|
||||
safeVoice := sanitiseVoice(voice)
|
||||
filename := fmt.Sprintf("ch%d-%s-%.1f.mp3", n, safeVoice, speed)
|
||||
return filepath.Join(w.AudioDir(slug), filename)
|
||||
}
|
||||
|
||||
// AudioPartPath returns the path for an individual audio chunk generated during
|
||||
// chunked TTS. Part files are named ch{n}-{voice}-{speed}.part{p}.mp3 and are
|
||||
// deleted after they have been merged into the final AudioPath file.
|
||||
func (w *Writer) AudioPartPath(slug string, n int, voice string, speed float64, part int) string {
|
||||
safeVoice := sanitiseVoice(voice)
|
||||
filename := fmt.Sprintf("ch%d-%s-%.1f.part%d.mp3", n, safeVoice, speed, part)
|
||||
return filepath.Join(w.AudioDir(slug), filename)
|
||||
}
|
||||
|
||||
// sanitiseVoice converts a voice name into a string that is safe to embed in a
|
||||
// filename (only a-z, A-Z, 0-9, '_', '-' are kept; everything else becomes '_').
|
||||
func sanitiseVoice(voice string) string {
|
||||
return strings.Map(func(r rune) rune {
|
||||
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '_' || r == '-' {
|
||||
return r
|
||||
}
|
||||
return '_'
|
||||
}, voice)
|
||||
filename := fmt.Sprintf("ch%d-%s-%.1f.mp3", n, safeVoice, speed)
|
||||
return filepath.Join(w.AudioDir(slug), filename)
|
||||
}
|
||||
|
||||
// chapterPath computes the full file path for a chapter.
|
||||
|
||||
Reference in New Issue
Block a user