Files
SubMiner/launcher/jellyfin.ts
sudacode ba94a33b46 refactor(launcher): extract mpv, youtube, and jellyfin modules
- launcher/mpv.ts: state object, mpv IPC, process management, socket helpers
- launcher/youtube.ts: YouTube subtitle generation pipeline and helpers
- launcher/jellyfin.ts: Jellyfin API client, icon caching, play menu
- runAppCommandWithInherit and related functions placed in mpv.ts
- buildAppEnv deduplicated into single helper in mpv.ts
2026-02-17 09:16:52 -08:00

355 lines
12 KiB
TypeScript

import fs from "node:fs";
import path from "node:path";
import { spawnSync } from "node:child_process";
import type { Args, JellyfinSessionConfig, JellyfinLibraryEntry, JellyfinItemEntry, JellyfinGroupEntry } from "./types.js";
import { log, fail } from "./log.js";
import { commandExists, resolvePathMaybe } from "./util.js";
import {
pickLibrary, pickItem, pickGroup, promptOptionalJellyfinSearch,
findRofiTheme,
} from "./picker.js";
import { loadLauncherJellyfinConfig } from "./config.js";
import {
runAppCommandWithInheritLogged, launchMpvIdleDetached, waitForUnixSocketReady,
} from "./mpv.js";
export function sanitizeServerUrl(value: string): string {
return value.trim().replace(/\/+$/, "");
}
export async function jellyfinApiRequest<T>(
session: JellyfinSessionConfig,
requestPath: string,
): Promise<T> {
const url = `${session.serverUrl}${requestPath}`;
const response = await fetch(url, {
headers: {
"X-Emby-Token": session.accessToken,
Authorization: `MediaBrowser Token="${session.accessToken}"`,
},
});
if (response.status === 401 || response.status === 403) {
fail("Jellyfin token invalid/expired. Run --jellyfin-login or --jellyfin.");
}
if (!response.ok) {
fail(`Jellyfin API failed: ${response.status} ${response.statusText}`);
}
return (await response.json()) as T;
}
function itemPreviewUrl(session: JellyfinSessionConfig, id: string): string {
return `${session.serverUrl}/Items/${id}/Images/Primary?maxHeight=720&quality=85&api_key=${encodeURIComponent(session.accessToken)}`;
}
function jellyfinIconCacheDir(session: JellyfinSessionConfig): string {
const serverKey = session.serverUrl.replace(/[^a-zA-Z0-9]+/g, "_").slice(0, 96);
const userKey = session.userId.replace(/[^a-zA-Z0-9]+/g, "_").slice(0, 96);
const baseDir = session.iconCacheDir
? resolvePathMaybe(session.iconCacheDir)
: path.join("/tmp", "subminer-jellyfin-icons");
return path.join(baseDir, serverKey, userKey);
}
function jellyfinIconPath(session: JellyfinSessionConfig, id: string): string {
const safeId = id.replace(/[^a-zA-Z0-9._-]+/g, "_");
return path.join(jellyfinIconCacheDir(session), `${safeId}.jpg`);
}
function ensureJellyfinIcon(
session: JellyfinSessionConfig,
id: string,
): string | null {
if (!session.pullPictures || !id || !commandExists("curl")) return null;
const iconPath = jellyfinIconPath(session, id);
try {
if (fs.existsSync(iconPath) && fs.statSync(iconPath).size > 0) {
return iconPath;
}
} catch {
// continue to download
}
try {
fs.mkdirSync(path.dirname(iconPath), { recursive: true });
} catch {
return null;
}
const result = spawnSync(
"curl",
["-fsSL", "-o", iconPath, itemPreviewUrl(session, id)],
{ stdio: "ignore" },
);
if (result.error || result.status !== 0) return null;
try {
if (fs.existsSync(iconPath) && fs.statSync(iconPath).size > 0) {
return iconPath;
}
} catch {
return null;
}
return null;
}
export function formatJellyfinItemDisplay(item: Record<string, unknown>): string {
const type = typeof item.Type === "string" ? item.Type : "Item";
const name = typeof item.Name === "string" ? item.Name : "Untitled";
if (type === "Episode") {
const series = typeof item.SeriesName === "string" ? item.SeriesName : "";
const season =
typeof item.ParentIndexNumber === "number"
? String(item.ParentIndexNumber).padStart(2, "0")
: "00";
const episode =
typeof item.IndexNumber === "number"
? String(item.IndexNumber).padStart(2, "0")
: "00";
return `${series} S${season}E${episode} ${name}`.trim();
}
return `${name} (${type})`;
}
export async function resolveJellyfinSelection(
args: Args,
session: JellyfinSessionConfig,
themePath: string | null = null,
): Promise<string> {
const libsPayload = await jellyfinApiRequest<{ Items?: Array<Record<string, unknown>> }>(
session,
`/Users/${session.userId}/Views`,
);
const libraries: JellyfinLibraryEntry[] = (libsPayload.Items || [])
.map((item) => ({
id: typeof item.Id === "string" ? item.Id : "",
name: typeof item.Name === "string" ? item.Name : "Untitled",
kind:
typeof item.CollectionType === "string"
? item.CollectionType
: typeof item.Type === "string"
? item.Type
: "unknown",
}))
.filter((item) => item.id.length > 0);
let libraryId = session.defaultLibraryId;
if (!libraryId) {
libraryId = pickLibrary(
session,
libraries,
args.useRofi,
ensureJellyfinIcon,
"",
themePath,
);
if (!libraryId) fail("No Jellyfin library selected.");
}
const searchTerm = await promptOptionalJellyfinSearch(args.useRofi, themePath);
const fetchItemsPaged = async (parentId: string) => {
const out: Array<Record<string, unknown>> = [];
let startIndex = 0;
while (true) {
const payload = await jellyfinApiRequest<{
Items?: Array<Record<string, unknown>>;
TotalRecordCount?: number;
}>(
session,
`/Users/${session.userId}/Items?ParentId=${encodeURIComponent(parentId)}&Recursive=false&SortBy=SortName&SortOrder=Ascending&Limit=500&StartIndex=${startIndex}`,
);
const page = payload.Items || [];
if (page.length === 0) break;
out.push(...page);
startIndex += page.length;
const total = typeof payload.TotalRecordCount === "number"
? payload.TotalRecordCount
: null;
if (total !== null && startIndex >= total) break;
if (page.length < 500) break;
}
return out;
};
const topLevelEntries = await fetchItemsPaged(libraryId);
const groups: JellyfinGroupEntry[] = topLevelEntries
.filter((item) => {
const type = typeof item.Type === "string" ? item.Type : "";
return (
type === "Series" ||
type === "Folder" ||
type === "CollectionFolder" ||
type === "Season"
);
})
.map((item) => {
const type = typeof item.Type === "string" ? item.Type : "Folder";
const name = typeof item.Name === "string" ? item.Name : "Untitled";
return {
id: typeof item.Id === "string" ? item.Id : "",
name,
type,
display: `${name} (${type})`,
};
})
.filter((entry) => entry.id.length > 0);
let contentParentId = libraryId;
const selectedGroupId = pickGroup(
session,
groups,
args.useRofi,
ensureJellyfinIcon,
searchTerm,
themePath,
);
if (selectedGroupId) {
contentParentId = selectedGroupId;
const nextLevelEntries = await fetchItemsPaged(selectedGroupId);
const seasons: JellyfinGroupEntry[] = nextLevelEntries
.filter((item) => {
const type = typeof item.Type === "string" ? item.Type : "";
return type === "Season" || type === "Folder";
})
.map((item) => {
const type = typeof item.Type === "string" ? item.Type : "Season";
const name = typeof item.Name === "string" ? item.Name : "Untitled";
return {
id: typeof item.Id === "string" ? item.Id : "",
name,
type,
display: `${name} (${type})`,
};
})
.filter((entry) => entry.id.length > 0);
if (seasons.length > 0) {
const selectedSeasonId = pickGroup(
session,
seasons,
args.useRofi,
ensureJellyfinIcon,
"",
themePath,
);
if (!selectedSeasonId) fail("No Jellyfin season selected.");
contentParentId = selectedSeasonId;
}
}
const fetchPage = async (startIndex: number) =>
jellyfinApiRequest<{
Items?: Array<Record<string, unknown>>;
TotalRecordCount?: number;
}>(
session,
`/Users/${session.userId}/Items?ParentId=${encodeURIComponent(contentParentId)}&Recursive=true&SortBy=SortName&SortOrder=Ascending&Limit=500&StartIndex=${startIndex}`,
);
const allEntries: Array<Record<string, unknown>> = [];
let startIndex = 0;
while (true) {
const payload = await fetchPage(startIndex);
const page = payload.Items || [];
if (page.length === 0) break;
allEntries.push(...page);
startIndex += page.length;
const total = typeof payload.TotalRecordCount === "number"
? payload.TotalRecordCount
: null;
if (total !== null && startIndex >= total) break;
if (page.length < 500) break;
}
let items: JellyfinItemEntry[] = allEntries
.filter((item) => {
const type = typeof item.Type === "string" ? item.Type : "";
return type === "Movie" || type === "Episode" || type === "Audio";
})
.map((item) => ({
id: typeof item.Id === "string" ? item.Id : "",
name: typeof item.Name === "string" ? item.Name : "",
type: typeof item.Type === "string" ? item.Type : "Item",
display: formatJellyfinItemDisplay(item),
}))
.filter((item) => item.id.length > 0);
if (items.length === 0) {
items = allEntries
.filter((item) => {
const type = typeof item.Type === "string" ? item.Type : "";
if (type === "Folder" || type === "CollectionFolder") return false;
const mediaType =
typeof item.MediaType === "string" ? item.MediaType.toLowerCase() : "";
if (mediaType === "video" || mediaType === "audio") return true;
return (
type === "Movie" ||
type === "Episode" ||
type === "Audio" ||
type === "Video" ||
type === "MusicVideo"
);
})
.map((item) => ({
id: typeof item.Id === "string" ? item.Id : "",
name: typeof item.Name === "string" ? item.Name : "",
type: typeof item.Type === "string" ? item.Type : "Item",
display: formatJellyfinItemDisplay(item),
}))
.filter((item) => item.id.length > 0);
}
const itemId = pickItem(session, items, args.useRofi, ensureJellyfinIcon, "", themePath);
if (!itemId) fail("No Jellyfin item selected.");
return itemId;
}
export async function runJellyfinPlayMenu(
appPath: string,
args: Args,
scriptPath: string,
mpvSocketPath: string,
): Promise<never> {
const config = loadLauncherJellyfinConfig();
const session: JellyfinSessionConfig = {
serverUrl: sanitizeServerUrl(args.jellyfinServer || config.serverUrl || ""),
accessToken: config.accessToken || "",
userId: config.userId || "",
defaultLibraryId: config.defaultLibraryId || "",
pullPictures: config.pullPictures === true,
iconCacheDir: config.iconCacheDir || "",
};
if (!session.serverUrl || !session.accessToken || !session.userId) {
fail(
"Missing Jellyfin session config. Run `subminer --jellyfin` or `subminer --jellyfin-login` first.",
);
}
const rofiTheme = args.useRofi ? findRofiTheme(scriptPath) : null;
if (args.useRofi && !rofiTheme) {
log(
"warn",
args.logLevel,
"Rofi theme not found for Jellyfin picker; using rofi defaults.",
);
}
const itemId = await resolveJellyfinSelection(args, session, rofiTheme);
log("debug", args.logLevel, `Jellyfin selection resolved: itemId=${itemId}`);
try {
fs.rmSync(mpvSocketPath, { force: true });
} catch {
// ignore cleanup errors
}
log("debug", args.logLevel, `Ensuring MPV IPC socket is ready: ${mpvSocketPath}`);
launchMpvIdleDetached(mpvSocketPath, appPath, args);
const mpvReady = await waitForUnixSocketReady(mpvSocketPath, 8000);
log(
"debug",
args.logLevel,
`MPV socket ready check result: ${mpvReady ? "ready" : "not ready"}`,
);
const forwarded = ["--start", "--jellyfin-play", "--jellyfin-item-id", itemId];
if (args.logLevel !== "info") forwarded.push("--log-level", args.logLevel);
runAppCommandWithInheritLogged(appPath, forwarded, args.logLevel, "jellyfin-play");
}