-
-
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: add jellyfin integration (#672)
* feat: #655 implement jellyfin media server * fix: table overflow * feat: pr feedback * refactor: format * refactor: merge existing code * fix: code smells * refactor: format commit
- Loading branch information
Showing
25 changed files
with
435 additions
and
17 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
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,39 @@ | ||
import { observable } from "@trpc/server/observable"; | ||
|
||
import type { StreamSession } from "@homarr/integrations"; | ||
import { createItemAndIntegrationChannel } from "@homarr/redis"; | ||
|
||
import { createManyIntegrationMiddleware } from "../../middlewares/integration"; | ||
import { createTRPCRouter, publicProcedure } from "../../trpc"; | ||
|
||
export const mediaServerRouter = createTRPCRouter({ | ||
getCurrentStreams: publicProcedure | ||
.unstable_concat(createManyIntegrationMiddleware("jellyfin", "plex")) | ||
.query(async ({ ctx }) => { | ||
return await Promise.all( | ||
ctx.integrations.map(async (integration) => { | ||
const channel = createItemAndIntegrationChannel<StreamSession[]>("mediaServer", integration.id); | ||
const data = await channel.getAsync(); | ||
return { | ||
integrationId: integration.id, | ||
sessions: data?.data ?? [], | ||
}; | ||
}), | ||
); | ||
}), | ||
subscribeToCurrentStreams: publicProcedure | ||
.unstable_concat(createManyIntegrationMiddleware("jellyfin", "plex")) | ||
.subscription(({ ctx }) => { | ||
return observable<{ integrationId: string; data: StreamSession[] }>((emit) => { | ||
for (const integration of ctx.integrations) { | ||
const channel = createItemAndIntegrationChannel<StreamSession[]>("mediaServer", integration.id); | ||
void channel.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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { decryptSecret } from "@homarr/common"; | ||
import { EVERY_5_SECONDS } from "@homarr/cron-jobs-core/expressions"; | ||
import { db, eq } from "@homarr/db"; | ||
import { items } from "@homarr/db/schema/sqlite"; | ||
import { JellyfinIntegration } from "@homarr/integrations"; | ||
import { createItemAndIntegrationChannel } from "@homarr/redis"; | ||
|
||
import { createCronJob } from "../../lib"; | ||
|
||
export const mediaServerJob = createCronJob("mediaServer", 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 channel = createItemAndIntegrationChannel("mediaServer", integration.integrationId); | ||
await channel.publishAndUpdateLastStateAsync(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
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,8 +1,12 @@ | ||
// General integrations | ||
export { PiHoleIntegration } from "./pi-hole/pi-hole-integration"; | ||
export { HomeAssistantIntegration } from "./homeassistant/homeassistant-integration"; | ||
export { JellyfinIntegration } from "./jellyfin/jellyfin-integration"; | ||
export { SonarrIntegration } from "./media-organizer/sonarr/sonarr-integration"; | ||
|
||
// Types | ||
export type { StreamSession } from "./interfaces/media-server/session"; | ||
|
||
// Helpers | ||
export { IntegrationTestConnectionError } from "./base/test-connection-error"; | ||
export { integrationCreatorByKind } from "./base/creator"; |
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: string | null; | ||
}; | ||
currentlyPlaying: { | ||
type: "audio" | "video" | "tv" | "movie"; | ||
name: string; | ||
seasonName: string | undefined; | ||
episodeName?: string | null; | ||
albumName?: string | null; | ||
episodeCount?: number | null; | ||
} | null; | ||
} |
68 changes: 68 additions & 0 deletions
68
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,68 @@ | ||
import { Jellyfin } from "@jellyfin/sdk"; | ||
import { getSessionApi } from "@jellyfin/sdk/lib/utils/api/session-api"; | ||
import { getSystemApi } from "@jellyfin/sdk/lib/utils/api/system-api"; | ||
|
||
import { Integration } from "../base/integration"; | ||
import type { StreamSession } from "../interfaces/media-server/session"; | ||
|
||
export class JellyfinIntegration extends Integration { | ||
private readonly jellyfin: Jellyfin = new Jellyfin({ | ||
clientInfo: { | ||
name: "Homarr", | ||
version: "0.0.1", | ||
}, | ||
deviceInfo: { | ||
name: "Homarr", | ||
id: "homarr", | ||
}, | ||
}); | ||
|
||
public async testConnectionAsync(): Promise<void> { | ||
const api = this.getApi(); | ||
const systemApi = getSystemApi(api); | ||
await systemApi.getPingSystem(); | ||
} | ||
|
||
public async getCurrentSessionsAsync(): Promise<StreamSession[]> { | ||
const api = this.getApi(); | ||
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: `${this.integration.url}/Users/${sessionInfo.UserId}/Images/Primary`, | ||
userId: sessionInfo.UserId ?? "", | ||
username: sessionInfo.UserName ?? "", | ||
}, | ||
currentlyPlaying: nowPlaying, | ||
}; | ||
}); | ||
} | ||
|
||
private getApi() { | ||
const apiKey = this.getSecretValue("apiKey"); | ||
return this.jellyfin.createApi(this.integration.url, apiKey); | ||
} | ||
} |
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.