T-030: Positionsabgleich — Lesen beim Laden (lokal bevorzugt, sonst audible last-heard), Schreiben best-effort beim Pausieren

This commit is contained in:
2026-11-06 20:35:54 +01:00
parent ccaefacb8b
commit bb1fea60df

View File

@ -14,7 +14,7 @@ use std::time::{Duration, Instant};
use serde::Serialize; use serde::Serialize;
use tauri::{AppHandle, Emitter}; use tauri::{AppHandle, Emitter};
use crate::audible::{books_dir, home_dir}; use crate::audible::{audible_binary, books_dir, home_dir};
#[derive(Debug, Clone, Serialize, PartialEq)] #[derive(Debug, Clone, Serialize, PartialEq)]
pub struct Chapter { pub struct Chapter {
@ -471,6 +471,9 @@ impl MpvPlayer {
snap.position, snap.position,
snap.title.as_deref().unwrap_or(""), snap.title.as_deref().unwrap_or(""),
); );
if let Some(bin) = audible_binary() {
audible_write_pos(&bin, &asin, snap.position);
}
*self.last_saved.lock().unwrap() = Instant::now(); *self.last_saved.lock().unwrap() = Instant::now();
} }
} }
@ -558,6 +561,56 @@ fn mpv_binary() -> Result<PathBuf, String> {
Err("mpv nicht gefunden — pacman -S mpv".into()) Err("mpv nicht gefunden — pacman -S mpv".into())
} }
/// Position an Audible melden (best effort, T-030). Läuft asynchron,
/// Fehlervorkehren: keine Logik hängt davon ab.
fn audible_write_pos(audible_bin: &Path, asin: &str, pos: f64) {
let url = format!("https://api.audible.de/1.0/content/{asin}/state");
let body = serde_json::json!({
"last_position_heard": {
"position_asin": asin.to_string(),
"time_range": { "start": pos.floor(), "end": pos.floor() },
"utc_timestamp": format_utc_now(),
}
});
let bin = audible_bin.to_path_buf();
std::thread::Builder::new()
.name("hoerwerk-audsync".into())
.spawn(move || {
let _ = Command::new(&bin)
.args(["api", "-m", "post", "-b"])
.arg(&serde_json::to_string(&body).unwrap_or_default())
.arg(&url)
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
})
.ok();
}
fn format_utc_now() -> String {
// RFC3339 mit Sekundengenauigkeit, UTC aus Systemzeit (ohne chrono)
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let mut secs = now as i64;
let days = secs / 86_400;
secs %= 86_400;
let (h, m, s2) = (secs / 3600, (secs % 3600) / 60, secs % 60);
// Zivilkalender: Tage seit 1970-01-01 → Datum (Algorithmus von Hinnant)
let z = days + 719_468;
let era = z / 146_097;
let doe = z % 146_097;
let yoe = doe / 365;
let y = yoe + era * 400;
let doy = doe % 365;
let mp = (5 * doy + 2) / 153;
let d = doy - mp * 153 / 5 + 1;
let mo = if mp < 10 { mp + 3 } else { mp - 9 };
let y = if mo <= 2 { y + 1 } else { y };
format!("{y:04}-{mo:02}-{d:02}T{h:02}:{m:02}:{s2:02}.000Z")
}
/// Last-heard-Position aus der audible-Sync-Datei im Buchordner (T-030). /// Last-heard-Position aus der audible-Sync-Datei im Buchordner (T-030).
pub fn audible_sync_pos(book_dir: &Path) -> Option<f64> { pub fn audible_sync_pos(book_dir: &Path) -> Option<f64> {
let s = fs::read_to_string(book_dir.join("sync.json")).ok()?; let s = fs::read_to_string(book_dir.join("sync.json")).ok()?;