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