stremiok 2.2.9 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +1 -0
- package/config.xml +1 -1
- package/css/style.css +10 -2
- package/index.html +39 -37
- package/js/jellyfin.js +642 -0
- package/js/player-audio.js +61 -4
- package/js/player-controls.js +29 -1
- package/js/player-core.js +28 -4
- package/js/plex.js +530 -0
- package/js/stremio.js +42 -5
- package/js/util.js +21 -2
- package/js/view-core.js +3 -1
- package/js/view-detail.js +3 -3
- package/js/view-home.js +2 -0
- package/js/view-settings.js +172 -1
- package/package.json +1 -1
package/js/jellyfin.js
ADDED
|
@@ -0,0 +1,642 @@
|
|
|
1
|
+
/* Jellyfin and Emby servers on your network. Global namespace: Jellyfin
|
|
2
|
+
- Added in Settings › Content › Media servers: its address, then (Jellyfin) Quick Connect, a
|
|
3
|
+
code to enter in Jellyfin, or a user name and password. What's kept: its address, kind, name
|
|
4
|
+
and version, who's signed in and the sign-in it gave back (never the password).
|
|
5
|
+
- Emby speaks the same API family (Jellyfin started as its fork): its paths under /emby, per
|
|
6
|
+
user (/Users/<id>/Items…), its sign-in line as X-Emby-Authorization, api_key in links; no
|
|
7
|
+
Quick Connect, trickplay or media segments, but intro and credits markers in its chapters
|
|
8
|
+
(KINDS: what differs).
|
|
9
|
+
- As local addons like Xtream's, one per kind ("Jellyfin", "Emby"): a catalog per Movies or
|
|
10
|
+
Shows library (Home rows, Discover, Search); a title the server knows the IMDb id of is a
|
|
11
|
+
card with that id (the usual title page, your addons' sources too), any other
|
|
12
|
+
jfm.<server>.<item> (a film) or jfs.<server>.<series> (its episodes …:<season>:<episode>),
|
|
13
|
+
its details from the server.
|
|
14
|
+
- On titles from your addons (tt…, tmdb:…), the server's copies as sources: each server's list
|
|
15
|
+
of films and series with their ids, read when first needed and again after INDEX_FRESH_MS.
|
|
16
|
+
- Played as the file is (/Videos/<item>/stream.<container>?static=true: nothing converted),
|
|
17
|
+
its subtitle files with it; what you play is reported back (Jellyfin's sessions: where you
|
|
18
|
+
are, stopped, played), except during private viewing. Jellyfin 10.9 or newer, Emby 4.7. */
|
|
19
|
+
var Jellyfin = (function () {
|
|
20
|
+
'use strict';
|
|
21
|
+
|
|
22
|
+
var KEY = 'stremiok.jellyfin.servers', DEV_KEY = 'stremiok.jellyfin.device', LIBS_KEY = 'stremiok.jellyfin.libs';
|
|
23
|
+
var PAGE = 100, INDEX_FRESH_MS = 30 * 60000, INDEX_RETRY_MS = 60000, LIBS_FRESH_MS = 86400000;
|
|
24
|
+
var QC_EVERY = 3000, QC_FOR = 10 * 60000, REPORT_EVERY = 60000, TICKS = 10000000;
|
|
25
|
+
var _changeFns = [];
|
|
26
|
+
function onChange(fn) { _changeFns.push(fn); }
|
|
27
|
+
function changed() { _changeFns.forEach(function (fn) { try { fn(); } catch (e) {} }); }
|
|
28
|
+
function enc(v) { return encodeURIComponent('' + v); }
|
|
29
|
+
function rand(n) { return U.randomHex(n); } // (the TV's secure random numbers)
|
|
30
|
+
function none() { return []; }
|
|
31
|
+
function flat(lists) { return [].concat.apply([], lists || []); }
|
|
32
|
+
|
|
33
|
+
// ---- What differs between Jellyfin and Emby (u: the signed-in user's id, encoded) ----
|
|
34
|
+
var KINDS = {
|
|
35
|
+
jellyfin: { name: 'Jellyfin', prefix: '', header: 'Authorization', key: 'ApiKey', fill: 'fillHeight=450',
|
|
36
|
+
views: function (u) { return '/UserViews?userId=' + u; }, items: function (u) { return '/Items?userId=' + u + '&'; },
|
|
37
|
+
item: function (u, id) { return '/Items/' + id + '?userId=' + u; }, resume: function (u) { return '/UserItems/Resume?userId=' + u + '&'; },
|
|
38
|
+
played: function (u, id) { return '/UserPlayedItems/' + id + '?userId=' + u; }, sub: '/0/Stream.srt' },
|
|
39
|
+
emby: { name: 'Emby', prefix: '/emby', header: 'X-Emby-Authorization', key: 'api_key', fill: 'maxHeight=450',
|
|
40
|
+
views: function (u) { return '/Users/' + u + '/Views'; }, items: function (u) { return '/Users/' + u + '/Items?'; },
|
|
41
|
+
item: function (u, id) { return '/Users/' + u + '/Items/' + id + '?Fields=Chapters,MediaSources,MediaStreams,ProviderIds,People,Genres,Overview'; },
|
|
42
|
+
resume: function (u) { return '/Users/' + u + '/Items/Resume?'; }, played: function (u, id) { return '/Users/' + u + '/PlayedItems/' + id; }, sub: '/Stream.srt' }
|
|
43
|
+
};
|
|
44
|
+
function kindOf(srv) { return srv && srv.kind === 'emby' ? 'emby' : 'jellyfin'; } // (servers kept before Emby: Jellyfin)
|
|
45
|
+
function K(srv) { return KINDS[kindOf(srv)]; }
|
|
46
|
+
function api(srv) { return srv.url + K(srv).prefix; }
|
|
47
|
+
function uid(srv) { return enc(srv.userId); }
|
|
48
|
+
|
|
49
|
+
// ---- Your servers ----
|
|
50
|
+
function servers(kind) {
|
|
51
|
+
var l = U.store.get(KEY, []);
|
|
52
|
+
return Array.isArray(l) ? l.filter(function (s) { return s && s.id && s.url && s.token && s.userId && (!kind || kindOf(s) === kind); }) : [];
|
|
53
|
+
}
|
|
54
|
+
function server(id) { return servers().filter(function (s) { return s.id === id; })[0] || null; }
|
|
55
|
+
function isEnabled(kind) { return servers(kind).length > 0; }
|
|
56
|
+
function kindsOn() { return Object.keys(KINDS).filter(function (k) { return isEnabled(k); }); }
|
|
57
|
+
function deviceId() {
|
|
58
|
+
var d = U.store.get(DEV_KEY, '');
|
|
59
|
+
if (!/^[0-9a-f]{16,40}$/.test('' + d)) { d = rand(32); U.store.set(DEV_KEY, d); }
|
|
60
|
+
return d;
|
|
61
|
+
}
|
|
62
|
+
function version() { // (StremioK's, from the ?v= build.sh stamps on the script tags)
|
|
63
|
+
try {
|
|
64
|
+
var tag = document.querySelector('script[src*="jellyfin.js"]');
|
|
65
|
+
var m = tag && /[?&]v=([0-9][0-9.]*)/.exec(tag.getAttribute('src') || '');
|
|
66
|
+
if (m) { return m[1]; }
|
|
67
|
+
} catch (e) {}
|
|
68
|
+
return '2';
|
|
69
|
+
}
|
|
70
|
+
// The server's own sign-in line: who's asking (StremioK, this TV) and, signed in, its token;
|
|
71
|
+
// in the kind's header (s: a server, or what probe found).
|
|
72
|
+
function auth(token) {
|
|
73
|
+
return 'MediaBrowser Client="StremioK", Device="Samsung TV", DeviceId="' + deviceId() + '", Version="' + version() + '"' + (token ? ', Token="' + token + '"' : '');
|
|
74
|
+
}
|
|
75
|
+
function headers(s, token, json) {
|
|
76
|
+
var h = {};
|
|
77
|
+
h[K(s).header] = auth(token);
|
|
78
|
+
if (json) { h['Content-Type'] = 'application/json'; }
|
|
79
|
+
return h;
|
|
80
|
+
}
|
|
81
|
+
function get(srv, path, max) { return U.getJSON(api(srv) + path, 20000, max || 4 * 1048576, null, headers(srv, srv.token)); }
|
|
82
|
+
function post(srv, path, body) { return U.getText(api(srv) + path, 15000, 65536, null, true, headers(srv, srv.token, true), body == null ? '' : JSON.stringify(body)); }
|
|
83
|
+
|
|
84
|
+
// An address as typed (192.168.0.50, http://nas:8096/web/…, …/emby/web/…): with http:// and
|
|
85
|
+
// the usual port (8096, both's) when it has none, nothing after the host.
|
|
86
|
+
function normAddress(v) {
|
|
87
|
+
var s = ('' + (v || '')).trim().replace(/#.*$/, '').replace(/\/web(\/.*)?$/i, '').replace(/\/+$/, '').replace(/\/emby$/i, '');
|
|
88
|
+
if (s && !/^[a-z][a-z0-9+.-]*:\/\//i.test(s)) { s = 'http://' + s; }
|
|
89
|
+
if (/^http:\/\/[^\/:@]+$/i.test(s)) { s += ':8096'; }
|
|
90
|
+
return s;
|
|
91
|
+
}
|
|
92
|
+
function addressProblem(v) {
|
|
93
|
+
var s = normAddress(v), p = /^https?:\/\//i.test(s) ? U.parseUrl(s) : null;
|
|
94
|
+
return p && p.host ? null : 'An address like http://192.168.0.50:8096';
|
|
95
|
+
}
|
|
96
|
+
// What answers there: a Jellyfin (10.9 or newer) or Emby (4.7 or newer) server, its kind, name
|
|
97
|
+
// and version. Asked at the address, then under /emby (an Emby that answers only there).
|
|
98
|
+
function probe(address) {
|
|
99
|
+
var url = normAddress(address);
|
|
100
|
+
function info() { return U.getJSON(url + '/System/Info/Public', 10000, 65536).then(null, function (e) { return U.getJSON(url + '/emby/System/Info/Public', 10000, 65536).then(null, function () { throw e; }); }); }
|
|
101
|
+
return info().then(function (r) {
|
|
102
|
+
if (!r || typeof r !== 'object' || !r.Id || !r.Version) { throw new Error('That address doesn’t answer as a Jellyfin or Emby server.'); }
|
|
103
|
+
// (Jellyfin always names itself, "Jellyfin Server"; Emby usually sends no ProductName at
|
|
104
|
+
// all: one that doesn't say Jellyfin is Emby when it says so or its version is Emby's, 4.x)
|
|
105
|
+
var v = ('' + r.Version).split('.').map(function (x) { return parseInt(x, 10) || 0; }), pn = '' + (r.ProductName || '');
|
|
106
|
+
var kind = /jellyfin/i.test(pn) ? 'jellyfin' : (/emby/i.test(pn) || v[0] < 10) ? 'emby' : 'jellyfin';
|
|
107
|
+
if (kind === 'jellyfin' && !(v[0] > 10 || (v[0] === 10 && v[1] >= 9))) { throw new Error('Jellyfin ' + r.Version + ' is too old: 10.9 or newer is needed.'); }
|
|
108
|
+
if (kind === 'emby' && !(v[0] > 4 || (v[0] === 4 && v[1] >= 7))) { throw new Error('Emby ' + r.Version + ' is too old: 4.7 or newer is needed.'); }
|
|
109
|
+
return { url: url, kind: kind, serverId: '' + r.Id, name: ('' + (r.ServerName || KINDS[kind].name)).replace(/\s+/g, ' ').trim().slice(0, 60) || KINDS[kind].name, version: ('' + r.Version).slice(0, 20) };
|
|
110
|
+
}, function (e) { throw new Error('No answer from ' + url + ' (' + U.problem(e) + ').'); });
|
|
111
|
+
}
|
|
112
|
+
function kindName(s) { return K(s).name; }
|
|
113
|
+
// Signed in: kept (the same server and user again: in its place, its id kept).
|
|
114
|
+
function added(info, r) {
|
|
115
|
+
if (!r || !r.AccessToken || !r.User || !r.User.Id) { throw new Error(kindName(info) + ' didn’t sign in.'); }
|
|
116
|
+
var list = servers(), old = list.filter(function (s) { return s.serverId === info.serverId && s.userId === '' + r.User.Id; })[0];
|
|
117
|
+
var srv = { id: old ? old.id : 's' + rand(8), url: info.url, kind: kindOf(info), serverId: info.serverId, name: info.name, version: info.version,
|
|
118
|
+
user: ('' + (r.User.Name || '')).slice(0, 60), userId: '' + r.User.Id, token: '' + r.AccessToken, at: Date.now() };
|
|
119
|
+
list = list.filter(function (s) { return s !== old; });
|
|
120
|
+
list.push(srv);
|
|
121
|
+
U.store.set(KEY, list);
|
|
122
|
+
forget(srv.id);
|
|
123
|
+
_libsP[srv.id] = refreshLibs(true, srv.id);
|
|
124
|
+
changed();
|
|
125
|
+
U.log('jellyfin', 'Signed in to “' + srv.name + '” (' + kindName(srv) + ' ' + srv.version + ') as ' + srv.user);
|
|
126
|
+
return srv;
|
|
127
|
+
}
|
|
128
|
+
// Signed in, once its libraries are read (or that failed): Settings shows them at once.
|
|
129
|
+
var _libsP = {};
|
|
130
|
+
function withLibs(srv) { return (_libsP[srv.id] || Promise.resolve()).then(function () { return srv; }); }
|
|
131
|
+
function signIn(info, user, pass) {
|
|
132
|
+
return U.getJSON(api(info) + '/Users/AuthenticateByName', 15000, 1048576, null, headers(info, '', true), JSON.stringify({ Username: '' + user, Pw: '' + pass })).then(function (r) {
|
|
133
|
+
return withLibs(added(info, r));
|
|
134
|
+
}, function (e) {
|
|
135
|
+
throw new Error(/HTTP 401/.test(e && e.message) ? 'Wrong user name or password.' : 'Couldn’t sign in (' + U.problem(e) + ').');
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
// Quick Connect: a code for you to enter in Jellyfin, then asked every QC_EVERY whether it
|
|
139
|
+
// was (for QC_FOR). cb: onCode(code), onSuccess(server), onError(message). Returns a stop.
|
|
140
|
+
function quickConnect(info, cb) {
|
|
141
|
+
var stopped = false, timer = null, until = Date.now() + QC_FOR;
|
|
142
|
+
function fail(msg) { if (!stopped) { stopped = true; cb.onError(msg); } }
|
|
143
|
+
U.getJSON(info.url + '/QuickConnect/Enabled', 10000, 4096, null, headers(info, ''), null, true).then(function (on) {
|
|
144
|
+
if (stopped) { return; }
|
|
145
|
+
if (on !== true) { fail('Quick Connect is off on this server: turn it on in Jellyfin (Dashboard › General), or sign in with a password.'); return; }
|
|
146
|
+
return U.getJSON(info.url + '/QuickConnect/Initiate', 10000, 65536, null, headers(info, '', true), '').then(function (r) {
|
|
147
|
+
if (stopped) { return; }
|
|
148
|
+
if (!r || !r.Secret || !r.Code) { fail('Jellyfin didn’t give a code.'); return; }
|
|
149
|
+
cb.onCode('' + r.Code);
|
|
150
|
+
function signedIn() {
|
|
151
|
+
U.getJSON(info.url + '/Users/AuthenticateWithQuickConnect', 15000, 1048576, null, headers(info, '', true), JSON.stringify({ Secret: r.Secret })).then(function (a) {
|
|
152
|
+
if (stopped) { return; }
|
|
153
|
+
return withLibs(added(info, a)).then(function (srv) {
|
|
154
|
+
if (stopped) { return; }
|
|
155
|
+
stopped = true;
|
|
156
|
+
cb.onSuccess(srv);
|
|
157
|
+
});
|
|
158
|
+
}).then(null, function (e) { fail('Couldn’t sign in (' + U.problem(e) + ').'); });
|
|
159
|
+
}
|
|
160
|
+
function poll() {
|
|
161
|
+
timer = setTimeout(function () {
|
|
162
|
+
if (stopped) { return; }
|
|
163
|
+
if (Date.now() > until) { fail('The code expired: try again.'); return; }
|
|
164
|
+
U.getJSON(info.url + '/QuickConnect/Connect?secret=' + enc(r.Secret), 10000, 65536, null, headers(info, ''), null, true).then(function (s) {
|
|
165
|
+
if (stopped) { return; }
|
|
166
|
+
if (s && s.Authenticated) { signedIn(); } else { poll(); }
|
|
167
|
+
}, function (e) {
|
|
168
|
+
if (stopped) { return; }
|
|
169
|
+
if (/HTTP 404/.test(e && e.message)) { fail('The code expired: try again.'); } else { poll(); }
|
|
170
|
+
});
|
|
171
|
+
}, QC_EVERY);
|
|
172
|
+
}
|
|
173
|
+
poll();
|
|
174
|
+
});
|
|
175
|
+
}).then(null, function (e) { fail('Couldn’t start Quick Connect (' + U.problem(e) + ').'); });
|
|
176
|
+
return function () { stopped = true; clearTimeout(timer); };
|
|
177
|
+
}
|
|
178
|
+
// Sign out and remove: its sign-in ended on the server too (if it answers).
|
|
179
|
+
function remove(id) {
|
|
180
|
+
var srv = server(id);
|
|
181
|
+
if (!srv) { return; }
|
|
182
|
+
post(srv, '/Sessions/Logout', null).then(null, function () {});
|
|
183
|
+
U.store.set(KEY, servers().filter(function (s) { return s.id !== id; }));
|
|
184
|
+
var libs = libsAll();
|
|
185
|
+
delete libs[id];
|
|
186
|
+
U.store.set(LIBS_KEY, libs);
|
|
187
|
+
forget(id);
|
|
188
|
+
fillAll();
|
|
189
|
+
changed();
|
|
190
|
+
}
|
|
191
|
+
// Reload: its libraries and its list of titles read again now.
|
|
192
|
+
function reload(id) {
|
|
193
|
+
if (!server(id)) { return; }
|
|
194
|
+
forget(id);
|
|
195
|
+
delete _libsAsked[id];
|
|
196
|
+
refreshLibs(true, id);
|
|
197
|
+
}
|
|
198
|
+
function forget(id) {
|
|
199
|
+
delete _index[id];
|
|
200
|
+
_items.clear(); _eps.clear();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// ---- Libraries: the catalogs, kept a day (U.store) so Home has its rows at launch ----
|
|
204
|
+
var _libs = null, _libsAsked = {};
|
|
205
|
+
function libsAll() {
|
|
206
|
+
if (!_libs) { var l = U.store.get(LIBS_KEY, {}); _libs = l && typeof l === 'object' && !Array.isArray(l) ? l : {}; }
|
|
207
|
+
return _libs;
|
|
208
|
+
}
|
|
209
|
+
function libsOf(srv) { var l = libsAll()[srv.id]; return l && Array.isArray(l.list) ? l.list : []; }
|
|
210
|
+
// (a promise: done when they're read, or that failed)
|
|
211
|
+
function refreshLibs(force, only) {
|
|
212
|
+
return Promise.all(servers().map(function (srv) {
|
|
213
|
+
if (only && srv.id !== only) { return null; }
|
|
214
|
+
var l = libsAll()[srv.id];
|
|
215
|
+
if (!force && ((l && Date.now() - l.at < LIBS_FRESH_MS) || _libsAsked[srv.id])) { return null; }
|
|
216
|
+
_libsAsked[srv.id] = true; // (once a launch, unless asked)
|
|
217
|
+
return get(srv, K(srv).views(uid(srv))).then(function (r) {
|
|
218
|
+
var list = U.listOf(r && r.Items, 50, function (x) { return x.Id && (x.CollectionType === 'movies' || x.CollectionType === 'tvshows'); }).map(function (x) {
|
|
219
|
+
return { id: '' + x.Id, name: ('' + (x.Name || '')).replace(/\s+/g, ' ').trim().slice(0, 60) || (x.CollectionType === 'movies' ? 'Movies' : 'Shows'), type: x.CollectionType === 'movies' ? 'movie' : 'series' };
|
|
220
|
+
});
|
|
221
|
+
if (!server(srv.id)) { return; } // (removed meanwhile)
|
|
222
|
+
var all = libsAll(), keep = {}, before = JSON.stringify(all[srv.id] && all[srv.id].list);
|
|
223
|
+
servers().forEach(function (s) { if (all[s.id]) { keep[s.id] = all[s.id]; } });
|
|
224
|
+
keep[srv.id] = { at: Date.now(), list: list };
|
|
225
|
+
_libs = keep;
|
|
226
|
+
U.store.set(LIBS_KEY, keep);
|
|
227
|
+
fillAll();
|
|
228
|
+
if (JSON.stringify(list) !== before || force) { changed(); }
|
|
229
|
+
}, function (e) { U.log('jellyfin', '“' + srv.name + '”: its libraries couldn’t be read (' + U.problem(e) + ')'); });
|
|
230
|
+
}));
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// ---- The addons: one per kind ("Jellyfin", "Emby": its rows say so, Source ranking lists it) ----
|
|
234
|
+
var _addons = {};
|
|
235
|
+
var RESOURCES = ['catalog', { name: 'stream', types: ['movie', 'series'], idPrefixes: ['tt', 'tmdb:'] }];
|
|
236
|
+
function catalogsNow(kind) {
|
|
237
|
+
var out = [], many = servers(kind).length > 1;
|
|
238
|
+
servers(kind).forEach(function (srv) {
|
|
239
|
+
var libs = libsOf(srv), on = many ? ' · ' + srv.name : ''; // (Home adds “Jellyfin” / “Emby”)
|
|
240
|
+
if (!libs.length) { return; } // (its libraries not read yet, or none)
|
|
241
|
+
if (libs.some(function (l) { return l.type === 'movie'; })) { out.push({ type: 'movie', id: 'jf.' + srv.id + '.resume', name: 'Continue watching' + on, extra: [] }); }
|
|
242
|
+
if (libs.some(function (l) { return l.type === 'series'; })) { out.push({ type: 'series', id: 'jf.' + srv.id + '.nextup', name: 'Next up' + on, extra: [] }); }
|
|
243
|
+
libs.forEach(function (lib) {
|
|
244
|
+
out.push({ type: lib.type, id: 'jf.' + srv.id + '.' + lib.id, name: lib.name + (many ? ' · ' + srv.name : ''), extra: [{ name: 'search' }, { name: 'skip' }] });
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
return out;
|
|
248
|
+
}
|
|
249
|
+
function fillCatalogs(kind) { // (changed in place: the loaded addon holds the same list)
|
|
250
|
+
var a = _addons[kind];
|
|
251
|
+
if (!a) { return; }
|
|
252
|
+
var l = a.manifest.catalogs;
|
|
253
|
+
l.splice.apply(l, [0, l.length].concat(catalogsNow(kind)));
|
|
254
|
+
a.manifest.resources = isEnabled(kind) ? RESOURCES : ['catalog'];
|
|
255
|
+
}
|
|
256
|
+
function fillAll() { Object.keys(KINDS).forEach(fillCatalogs); }
|
|
257
|
+
function synthAddon(kind) {
|
|
258
|
+
kind = KINDS[kind] ? kind : 'jellyfin';
|
|
259
|
+
if (!_addons[kind]) {
|
|
260
|
+
_addons[kind] = { __local: true, __sources: true, url: 'local:' + kind, base: 'local:' + kind,
|
|
261
|
+
__impl: { catalog: catalog, meta: meta, streams: function (type, id) { return streams(type, id, kind); } },
|
|
262
|
+
manifest: { id: 'org.stremiok.' + kind, name: KINDS[kind].name, version: '1.0.0', types: ['movie', 'series'], resources: RESOURCES, catalogs: [] } };
|
|
263
|
+
}
|
|
264
|
+
fillCatalogs(kind);
|
|
265
|
+
refreshLibs(false);
|
|
266
|
+
return _addons[kind];
|
|
267
|
+
}
|
|
268
|
+
// The addons of the kinds you have a server of, Jellyfin first.
|
|
269
|
+
function synthAddons() { return kindsOn().map(synthAddon); }
|
|
270
|
+
|
|
271
|
+
// ---- Titles ----
|
|
272
|
+
function imdbOf(it) { var p = (it && it.ProviderIds) || {}, v = '' + (p.Imdb || p.IMDB || p.imdb || ''); return /^tt\d{5,10}$/.test(v) ? v : ''; }
|
|
273
|
+
function tmdbOf(it) { var p = (it && it.ProviderIds) || {}, v = '' + (p.Tmdb || p.TMDB || p.tmdb || ''); return /^\d{1,10}$/.test(v) ? v : ''; }
|
|
274
|
+
function img(srv, id, kind, tag, size) {
|
|
275
|
+
return api(srv) + '/Items/' + enc(id) + '/Images/' + kind + '?' + (kind === 'Backdrop' ? 'maxWidth=1280' : size || K(srv).fill) + '&quality=90' + (tag ? '&tag=' + enc(tag) : '');
|
|
276
|
+
}
|
|
277
|
+
function cardOf(srv, type, it) {
|
|
278
|
+
if (!it || !it.Id || !it.Name) { return null; }
|
|
279
|
+
var tt = imdbOf(it);
|
|
280
|
+
var m = { id: tt || ((type === 'movie' ? 'jfm.' : 'jfs.') + srv.id + '.' + it.Id), type: type, name: ('' + it.Name).replace(/\s+/g, ' ').trim(), posterShape: 'poster' };
|
|
281
|
+
if (it.ImageTags && it.ImageTags.Primary) { m.poster = img(srv, it.Id, 'Primary', it.ImageTags.Primary); }
|
|
282
|
+
if (Array.isArray(it.BackdropImageTags) && it.BackdropImageTags[0]) { m.background = img(srv, it.Id, 'Backdrop', it.BackdropImageTags[0]); }
|
|
283
|
+
if (it.ProductionYear > 1880) { m.releaseInfo = '' + it.ProductionYear; }
|
|
284
|
+
var r = parseFloat(it.CommunityRating);
|
|
285
|
+
if (r > 0 && r <= 10) { m.imdbRating = r.toFixed(1); }
|
|
286
|
+
return m;
|
|
287
|
+
}
|
|
288
|
+
var CARD_FIELDS = '&fields=ProviderIds&enableImageTypes=Primary,Backdrop&imageTypeLimit=1';
|
|
289
|
+
// Jellyfin's Continue watching (films part-watched) and Next up (a show's next episode, or
|
|
290
|
+
// the one part-watched): a show's card opens at that episode (_openEp).
|
|
291
|
+
function resumeCards(srv) {
|
|
292
|
+
return get(srv, K(srv).resume(uid(srv)) + 'includeItemTypes=Movie&limit=50' + CARD_FIELDS + '&enableTotalRecordCount=false').then(function (r) {
|
|
293
|
+
return U.listOf(r && r.Items, 50).map(function (it) { return cardOf(srv, 'movie', it); }).filter(Boolean);
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
function nextUpCards(srv) {
|
|
297
|
+
return Promise.all([get(srv, '/Shows/NextUp?userId=' + uid(srv) + '&limit=50' + CARD_FIELDS + '&enableTotalRecordCount=false'), index(srv)]).then(function (r) {
|
|
298
|
+
var seen = {};
|
|
299
|
+
return U.listOf(r[0] && r[0].Items, 50, function (e) { return e.SeriesId && e.ParentIndexNumber >= 0 && e.IndexNumber >= 0; }).map(function (e) {
|
|
300
|
+
var sid = '' + e.SeriesId, show = (r[1].__ids || {})[sid] || ('jfs.' + srv.id + '.' + sid);
|
|
301
|
+
if (seen[show]) { return null; }
|
|
302
|
+
seen[show] = 1;
|
|
303
|
+
var m = { id: show, type: 'series', name: ('' + (e.SeriesName || e.Name || '')).replace(/\s+/g, ' ').trim(), posterShape: 'poster',
|
|
304
|
+
_openEp: show + ':' + e.ParentIndexNumber + ':' + e.IndexNumber, _ep: 'S' + e.ParentIndexNumber + 'E' + e.IndexNumber };
|
|
305
|
+
if (e.SeriesPrimaryImageTag) { m.poster = img(srv, sid, 'Primary', e.SeriesPrimaryImageTag); }
|
|
306
|
+
if (Array.isArray(e.ParentBackdropImageTags) && e.ParentBackdropImageTags[0]) { m.background = img(srv, e.ParentBackdropItemId || sid, 'Backdrop', e.ParentBackdropImageTags[0]); }
|
|
307
|
+
return m.name ? m : null;
|
|
308
|
+
}).filter(Boolean);
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
function catalog(catId, extra) {
|
|
312
|
+
var m = /^jf\.([^.]+)\.([^.]+)$/.exec(catId || ''), srv = m && server(m[1]);
|
|
313
|
+
if (srv && m[2] === 'resume') { return resumeCards(srv); }
|
|
314
|
+
if (srv && m[2] === 'nextup') { return nextUpCards(srv); }
|
|
315
|
+
var lib = srv && libsOf(srv).filter(function (l) { return l.id === m[2]; })[0];
|
|
316
|
+
if (!lib) { return Promise.resolve([]); }
|
|
317
|
+
var skip = extra && extra.skip ? (parseInt(extra.skip, 10) || 0) : 0, term = extra && extra.search ? ('' + extra.search).slice(0, 120) : '';
|
|
318
|
+
var path = K(srv).items(uid(srv)) + 'parentId=' + enc(lib.id) + '&recursive=true&includeItemTypes=' + (lib.type === 'movie' ? 'Movie' : 'Series') +
|
|
319
|
+
CARD_FIELDS + '&enableUserData=false&enableTotalRecordCount=false&startIndex=' + skip + '&limit=' + PAGE +
|
|
320
|
+
(term ? '&searchTerm=' + enc(term) : '&sortBy=DateCreated,SortName&sortOrder=Descending');
|
|
321
|
+
return get(srv, path).then(function (r) {
|
|
322
|
+
return U.listOf(r && r.Items, PAGE).map(function (it) { return cardOf(srv, lib.type, it); }).filter(Boolean);
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
var _items = U.lru(40), _eps = U.lru(20);
|
|
326
|
+
function kept(cache, k, make) {
|
|
327
|
+
var p = cache.get(k);
|
|
328
|
+
if (p) { return p; }
|
|
329
|
+
p = make();
|
|
330
|
+
cache.set(k, p);
|
|
331
|
+
p.then(null, function () { if (cache.get(k) === p) { cache.set(k, null); } });
|
|
332
|
+
return p;
|
|
333
|
+
}
|
|
334
|
+
function item(srv, id) { return kept(_items, srv.id + '|' + id, function () { return get(srv, K(srv).item(uid(srv), enc(id))); }); }
|
|
335
|
+
// A show's episodes on the server (not the missing ones Jellyfin may list): [{ season, episode, it }].
|
|
336
|
+
function episodes(srv, sid) {
|
|
337
|
+
return kept(_eps, srv.id + '|' + sid, function () {
|
|
338
|
+
return get(srv, '/Shows/' + enc(sid) + '/Episodes?userId=' + uid(srv) + '&fields=Overview,MediaSources,MediaStreams,Chapters,Trickplay&isMissing=false&enableUserData=true', 16 * 1048576).then(function (r) {
|
|
339
|
+
return U.listOf(r && r.Items, 5000, function (e) { return e.Id && e.LocationType !== 'Virtual' && e.ParentIndexNumber >= 0 && e.IndexNumber >= 0; }).map(function (e) {
|
|
340
|
+
return { season: +e.ParentIndexNumber, episode: +e.IndexNumber, it: e };
|
|
341
|
+
}).sort(function (a, b) { return a.season - b.season || a.episode - b.episode; });
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
function parseId(id) {
|
|
346
|
+
var m = /^jf([ms])\.([^.]+)\.([0-9a-f-]{8,40})(?::(\d+):(\d+))?$/i.exec('' + id);
|
|
347
|
+
return m ? { kind: m[1], srv: server(m[2]), item: m[3], season: m[4] != null ? +m[4] : null, episode: m[5] != null ? +m[5] : null } : null;
|
|
348
|
+
}
|
|
349
|
+
function names(people, kind) {
|
|
350
|
+
return U.listOf(people, 200, function (p) { return p.Type === kind && p.Name; }).slice(0, 12).map(function (p) { return '' + p.Name; });
|
|
351
|
+
}
|
|
352
|
+
function meta(type, id) {
|
|
353
|
+
var x = parseId(id);
|
|
354
|
+
if (!x || !x.srv) { return Promise.resolve(null); }
|
|
355
|
+
var srv = x.srv, t = x.kind === 'm' ? 'movie' : 'series';
|
|
356
|
+
return item(srv, x.item).then(function (it) {
|
|
357
|
+
if (!it || !it.Id || !it.Name) { return null; }
|
|
358
|
+
var showId = 'jf' + x.kind + '.' + srv.id + '.' + it.Id, m = cardOf(srv, t, it);
|
|
359
|
+
m.id = showId; m._from = srv.name;
|
|
360
|
+
if (it.Overview) { m.description = ('' + it.Overview).slice(0, 2000); }
|
|
361
|
+
if (Array.isArray(it.Genres) && it.Genres.length) { m.genres = it.Genres.slice(0, 12).map(String); }
|
|
362
|
+
if (names(it.People, 'Actor').length) { m.cast = names(it.People, 'Actor'); }
|
|
363
|
+
if (names(it.People, 'Director').length) { m.director = names(it.People, 'Director'); }
|
|
364
|
+
if (+it.RunTimeTicks > 0) { m.runtime = Math.round(it.RunTimeTicks / TICKS / 60) + ' min'; }
|
|
365
|
+
if (m.releaseInfo) { m.year = m.releaseInfo; }
|
|
366
|
+
if (t === 'movie') { return m; }
|
|
367
|
+
return episodes(srv, it.Id).then(function (eps) {
|
|
368
|
+
m.videos = eps.map(function (e) {
|
|
369
|
+
var v = { id: showId + ':' + e.season + ':' + e.episode, season: e.season, episode: e.episode, title: ('' + (e.it.Name || '')).trim() || 'Episode ' + e.episode };
|
|
370
|
+
if (e.it.Overview) { v.overview = ('' + e.it.Overview).slice(0, 1000); }
|
|
371
|
+
if (e.it.ImageTags && e.it.ImageTags.Primary) { v.thumbnail = img(srv, e.it.Id, 'Primary', e.it.ImageTags.Primary, 'maxWidth=640'); }
|
|
372
|
+
var rel = Date.parse(e.it.PremiereDate || '');
|
|
373
|
+
if (rel) { v.released = new Date(rel).toISOString(); }
|
|
374
|
+
return v;
|
|
375
|
+
});
|
|
376
|
+
return m;
|
|
377
|
+
});
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// ---- Which titles each server has (for titles from your addons) ----
|
|
382
|
+
// type|tt… and type|tmdb:… -> its item ids; read when first asked, kept INDEX_FRESH_MS (the
|
|
383
|
+
// old list answers while a new one is read).
|
|
384
|
+
var _index = {};
|
|
385
|
+
function index(srv) {
|
|
386
|
+
var x = _index[srv.id];
|
|
387
|
+
if (x && x.map && (x.p || Date.now() - x.at < INDEX_FRESH_MS)) { return Promise.resolve(x.map); }
|
|
388
|
+
if (x && x.p) { return x.p; }
|
|
389
|
+
var p = get(srv, K(srv).items(uid(srv)) + 'recursive=true&includeItemTypes=Movie,Series&fields=ProviderIds&enableImages=false&enableUserData=false&enableTotalRecordCount=false', 16 * 1048576).then(function (r) {
|
|
390
|
+
var map = { __ids: {} }; // (__ids: an item's IMDb id, for Next up's cards)
|
|
391
|
+
function add(k, v) { (map[k] = map[k] || []).push(v); }
|
|
392
|
+
U.listOf(r && r.Items, 100000).forEach(function (it) {
|
|
393
|
+
var t = it.Type === 'Movie' ? 'movie' : it.Type === 'Series' ? 'series' : '', tt = imdbOf(it), tm = tmdbOf(it);
|
|
394
|
+
if (!t || !it.Id) { return; }
|
|
395
|
+
if (tt) { add(t + '|' + tt, '' + it.Id); map.__ids['' + it.Id] = tt; }
|
|
396
|
+
if (tm) { add(t + '|tmdb:' + tm, '' + it.Id); }
|
|
397
|
+
});
|
|
398
|
+
if (_index[srv.id] && _index[srv.id].p === p) { _index[srv.id] = { at: Date.now(), map: map }; }
|
|
399
|
+
return map;
|
|
400
|
+
}, function (e) {
|
|
401
|
+
U.log('jellyfin', '“' + srv.name + '” (' + kindName(srv) + '): its titles couldn’t be read (' + U.problem(e) + ')');
|
|
402
|
+
var old = _index[srv.id], map = (old && old.map) || { __ids: {} };
|
|
403
|
+
if (old && old.p === p) { _index[srv.id] = { at: Date.now() - INDEX_FRESH_MS + INDEX_RETRY_MS, map: map }; } // (asked again in a minute)
|
|
404
|
+
return map;
|
|
405
|
+
});
|
|
406
|
+
_index[srv.id] = { at: x ? x.at : 0, map: x ? x.map : null, p: p };
|
|
407
|
+
return x && x.map ? Promise.resolve(x.map) : p;
|
|
408
|
+
}
|
|
409
|
+
function copies(type, id, kind) {
|
|
410
|
+
var key = '', season = null, episode = null;
|
|
411
|
+
if (type === 'movie') { key = /^(tt\d+|tmdb:\d+)$/.test('' + id) ? '' + id : ''; }
|
|
412
|
+
else if (type === 'series') {
|
|
413
|
+
var mm = /^(tt\d+|tmdb:\d+):(\d+):(\d+)$/.exec('' + id);
|
|
414
|
+
if (mm) { key = mm[1]; season = +mm[2]; episode = +mm[3]; }
|
|
415
|
+
}
|
|
416
|
+
if (!key) { return Promise.resolve([]); }
|
|
417
|
+
return Promise.all(servers(kind).map(function (srv) {
|
|
418
|
+
return index(srv).then(function (map) {
|
|
419
|
+
return Promise.all((map[type + '|' + key] || []).slice(0, 3).map(function (iid) {
|
|
420
|
+
return type === 'movie' ? item(srv, iid).then(function (it) { return sourcesOf(srv, it); }, none) : episodeSources(srv, iid, season, episode);
|
|
421
|
+
}));
|
|
422
|
+
}).then(flat, none);
|
|
423
|
+
})).then(flat);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// ---- Sources ----
|
|
427
|
+
var EXT = { mkv: 'mkv', matroska: 'mkv', webm: 'webm', mp4: 'mp4', mov: 'mp4', m4v: 'mp4', avi: 'avi', mpegts: 'ts', ts: 'ts', m2ts: 'm2ts', mpeg: 'mpg', mpg: 'mpg', flv: 'flv', wmv: 'wmv', asf: 'wmv', ogg: 'ogv' };
|
|
428
|
+
function extOf(c) { var f = ('' + (c || '')).toLowerCase().split(',')[0].trim(); return EXT[f] || (/^[a-z0-9]{2,5}$/.test(f) ? f : 'mkv'); }
|
|
429
|
+
function quality(v) {
|
|
430
|
+
var w = +(v && v.Width) || 0, h = +(v && v.Height) || 0;
|
|
431
|
+
return w >= 3200 || h >= 1600 ? '4K' : w >= 1800 || h >= 900 ? '1080p' : w >= 1200 || h >= 600 ? '720p' : h > 0 ? h + 'p' : '';
|
|
432
|
+
}
|
|
433
|
+
function human(n) { return n >= 1073741824 ? (n / 1073741824).toFixed(1) + ' GB' : Math.max(1, Math.round(n / 1048576)) + ' MB'; }
|
|
434
|
+
var SUB_CODECS = /^(srt|subrip|ass|ssa|vtt|webvtt)$/i;
|
|
435
|
+
function source(srv, it, ms) {
|
|
436
|
+
var all = U.listOf(ms.MediaStreams, 200), v = all.filter(function (s) { return s.Type === 'Video'; })[0];
|
|
437
|
+
var range = '' + ((v && (v.VideoRangeType || v.VideoRange)) || ''), hdr = /DOVI/i.test(range) ? 'DV' : /HDR|HLG/i.test(range) ? 'HDR' : '';
|
|
438
|
+
var q = quality(v), ext = extOf(ms.Container), size = +ms.Size || 0;
|
|
439
|
+
var file = ('' + (ms.Path || ms.Name || it.Name || '')).split(/[\/\\]/).pop() || ('' + it.Name);
|
|
440
|
+
var st = {
|
|
441
|
+
url: api(srv) + '/Videos/' + enc(it.Id) + '/stream.' + ext + '?static=true&mediaSourceId=' + enc(ms.Id) + '&' + K(srv).key + '=' + enc(srv.token),
|
|
442
|
+
name: kindName(srv) + (q ? '\n' + q + (hdr ? ' ' + hdr : '') : ''),
|
|
443
|
+
title: file + '\n' + srv.name + (size ? ' · 💾 ' + human(size) : '') + ' · ' + ext.toUpperCase(),
|
|
444
|
+
behaviorHints: { filename: file, videoSize: size || undefined, bingeGroup: 'stremiok-jellyfin-' + srv.id },
|
|
445
|
+
_jf: placeOf({ srv: srv.id, kind: kindOf(srv), item: '' + it.Id, ms: '' + ms.Id }, it.UserData)
|
|
446
|
+
};
|
|
447
|
+
var ch = chapterSegments(it), mk = markerSegments(it), named = namedChapters(it), tp = trickplayOf(srv, it, ms);
|
|
448
|
+
if (ch) { st._jf.ch = ch; }
|
|
449
|
+
if (mk) { st._jf.mk = mk; }
|
|
450
|
+
if (named) { st._jf.chapters = named; }
|
|
451
|
+
if (tp) { st._jf.tp = tp; }
|
|
452
|
+
// its subtitle files (text ones), as SRT
|
|
453
|
+
var subs = all.filter(function (s) { return s.Type === 'Subtitle' && s.IsExternal && SUB_CODECS.test('' + (s.Codec || '')) && s.Index >= 0; }).slice(0, 30).map(function (s) {
|
|
454
|
+
return { id: 'jf' + s.Index, url: api(srv) + '/Videos/' + enc(it.Id) + '/' + enc(ms.Id) + '/Subtitles/' + (+s.Index) + K(srv).sub + '?' + K(srv).key + '=' + enc(srv.token),
|
|
455
|
+
lang: ('' + (s.Language || '')).slice(0, 12) };
|
|
456
|
+
});
|
|
457
|
+
if (subs.length) { st.subtitles = subs; }
|
|
458
|
+
return st;
|
|
459
|
+
}
|
|
460
|
+
// Where Jellyfin last had it (any of its apps): pos seconds (0: finished), at when.
|
|
461
|
+
function placeOf(jf, ud) {
|
|
462
|
+
var at = ud ? Date.parse(ud.LastPlayedDate || '') : 0;
|
|
463
|
+
if (at > 0) { jf.at = at; jf.pos = ud.Played ? 0 : Math.max(0, Math.round((+ud.PlaybackPositionTicks || 0) / TICKS)); }
|
|
464
|
+
return jf;
|
|
465
|
+
}
|
|
466
|
+
// Where to start a Jellyfin source: Jellyfin's place when it played it later than StremioK
|
|
467
|
+
// did (by over SAME_MS: the two are told the same place on leaving), else StremioK's (own).
|
|
468
|
+
var SAME_MS = 120000;
|
|
469
|
+
function resumeFor(jf, entry, own) {
|
|
470
|
+
if (!jf || !(jf.at > 0) || (entry && +entry.updated + SAME_MS >= jf.at)) { return own; }
|
|
471
|
+
return jf.pos > 5 ? jf.pos : 0;
|
|
472
|
+
}
|
|
473
|
+
// The file's chapters named for an intro, a recap, the credits or a preview (a release's own
|
|
474
|
+
// "Opening", "Recap", "End Credits"…; Jellyfin's "Chapter 3" isn't): each up to the next
|
|
475
|
+
// chapter (the last, to the end). { intro: [{ start, end }], … } in seconds, or null.
|
|
476
|
+
var CHAPTERS = [['intro', /^(intro|opening|opening (credits|titles|theme|song)|title sequence|main titles?|op)$/],
|
|
477
|
+
['recap', /^(recap|previously|previously on.*)$/], ['credits', /^(credits|end credits|ending|ending credits|closing credits|outro|ed)$/],
|
|
478
|
+
['preview', /^(preview|next time|next episode|coming up|next on.*)$/]];
|
|
479
|
+
function isChapter(c) { return !c.MarkerType || c.MarkerType === 'Chapter'; } // (Emby's markers are among its chapters)
|
|
480
|
+
function chapterSegments(it) {
|
|
481
|
+
var ch = U.listOf(it && it.Chapters, 200, function (c) { return c.Name != null && +c.StartPositionTicks >= 0 && isChapter(c); }).sort(function (a, b) { return a.StartPositionTicks - b.StartPositionTicks; });
|
|
482
|
+
var end = +(it && it.RunTimeTicks) || 0, out = null;
|
|
483
|
+
ch.forEach(function (c, i) {
|
|
484
|
+
var name = ('' + c.Name).toLowerCase().replace(/[\s._-]+/g, ' ').trim(), kind = null;
|
|
485
|
+
CHAPTERS.forEach(function (x) { if (!kind && x[1].test(name)) { kind = x[0]; } });
|
|
486
|
+
var a = +c.StartPositionTicks / TICKS, b = (i + 1 < ch.length ? +ch[i + 1].StartPositionTicks : end) / TICKS;
|
|
487
|
+
if (!kind || !(b > a)) { return; }
|
|
488
|
+
out = out || {};
|
|
489
|
+
(out[kind] = out[kind] || []).push({ start: Math.round(a * 10) / 10, end: Math.round(b * 10) / 10 });
|
|
490
|
+
});
|
|
491
|
+
return out;
|
|
492
|
+
}
|
|
493
|
+
// Its chapters as the player shows a YouTube video's ([{ t, title }]), when one has a name of
|
|
494
|
+
// its own: none for a file whose chapters are only numbered ("Chapter 3", "Chapter 03"), or
|
|
495
|
+
// timed ("00:05:00.000"). Among named ones, a numbered one has none (the player calls it by
|
|
496
|
+
// its place, "Chapter 3", not the file's "Chapter 04" left from before a chapter was added).
|
|
497
|
+
var PLAIN_CHAPTER = /^((chapter|chapitre|kapitel|cap[ií]tulo|capitolo|hoofdstuk)\s*\d+|\d+|[\d:.]+)$/i;
|
|
498
|
+
// Emby's intro and credits markers (its chapters' MarkerType: IntroStart, IntroEnd,
|
|
499
|
+
// CreditsStart; the credits to the end): { intro, credits }, or null.
|
|
500
|
+
function markerSegments(it) {
|
|
501
|
+
var at = {}, end = +(it && it.RunTimeTicks) || 0, out = null;
|
|
502
|
+
U.listOf(it && it.Chapters, 200, function (c) { return c.MarkerType && +c.StartPositionTicks >= 0; }).forEach(function (c) {
|
|
503
|
+
if (at[c.MarkerType] == null) { at[c.MarkerType] = +c.StartPositionTicks / TICKS; }
|
|
504
|
+
});
|
|
505
|
+
if (at.IntroStart != null && at.IntroEnd > at.IntroStart) { out = { intro: [{ start: at.IntroStart, end: at.IntroEnd }] }; }
|
|
506
|
+
if (at.CreditsStart != null && end / TICKS > at.CreditsStart) { (out = out || {}).credits = [{ start: at.CreditsStart, end: end / TICKS }]; }
|
|
507
|
+
return out;
|
|
508
|
+
}
|
|
509
|
+
function namedChapters(it) {
|
|
510
|
+
var list = U.listOf(it && it.Chapters, 200, function (c) { return c.Name != null && +c.StartPositionTicks >= 0 && isChapter(c); }).map(function (c) {
|
|
511
|
+
return { t: Math.round(+c.StartPositionTicks / TICKS * 10) / 10, title: ('' + c.Name).replace(/\s+/g, ' ').trim().slice(0, 80) };
|
|
512
|
+
}).sort(function (a, b) { return a.t - b.t; });
|
|
513
|
+
if (list.length < 2 || !list.some(function (c) { return c.title && !PLAIN_CHAPTER.test(c.title); })) { return null; }
|
|
514
|
+
list.forEach(function (c) { if (PLAIN_CHAPTER.test(c.title)) { c.title = ''; } });
|
|
515
|
+
return list;
|
|
516
|
+
}
|
|
517
|
+
// Its seek pictures (trickplay, made by Jellyfin 10.9 and later when switched on): the set
|
|
518
|
+
// nearest 320 wide. { w, h, cols, rows, n, every (ms), base (a tile's link, its number and
|
|
519
|
+
// .jpg added) }, or null.
|
|
520
|
+
function trickplayOf(srv, it, ms) {
|
|
521
|
+
var sets = it && it.Trickplay && (it.Trickplay[ms.Id] || it.Trickplay[it.Id]), best = null;
|
|
522
|
+
if (!sets || typeof sets !== 'object') { return null; }
|
|
523
|
+
Object.keys(sets).forEach(function (k) {
|
|
524
|
+
var x = sets[k];
|
|
525
|
+
if (!x || !(+x.Width > 0) || !(+x.Height > 0) || !(+x.TileWidth > 0) || !(+x.TileHeight > 0) || !(+x.ThumbnailCount > 0) || !(+x.Interval > 0)) { return; }
|
|
526
|
+
if (!best || Math.abs(x.Width - 320) < Math.abs(best.Width - 320)) { best = x; }
|
|
527
|
+
});
|
|
528
|
+
if (!best) { return null; }
|
|
529
|
+
return { w: +best.Width, h: +best.Height, cols: +best.TileWidth, rows: +best.TileHeight, n: +best.ThumbnailCount, every: +best.Interval,
|
|
530
|
+
base: api(srv) + '/Videos/' + enc(it.Id) + '/Trickplay/' + (+best.Width) + '/', q: '.jpg?mediaSourceId=' + enc(ms.Id) + '&' + K(srv).key + '=' + enc(srv.token) };
|
|
531
|
+
}
|
|
532
|
+
// The seek picture for a moment (seconds): its tile's link and where in it, or null.
|
|
533
|
+
// (tp.one: a picture a link, base + its moment in ms, as Plex's are)
|
|
534
|
+
function trickplayAt(tp, sec) {
|
|
535
|
+
if (!tp || !(sec >= 0)) { return null; }
|
|
536
|
+
var i = Math.min(tp.n - 1, Math.floor(sec * 1000 / tp.every)), per = tp.cols * tp.rows, at = i % per;
|
|
537
|
+
if (tp.one) { return { url: tp.base + (i * tp.every) + tp.q, x: 0, y: 0, w: tp.w, h: tp.h, cols: 1, rows: 1 }; }
|
|
538
|
+
return { url: tp.base + Math.floor(i / per) + tp.q, x: (at % tp.cols) * tp.w, y: Math.floor(at / tp.cols) * tp.h, w: tp.w, h: tp.h, cols: tp.cols, rows: tp.rows };
|
|
539
|
+
}
|
|
540
|
+
// Its media segments (Jellyfin 10.10 and later, made by a plugin such as Intro Skipper) or
|
|
541
|
+
// markers (Emby's, Plex's), else its file's chapters (chapterSegments), as the skip-time
|
|
542
|
+
// services give theirs (seconds); null without any. asked: Diagnostics' line.
|
|
543
|
+
var SEG_TYPES = { Intro: 'intro', Recap: 'recap', Outro: 'credits', Preview: 'preview' };
|
|
544
|
+
function segments(jf, asked) {
|
|
545
|
+
var plex = !!jf && jf.kind === 'plex', srv = jf && !plex ? server(jf.srv) : null, name = plex ? 'Plex' : srv ? kindName(srv) : '';
|
|
546
|
+
if (!srv && !plex) { return Promise.resolve(null); }
|
|
547
|
+
function done(out, has, why) {
|
|
548
|
+
var ch = jf.ch || {}, more = [];
|
|
549
|
+
out = out || { intro: [], recap: [], credits: [], preview: [], from: {} };
|
|
550
|
+
['intro', 'recap', 'credits', 'preview'].forEach(function (k) {
|
|
551
|
+
if (!out[k].length && U.listOf(ch[k], 10).length) { out[k] = U.listOf(ch[k], 10); out.from[k] = name + '’s chapters'; more.push(k); }
|
|
552
|
+
});
|
|
553
|
+
if (asked) { asked.push(name + ': ' + (has.length ? has.join(', ') : why) + (more.length ? '; its chapters: ' + more.join(', ') : '')); }
|
|
554
|
+
return has.length || more.length ? out : null;
|
|
555
|
+
}
|
|
556
|
+
if (plex || kindOf(srv) === 'emby') { // (its markers, read with its details: nothing to ask)
|
|
557
|
+
var out = { intro: [], recap: [], credits: [], preview: [], from: {} }, has = [];
|
|
558
|
+
['intro', 'credits'].forEach(function (k) { if (jf.mk && U.listOf(jf.mk[k], 4).length) { out[k] = U.listOf(jf.mk[k], 4); out.from[k] = name; has.push(k); } });
|
|
559
|
+
return Promise.resolve(done(out, has, 'no intro or credits markers'));
|
|
560
|
+
}
|
|
561
|
+
return U.getJSON(api(srv) + '/MediaSegments/' + enc(jf.item), 8000, 262144, null, headers(srv, srv.token), null, true).then(function (r) {
|
|
562
|
+
var out = { intro: [], recap: [], credits: [], preview: [], from: {} }, has = [];
|
|
563
|
+
U.listOf(r && r.Items, 40).forEach(function (g) {
|
|
564
|
+
var k = SEG_TYPES[g.Type], a = +g.StartTicks / TICKS, b = +g.EndTicks / TICKS;
|
|
565
|
+
if (!k || !(a >= 0) || !(b > a)) { return; }
|
|
566
|
+
out[k].push({ start: a, end: b });
|
|
567
|
+
out.from[k] = 'Jellyfin';
|
|
568
|
+
if (has.indexOf(k) === -1) { has.push(k); }
|
|
569
|
+
});
|
|
570
|
+
return done(out, has, 'no media segments');
|
|
571
|
+
}, function (e) {
|
|
572
|
+
return done(null, [], /HTTP 404/.test(e && e.message) ? 'no media segments (before 10.10)' : U.problem(e));
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
function sourcesOf(srv, it) {
|
|
576
|
+
if (!it || !it.Id) { return []; }
|
|
577
|
+
return U.listOf(it.MediaSources, 8, function (ms) { return ms.Id; }).map(function (ms) { return source(srv, it, ms); });
|
|
578
|
+
}
|
|
579
|
+
function episodeSources(srv, sid, season, episode) {
|
|
580
|
+
return episodes(srv, sid).then(function (eps) {
|
|
581
|
+
var e = eps.filter(function (x) { return x.season === season && x.episode === episode; })[0];
|
|
582
|
+
return e ? sourcesOf(srv, e.it) : [];
|
|
583
|
+
}, none);
|
|
584
|
+
}
|
|
585
|
+
// A title's sources: its own (jfm / jfs ids), or the servers' copies (of that kind: each
|
|
586
|
+
// kind's addon asks for its own) of a title from your addons.
|
|
587
|
+
function streams(type, id, kind) {
|
|
588
|
+
var x = parseId(id);
|
|
589
|
+
if (x) {
|
|
590
|
+
if (!x.srv) { return Promise.resolve([]); }
|
|
591
|
+
if (x.kind === 'm') { return item(x.srv, x.item).then(function (it) { return sourcesOf(x.srv, it); }, none); }
|
|
592
|
+
return x.season != null ? episodeSources(x.srv, x.item, x.season, x.episode) : Promise.resolve([]);
|
|
593
|
+
}
|
|
594
|
+
return copies(type, id, kind);
|
|
595
|
+
}
|
|
596
|
+
function isJellyfinId(id) { return /^jf[ms]\./.test('' + id); }
|
|
597
|
+
function nameOfId(id) { var x = parseId(id); return x && x.srv ? kindName(x.srv) : 'Jellyfin'; }
|
|
598
|
+
// The kind of server a source is from, by name ("Jellyfin", "Emby", "Plex").
|
|
599
|
+
function sourceKind(jf) { return jf && jf.kind === 'plex' ? 'Plex' : jf && KINDS[jf.kind] ? KINDS[jf.kind].name : 'Jellyfin'; }
|
|
600
|
+
|
|
601
|
+
// ---- What you play, back to Jellyfin (its sessions) ----
|
|
602
|
+
// From the player (player-core): 'progress' as it plays, 'pause', 'stop' on leaving,
|
|
603
|
+
// 'watched' at the credits. Started once it's playing; where it is every REPORT_EVERY and on
|
|
604
|
+
// pausing or carrying on. Nothing during private viewing, for a trailer, or another source.
|
|
605
|
+
var _play = null;
|
|
606
|
+
function report(p, path, cur) {
|
|
607
|
+
var srv = server(p.srv);
|
|
608
|
+
if (!srv) { return; }
|
|
609
|
+
var b = { ItemId: p.item, MediaSourceId: p.ms, PlaySessionId: p.sid, PositionTicks: Math.max(0, Math.round((cur || 0) * TICKS)) };
|
|
610
|
+
if (path !== '/Sessions/Playing/Stopped') { b.CanSeek = true; b.IsPaused = !!p.paused; b.PlayMethod = 'DirectPlay'; }
|
|
611
|
+
post(srv, path, b).then(null, function (e) { U.log('jellyfin', '“' + srv.name + '” wasn’t told where you are (' + U.problem(e) + ')'); });
|
|
612
|
+
}
|
|
613
|
+
function stopNow(cur) {
|
|
614
|
+
var p = _play;
|
|
615
|
+
_play = null;
|
|
616
|
+
if (p && p.started) { report(p, '/Sessions/Playing/Stopped', cur != null ? cur : p.cur); }
|
|
617
|
+
}
|
|
618
|
+
function onPlayer(ev, ctx, cur, dur, paused) {
|
|
619
|
+
if (typeof Plex !== 'undefined' && Plex.onPlayer) { try { Plex.onPlayer(ev, ctx, cur, dur, paused); } catch (e) {} } // (a Plex source: told to Plex)
|
|
620
|
+
var jf = ctx && !ctx.private && !ctx.trailer && ctx.stream && ctx.stream._jf, srv = jf && jf.kind !== 'plex' && server(jf.srv);
|
|
621
|
+
if (ev === 'watched') {
|
|
622
|
+
if (srv) { post(srv, K(srv).played(uid(srv), enc(jf.item)), null).then(null, function (e) { U.log('jellyfin', '“' + srv.name + '”: not marked played (' + U.problem(e) + ')'); }); }
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
if (_play && (!srv || _play.ctx !== ctx || _play.item !== jf.item || _play.ms !== jf.ms)) { stopNow(); } // (another playback, or another source)
|
|
626
|
+
if (!srv || ev === 'stop') { if (ev === 'stop') { stopNow(cur); } return; }
|
|
627
|
+
if (!_play) { _play = { ctx: ctx, srv: srv.id, item: jf.item, ms: jf.ms, sid: rand(32), started: false, sent: 0, paused: false, cur: 0 }; }
|
|
628
|
+
var p = _play, now = Date.now(), pz = ev === 'pause' || !!paused;
|
|
629
|
+
if (cur > 0) { p.cur = cur; }
|
|
630
|
+
if (!(cur > 0)) { return; } // (not playing yet)
|
|
631
|
+
if (!p.started) { p.started = true; p.sent = now; p.paused = pz; report(p, '/Sessions/Playing', cur); return; }
|
|
632
|
+
if (pz !== p.paused || now - p.sent >= REPORT_EVERY) { p.sent = now; p.paused = pz; report(p, '/Sessions/Playing/Progress', cur); }
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
return {
|
|
636
|
+
servers: servers, server: server, isEnabled: isEnabled, onChange: onChange, normAddress: normAddress, addressProblem: addressProblem, probe: probe,
|
|
637
|
+
signIn: signIn, quickConnect: quickConnect, remove: remove, reload: reload, libraries: libsOf, synthAddon: synthAddon, synthAddons: synthAddons, kindsOn: kindsOn,
|
|
638
|
+
kindName: kindName, nameOfId: nameOfId, catalog: catalog, meta: meta, streams: streams, isJellyfinId: isJellyfinId, onPlayer: onPlayer, resumeFor: resumeFor,
|
|
639
|
+
segments: segments, trickplayAt: trickplayAt, sourceKind: sourceKind,
|
|
640
|
+
shared: { chapterSegments: chapterSegments, namedChapters: namedChapters } // (Plex's chapters read the same way)
|
|
641
|
+
};
|
|
642
|
+
})();
|