From 0b7e223ad5bddeb640ce0dbcdbda7c8e516d6a1a Mon Sep 17 00:00:00 2001 From: wlkns-dev Date: Fri, 6 Nov 2026 20:20:51 +0100 Subject: [PATCH] Altersangaben: Genre-Signal-Chip (Kinder/Jugend aus Audible-Genres) + manuelle Altersnote pro Buch (labels.json, Inline-Edit mit Stift-Button) --- src-tauri/src/audible.rs | 51 +++++++++++++++++++++++++++++++++ src-tauri/src/download.rs | 17 +++++++++++ src-tauri/src/lib.rs | 19 +++++++++++++ src/App.svelte | 60 +++++++++++++++++++++++++++++++++++++-- src/app.css | 11 +++++++ src/lib/Icon.svelte | 2 ++ src/lib/audible.ts | 4 +++ 7 files changed, 162 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/audible.rs b/src-tauri/src/audible.rs index 6dbae90..166db4f 100644 --- a/src-tauri/src/audible.rs +++ b/src-tauri/src/audible.rs @@ -32,6 +32,8 @@ pub struct Book { pub is_finished: bool, #[serde(default)] pub date_added: String, + #[serde(default)] + pub genres: String, /// lokal vorhanden (book.m4b im Datenverzeichnis) — vom Backend gesetzt #[serde(default, skip_deserializing)] pub local: bool, @@ -263,6 +265,7 @@ pub fn local_books() -> Vec { percent_complete: 0.0, is_finished: false, date_added: String::new(), + genres: String::new(), local: true, }); } @@ -306,6 +309,54 @@ fn probe_m4b(m4b: &Path) -> (String, String, i64) { (title, artist, minutes) } +// Manuelle Altersnotizen (bewertbar pro Buch, lokale JSON-Ablage) + +#[derive(Debug, Clone, Serialize)] +pub struct Labels(pub std::collections::BTreeMap); + +fn labels_path() -> PathBuf { + books_dir() + .parent() + .map(|p| p.join("labels.json")) + .unwrap_or_else(|| PathBuf::from("labels.json")) +} + +pub fn alter_notes() -> Labels { + let map: std::collections::BTreeMap = fs::read_to_string(labels_path()) + .ok() + .and_then(|txt| serde_json::from_str(&txt).ok()) + .unwrap_or_default(); + Labels(map) +} + +pub fn set_alter_note(asin: &str, note: Option) -> Result<(), String> { + let asin = asin.trim().to_string(); + if asin.is_empty() { + return Err("keine ASIN angegeben".to_string()); + } + let mut l = alter_notes(); + let note = note.map(|n| n.trim().to_string()).filter(|n| !n.is_empty()); + match note { + Some(n) => { + l.0.insert(asin, n); + } + None => { + l.0.remove(&asin); + } + } + if let Some(parent) = labels_path().parent() { + fs::create_dir_all(parent).map_err(|e| format!("Notiz-Ordner: {e}"))?; + } + let path = labels_path(); + let tmp = path.with_extension("json.tmp"); + fs::write(&tmp, serde_json::to_vec(&l).unwrap_or_default()) + .map_err(|e| format!("Notiz schreiben: {e}"))?; + fs::rename(&tmp, &path).map_err(|e| format!("Notiz speichern: {e}") + ) +} + +// Manuelle Altersnotizen — lokale JSON-Ablage (siehe labels_path) + /// Taugt stderr von audible-cli: Warnungen und Passwort-Prompts raus, /// echte Fehlertexte bleiben. pub(crate) fn reduce_noise(s: &str) -> String { diff --git a/src-tauri/src/download.rs b/src-tauri/src/download.rs index 9e6ce4e..090fca0 100644 --- a/src-tauri/src/download.rs +++ b/src-tauri/src/download.rs @@ -730,6 +730,23 @@ mod tests { let _ = fs::remove_dir_all(&root); } + #[test] + fn alter_note_roundtrip() { + let tmp = std::env::temp_dir().join(format!("hw-labels-{}", std::process::id())); + unsafe { + std::env::set_var("HOERWERK_BOOKS_DIR", &tmp); + } + crate::audible::set_alter_note("B08L4WJMDM", Some("ab 6".into())).unwrap(); + let l = crate::audible::alter_notes(); + assert_eq!(l.0.get("B08L4WJMDM").map(|s| s.as_str()), Some("ab 6")); + crate::audible::set_alter_note("B08L4WJMDM", None).unwrap(); + assert!(crate::audible::alter_notes().0.get("B08L4WJMDM").is_none()); + unsafe { + std::env::remove_var("HOERWERK_BOOKS_DIR"); + } + let _ = fs::remove_dir_all(&tmp); + } + #[test] fn dl_item_serde_status() { let it = DlItem { diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 7b5c492..8d29e82 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -68,6 +68,23 @@ async fn stream_only() -> Result, String> { .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}")) } + +#[tauri::command] +async fn get_alter_notes() -> Result { + tauri::async_runtime::spawn_blocking(audible::alter_notes) + .await + .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}")) +} + +#[tauri::command] +async fn set_alter_note( + asin: String, + note: Option, +) -> Result<(), String> { + tauri::async_runtime::spawn_blocking(move || audible::set_alter_note(&asin, note)) + .await + .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? +} #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { tauri::Builder::default() @@ -86,6 +103,8 @@ pub fn run() { get_library, get_local_books, stream_only, + get_alter_notes, + set_alter_note, download_list, download_enqueue, download_cancel, diff --git a/src/App.svelte b/src/App.svelte index 1efde81..26a4a04 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -6,6 +6,8 @@ getLibrary, getLocalBooks, getStreamOnly, + getAlterNotes, + setAlterNote, downloadList, downloadEnqueue, downloadCancel, @@ -35,6 +37,40 @@ let loadedOverride = $state>({}) let removedLocals = $state>({}) let streamOnly = $state>({}) + let alterNotes = $state>({}) + let editingAlter = $state(null) + let alterDraft = $state('') + + function ageTag(b: Book): 'Kinder' | 'Jugend' | null { + const g = (b.genres || '').toLowerCase() + if (g.includes('kinder')) return 'Kinder' + if (g.includes('jugend')) return 'Jugend' + return null + } + + function alterNote(b: Book): string { + return alterNotes[b.asin] ?? '' + } + + function startEditAlter(b: Book): void { + editingAlter = b.asin + alterDraft = alterNote(b) + } + + async function saveAlter(b: Book): Promise { + try { + await setAlterNote(b.asin, alterDraft.trim() || null) + alterNotes = { ...alterNotes, [b.asin]: alterDraft.trim() } + if (!alterDraft.trim()) { + const rest = { ...alterNotes } + delete rest[b.asin] + alterNotes = rest + } + editingAlter = null + } catch (e) { + error = String(e) + } + } const actives = $derived.by(() => { let n = 0 @@ -256,6 +292,9 @@ // periodische Abgleich macht den Backend-Stand zur Autorität. const syncId = setInterval(syncDownloads, 3000) syncDownloads() + getAlterNotes() + .then((all) => (alterNotes = all)) + .catch(() => {}) return () => { clearPoll() clearInterval(syncId) @@ -381,6 +420,11 @@ {#if b.is_finished && !isLocal(b)} fertig {/if} + {#if alterNote(b)} + {alterNote(b)} + {:else if ageTag(b)} + {ageTag(b)} + {/if} {#if b.runtime_length_min > 0}{fmtRuntime(b.runtime_length_min)}{/if} {#if d && d.status === 'failed'}Fehlgeschlagen{/if} @@ -393,8 +437,20 @@
{/if}
- {#if isLocal(b)} - + {#if editingAlter === b.asin} + e.key === 'Enter' && saveAlter(b)} + /> + + + {:else if isLocal(b)} + + {:else if streamOnly[b.asin]}