Refactor random item fetching logic and enhance error handling; add JSON download feature

This commit is contained in:
CodeDevMLH
2026-02-09 02:39:32 +01:00
parent 998a0cfc68
commit abca7cb3b6

View File

@@ -1,44 +1,32 @@
(async () => { (async () => {
// 1. Prüfen, ob wir im Jellyfin-Kontext sind
if (!window.ApiClient) {
console.error("ApiClient nicht gefunden. Bist du sicher, dass Jellyfin im Browser offen ist?");
return;
}
const apiClient = window.ApiClient; const apiClient = window.ApiClient;
const userId = apiClient.currentUserId(); if (!apiClient) { console.error("Logged in?"); return; }
// 2. Alle Items abrufen (Filme und Serien) try {
// Wir fordern extra viele Felder an, um "alle verfügbaren Einträge" zu erhalten // Fetch 1 random item ID
const response = await apiClient.getItems(userId, { const rnd = await apiClient.getItems(apiClient.getCurrentUserId(), { SortBy: "Random", Limit: 1, Recursive: true, IncludeItemTypes: "Movie,Series" });
Recursive: true, if (rnd.Items.length > 0) {
IncludeItemTypes: "Movie,Series,Episode", const id = rnd.Items[0].Id;
Fields: "Overview,Genres,Path,OfficialRating,Tags,CommunityRating,ExternalUrls,MediaSources,ProductionYear", console.log("Random Item ID:", id);
EnableImageTypes: "Primary,Backdrop,Banner"
});
const items = response.Items; // Fetch Default Details
const defd = await apiClient.getItem(apiClient.getCurrentUserId(), id);
console.log("Default Fields:", defd);
if (!items || items.length === 0) { // Fetch ALL Known Fields manually
console.warn("Keine Items in deiner Bibliothek gefunden."); const allFields = "Chapters,People,MediaStreams,UserData,RecursiveItemCount,DateCreated,MediaSources,ProductionYear,Studios,Genres,Tags,RemoteTrailers,ProviderIds,Overview,CommunityRating,CriticRating,OfficialRating,PremiereDate,RunTimeTicks";
return; const full = await res.json();
} console.log("Full Details:", full);
// 3. Zufälliges Item auswählen // Helper to download JSON
const randomItem = items[Math.floor(Math.random() * items.length)]; const blob = new Blob([JSON.stringify(full, null, 2)], { type: "application/json" });
const url = URL.createObjectURL(blob);
// 4. Ausgabe in der Konsole const a = document.createElement("a");
console.log(`%c🎲 Zufälliges Item: ${randomItem.Name}`, "color: #00a4dc; font-weight: bold; font-size: 1.4em;"); a.href = url;
a.download = `jellyfin-item-${id}.json`;
// Gibt alle Metadaten als ausklappbares Objekt aus a.click();
console.log("Vollständige Metadaten:", randomItem); URL.revokeObjectURL(url);
console.log("Downloaded JSON file.");
// Erstellt eine übersichtliche Tabelle der wichtigsten Werte } else { console.warn("No items."); }
console.table({ } catch (e) { console.error(e); }
Name: randomItem.Name,
Typ: randomItem.Type,
Jahr: randomItem.ProductionYear,
ID: randomItem.Id,
Pfad: randomItem.Path
});
})(); })();