Skip to content

Commit

Permalink
feat: add jellyfin support (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin authored Sep 14, 2024
1 parent 29e42a5 commit 1f23d2a
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions extension/src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"pages/viki-page.js",
"pages/unext-page.js",
"pages/emby-page.js",
"pages/jellyfin-page.js",
"pages/osnplus-page.js",
"pages/bilibili-page.js",
"pages/nrk-tv-page.js",
Expand Down
8 changes: 8 additions & 0 deletions extension/src/pages.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@
"enabled": true
}
},
{
"host": "jellyfin*|:8096*",
"script": "jellyfin-page.js",
"hash": "#/video",
"autoSync": {
"enabled": true
}
},
{
"host": "emby*|:8096*",
"script": "emby-page.js",
Expand Down
80 changes: 80 additions & 0 deletions extension/src/pages/jellyfin-page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { VideoDataSubtitleTrack } from '@project/common';
import { VideoData } from '@project/common';
import { trackFromDef } from './util';

declare const ApiClient: any | undefined;

document.addEventListener(
'asbplayer-get-synced-data',
async () => {
const response: VideoData = { error: '', basename: '', subtitles: [] };
if (!ApiClient) {
response.error = 'ApiClient is undefined';
return document.dispatchEvent(
new CustomEvent('asbplayer-synced-data', {
detail: response,
}),
);
}

const deviceID = ApiClient?._deviceId;
const apikey = ApiClient?._serverInfo.AccessToken;

let session;
for (let attempt = 0; attempt < 5; attempt++) {
const sessions = await ApiClient.getSessions({ deviceId: deviceID });
session = sessions[0];
if (session.PlayState.MediaSourceId) {
break;
}
await new Promise((resolve) => setTimeout(resolve, 1000));
}

if (!session || !session.PlayState.MediaSourceId) {
response.error = 'Failed to retrieve a valid MediaSourceId after 5 attempts';
return document.dispatchEvent(
new CustomEvent('asbplayer-synced-data', {
detail: response,
}),
);
}

const mediaID = session.PlayState.MediaSourceId;
const nowPlayingItem = session.NowPlayingItem;
response.basename = nowPlayingItem.Name;

const subtitles: VideoDataSubtitleTrack[] = [];
nowPlayingItem.MediaStreams.filter(
(stream: { IsTextSubtitleStream: any }) => stream.IsTextSubtitleStream,
).forEach((sub: { DisplayTitle: any; Language: any; Index: number; Codec: string; Path: string }) => {
const extension = sub.Path ? sub.Path.split('.').pop() : sub.Codec;
var url =
'/Videos/' +
nowPlayingItem.Id +
'/' +
mediaID +
'/Subtitles/' +
sub.Index +
'/0/Stream.' +
extension +
'?api_key=' +
apikey;
subtitles.push(
trackFromDef({
label: sub.DisplayTitle,
language: sub.Language || '',
url: url,
extension,
}),
);
});
response.subtitles = subtitles;

document.dispatchEvent(
new CustomEvent('asbplayer-synced-data', {
detail: response,
}),
);
},
false,
);

0 comments on commit 1f23d2a

Please sign in to comment.