-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #655 implement jellyfin media server
- Loading branch information
Showing
20 changed files
with
375 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { decryptSecret } from "@homarr/common"; | ||
import { db, eq } from "@homarr/db"; | ||
import { items } from "@homarr/db/schema/sqlite"; | ||
import { JellyfinIntegration } from "@homarr/integrations"; | ||
import { createCacheChannel } from "@homarr/redis"; | ||
|
||
import { EVERY_5_SECONDS } from "~/lib/cron-job/constants"; | ||
import { createCronJob } from "~/lib/cron-job/creator"; | ||
|
||
export const mediaServerJob = createCronJob(EVERY_5_SECONDS).withCallback(async () => { | ||
const itemsForIntegration = await db.query.items.findMany({ | ||
where: eq(items.kind, "mediaServer"), | ||
with: { | ||
integrations: { | ||
with: { | ||
integration: { | ||
with: { | ||
secrets: { | ||
columns: { | ||
kind: true, | ||
value: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
for (const itemForIntegration of itemsForIntegration) { | ||
for (const integration of itemForIntegration.integrations) { | ||
const jellyfinIntegration = new JellyfinIntegration({ | ||
...integration.integration, | ||
decryptedSecrets: integration.integration.secrets.map((secret) => ({ | ||
...secret, | ||
value: decryptSecret(secret.value), | ||
})), | ||
}); | ||
const streamSessions = await jellyfinIntegration.getCurrentSessionsAsync(); | ||
const cache = createCacheChannel(`media-server:${integration.integrationId}`); | ||
await cache.setAsync(streamSessions); | ||
await cache.publishAsync(streamSessions); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { observable } from "@trpc/server/observable"; | ||
|
||
import { createCacheChannel } from "@homarr/redis"; | ||
|
||
import type { StreamSession } from "../../../../integrations/src/interfaces/media-server/session"; | ||
import { createManyIntegrationMiddleware } from "../../middlewares/integration"; | ||
import { createTRPCRouter, publicProcedure } from "../../trpc"; | ||
|
||
export const mediaServerRouter = createTRPCRouter({ | ||
getCurrentStreams: publicProcedure | ||
.unstable_concat(createManyIntegrationMiddleware("jellyfin", "plex")) | ||
.query(async ({ ctx }) => { | ||
const data = await Promise.all( | ||
ctx.integrations.map(async (integration) => { | ||
const cache = createCacheChannel<StreamSession[]>(`media-server:${integration.id}`); | ||
return await cache.getAsync(); | ||
}), | ||
); | ||
return data; | ||
}), | ||
subcribeToCurrentStreams: publicProcedure | ||
.unstable_concat(createManyIntegrationMiddleware("jellyfin", "plex")) | ||
.subscription(({ ctx }) => { | ||
return observable<{ integrationId: string; data: StreamSession[] }>((emit) => { | ||
for (const integration of ctx.integrations) { | ||
const cache = createCacheChannel<StreamSession[]>(`media-server:${integration.id}`); | ||
void cache.subscribeAsync((sessions) => { | ||
emit.next({ | ||
integrationId: integration.id, | ||
data: sessions, | ||
}); | ||
}); | ||
} | ||
}); | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { PiHoleIntegration } from "./pi-hole/pi-hole-integration"; | ||
export { HomeAssistantIntegration } from "./homeassistant/homeassistant-integration"; | ||
export { JellyfinIntegration } from "./jellyfin/jellyfin-integration"; |
17 changes: 17 additions & 0 deletions
17
packages/integrations/src/interfaces/media-server/session.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export interface StreamSession { | ||
sessionId: string; | ||
sessionName: string; | ||
user: { | ||
userId: string; | ||
username: string; | ||
profilePictureUrl: URL | null; | ||
}; | ||
currentlyPlaying: { | ||
type: "audio" | "video" | "tv" | "movie"; | ||
name: string; | ||
seasonName: string | undefined; | ||
episodeName?: string | null; | ||
albumName?: string | null; | ||
episodeCount?: number | null; | ||
} | null; | ||
} |
57 changes: 57 additions & 0 deletions
57
packages/integrations/src/jellyfin/jellyfin-integration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Jellyfin } from "@jellyfin/sdk"; | ||
import { getSessionApi } from "@jellyfin/sdk/lib/utils/api/session-api"; | ||
|
||
import { Integration } from "../base/integration"; | ||
import type { StreamSession } from "../interfaces/media-server/session"; | ||
|
||
const jellyfin = new Jellyfin({ | ||
clientInfo: { | ||
name: "Homarr", | ||
version: "0.0.1", | ||
}, | ||
deviceInfo: { | ||
name: "Homarr", | ||
id: "homarr", | ||
}, | ||
}); | ||
|
||
export class JellyfinIntegration extends Integration { | ||
async getCurrentSessionsAsync(): Promise<StreamSession[]> { | ||
const apiKey = this.getSecretValue("apiKey"); | ||
const api = jellyfin.createApi(this.integration.url, apiKey); | ||
const sessionApi = getSessionApi(api); | ||
const sessions = await sessionApi.getSessions(); | ||
|
||
if (sessions.status !== 200) { | ||
throw new Error( | ||
`Jellyfin server ${this.integration.url} returned a non successful status code: ${sessions.status}`, | ||
); | ||
} | ||
|
||
return sessions.data.map((sessionInfo): StreamSession => { | ||
let nowPlaying: StreamSession["currentlyPlaying"] | null = null; | ||
|
||
if (sessionInfo.NowPlayingItem) { | ||
nowPlaying = { | ||
type: "tv", | ||
name: sessionInfo.NowPlayingItem.Name ?? "", | ||
seasonName: sessionInfo.NowPlayingItem.SeasonName ?? "", | ||
episodeName: sessionInfo.NowPlayingItem.EpisodeTitle, | ||
albumName: sessionInfo.NowPlayingItem.Album ?? "", | ||
episodeCount: sessionInfo.NowPlayingItem.EpisodeCount, | ||
}; | ||
} | ||
|
||
return { | ||
sessionId: `${sessionInfo.Id}`, | ||
sessionName: `${sessionInfo.Client} (${sessionInfo.DeviceName})`, | ||
user: { | ||
profilePictureUrl: null, | ||
userId: `${sessionInfo.UserId}`, | ||
username: `${sessionInfo.UserName}`, | ||
}, | ||
currentlyPlaying: nowPlaying, | ||
}; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.