Files
Syco 89fd54b3dc Gitea Test
First Push to giTea
2026-05-14 10:41:14 +02:00

59 lines
2.0 KiB
TypeScript

import { Router } from 'express'
import axios from 'axios'
const router = Router()
router.get('/status', async (_req, res) => {
const host = process.env.JELLYFIN_HOST
const key = process.env.JELLYFIN_API_KEY
if (!host || !key) return res.status(503).json({ error: 'Jellyfin not configured' })
const headers = { 'X-Emby-Token': key }
try {
const apiKey = { api_key: key }
const [sessionsRes, countsRes] = await Promise.all([
axios.get(`${host}/Sessions`, { headers, params: { ...apiKey, ActiveWithinSeconds: 180 } }),
axios.get(`${host}/Items/Counts`, { headers, params: apiKey }),
])
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const sessions = (sessionsRes.data as any[])
.filter((s: any) => s.NowPlayingItem)
.map((s: any) => {
const item = s.NowPlayingItem
const isEpisode = item?.Type === 'Episode'
const title = isEpisode ? (item?.SeriesName ?? item?.Name ?? '') : (item?.Name ?? '')
const subtitle = isEpisode
? `S${String(item?.ParentIndexNumber ?? 0).padStart(2,'0')}E${String(item?.IndexNumber ?? 0).padStart(2,'0')} · ${item?.Name ?? ''}`
: null
return {
user: s.UserName ?? 'Unknown',
title,
subtitle,
type: item?.Type ?? '',
progress: s.PlayState?.PositionTicks && item?.RunTimeTicks
? Math.round((s.PlayState.PositionTicks / item.RunTimeTicks) * 100)
: null,
paused: s.PlayState?.IsPaused ?? false,
client: s.Client ?? '',
}
})
const counts = countsRes.data as any
res.json({
sessions,
library: {
movies: counts.MovieCount ?? 0,
episodes: counts.EpisodeCount ?? 0,
songs: counts.SongCount ?? 0,
albums: counts.AlbumCount ?? 0,
},
})
} catch (err: any) {
res.status(502).json({ error: err.message ?? 'Jellyfin error' })
}
})
export default router