oops went on a massive code sesh and theres like a whole website now
This commit is contained in:
124
src/lib/server/jellyfin.ts
Normal file
124
src/lib/server/jellyfin.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
import { JELLYFIN_URL, JELLYFIN_API_KEY, JELLYFIN_USER_ID } from '$app/env/private';
|
||||
|
||||
const CACHE_TTL = 3 * 60 * 60 * 1000;
|
||||
|
||||
interface JellyfinSong {
|
||||
Id: string;
|
||||
Name: string;
|
||||
Album?: string;
|
||||
AlbumId?: string;
|
||||
Artists?: string[];
|
||||
DateCreated?: string;
|
||||
}
|
||||
|
||||
interface PlaylistItemsResponse {
|
||||
Items: JellyfinSong[];
|
||||
TotalRecordCount: number;
|
||||
}
|
||||
|
||||
export interface LatestSong {
|
||||
id: string;
|
||||
name: string;
|
||||
artist: string;
|
||||
album: string;
|
||||
dateAdded: string | null;
|
||||
coverArt: string | null;
|
||||
}
|
||||
|
||||
interface CacheEntry<T> {
|
||||
value: T;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
const playlistCache = new Map<string, CacheEntry<LatestSong | null>>();
|
||||
|
||||
const pendingRequests = new Map<string, Promise<LatestSong | null>>();
|
||||
|
||||
async function fetchLatestPlaylistSong(
|
||||
fetch: typeof globalThis.fetch,
|
||||
playlistId: string
|
||||
): Promise<LatestSong | null> {
|
||||
const url = new URL(`${JELLYFIN_URL}/Playlists/${playlistId}/Items`);
|
||||
|
||||
url.searchParams.set('UserId', JELLYFIN_USER_ID);
|
||||
url.searchParams.set('Limit', '1');
|
||||
|
||||
const headers = {
|
||||
'X-Emby-Token': JELLYFIN_API_KEY
|
||||
};
|
||||
|
||||
const countResponse = await fetch(url, { headers });
|
||||
|
||||
if (!countResponse.ok) {
|
||||
const body = await countResponse.text();
|
||||
throw new Error(`Jellyfin error: ${countResponse.status} - ${body}`);
|
||||
}
|
||||
|
||||
const countData: PlaylistItemsResponse = await countResponse.json();
|
||||
|
||||
const total = countData.TotalRecordCount;
|
||||
|
||||
if (!total) return null;
|
||||
|
||||
url.searchParams.set('StartIndex', String(total - 1));
|
||||
|
||||
const lastResponse = await fetch(url, { headers });
|
||||
|
||||
if (!lastResponse.ok) {
|
||||
const body = await lastResponse.text();
|
||||
throw new Error(`Jellyfin error: ${lastResponse.status} - ${body}`);
|
||||
}
|
||||
|
||||
const lastData: PlaylistItemsResponse = await lastResponse.json();
|
||||
|
||||
const song = lastData.Items?.[0];
|
||||
|
||||
if (!song) return null;
|
||||
|
||||
const artworkId = song.AlbumId ?? song.Id;
|
||||
|
||||
return {
|
||||
id: song.Id,
|
||||
name: song.Name,
|
||||
artist: song.Artists?.join(', ') ?? 'Unknown artist',
|
||||
album: song.Album ?? 'Unknown album',
|
||||
dateAdded: song.DateCreated ?? null,
|
||||
coverArt: `/artwork/${encodeURIComponent(artworkId)}`
|
||||
};
|
||||
}
|
||||
|
||||
export async function getLatestPlaylistSong(
|
||||
fetch: typeof globalThis.fetch,
|
||||
playlistId: string
|
||||
): Promise<LatestSong | null> {
|
||||
const cached = playlistCache.get(playlistId);
|
||||
|
||||
if (cached && cached.expiresAt > Date.now()) {
|
||||
return cached.value;
|
||||
}
|
||||
|
||||
const pending = pendingRequests.get(playlistId);
|
||||
|
||||
if (pending) return pending;
|
||||
|
||||
const request = fetchLatestPlaylistSong(fetch, playlistId)
|
||||
.then((song) => {
|
||||
playlistCache.set(playlistId, {
|
||||
value: song,
|
||||
expiresAt: Date.now() + CACHE_TTL
|
||||
});
|
||||
|
||||
return song;
|
||||
})
|
||||
.finally(() => {
|
||||
pendingRequests.delete(playlistId);
|
||||
});
|
||||
|
||||
pendingRequests.set(playlistId, request);
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
export function invalidatePlaylistCache(playlistId: string): void {
|
||||
playlistCache.delete(playlistId);
|
||||
}
|
||||
Reference in New Issue
Block a user