M1-Politur nach Feedback: lokal statt fertig-Chip + Löschen für lokale Bücher, 'Nur lokale'-Filter, Offline-Scan über ffprobe (lokale Titel auch ohne Netz), Klartext-Fehler ('nicht als Datei', Timeout)

This commit is contained in:
2026-11-06 20:09:07 +01:00
parent 560361004a
commit 5b1749ec78
6 changed files with 196 additions and 31 deletions

View File

@ -1,6 +1,6 @@
use std::env;
use std::fs;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process::Command;
use serde::{Deserialize, Serialize};
@ -233,6 +233,79 @@ pub fn parse_library(content: &str) -> Result<Vec<Book>, String> {
serde_json::from_str(content).map_err(|e| format!("Bibliothek-JSON nicht parsebar: {e}"))
}
/// Lokal abgelegte Bücher (book.m4b im Bücher-Verzeichnis) — auch nutzbar,
/// wenn Audible gerade nicht erreichbar ist. Metadaten aus m4b-Tags.
pub fn local_books() -> Vec<Book> {
let dir = books_dir();
let mut out = Vec::new();
let Ok(rd) = fs::read_dir(dir) else {
return out;
};
for e in rd.flatten() {
let book_dir = e.path();
let m4b = book_dir.join("book.m4b");
if !m4b.is_file() {
continue;
}
let asin = book_dir
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("?")
.to_string();
let (title, authors, runtime_min) = probe_m4b(&m4b);
out.push(Book {
asin: asin.clone(),
title: if title.is_empty() { format!("Unbekannt ({asin})") } else { title },
authors,
narrators: String::new(),
runtime_length_min: runtime_min.max(0),
cover_url: String::new(),
percent_complete: 0.0,
is_finished: false,
date_added: String::new(),
local: true,
});
}
out
}
/// ffprobe: Tag title/artist + Dauer in Minuten.
fn probe_m4b(m4b: &Path) -> (String, String, i64) {
let out = std::process::Command::new("ffprobe")
.args(["-v", "error", "-show_format", "-of", "json"])
.arg(m4b)
.output();
let mut title = String::new();
let mut artist = String::new();
let mut minutes = 0i64;
if let Ok(o) = out {
let txt = String::from_utf8_lossy(&o.stdout);
if let Ok(v) = serde_json::from_str::<serde_json::Value>(&txt) {
title = v["format"]["tags"]["title"]
.as_str()
.unwrap_or("")
.to_string();
artist = v["format"]["tags"]["artist"]
.as_str()
.unwrap_or("")
.to_string();
if let Some(sec) = v["format"]["duration"].as_str().and_then(|s| s.parse::<f64>().ok()) {
minutes = (sec / 60.0).round() as i64;
}
}
}
// Falls kein Titel im Tag: ASIN-Ordnername als Anhaltspunkt.
if title.is_empty() {
if let Some(p) = m4b.parent() {
title = p
.file_name()
.map(|n| n.to_string_lossy().to_string())
.unwrap_or_default();
}
}
(title, artist, minutes)
}
/// Taugt stderr von audible-cli: Warnungen und Passwort-Prompts raus,
/// echte Fehlertexte bleiben.
pub(crate) fn reduce_noise(s: &str) -> String {