stremiok 2.3.8 → 2.4.0
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 +14 -0
- package/README.md +6 -2
- package/config.xml +1 -1
- package/css/style.css +14 -0
- package/index.html +46 -43
- package/js/app.js +15 -1
- package/js/hubpreview.js +8 -1
- package/js/i18n.js +3 -2
- package/js/iptv.js +12 -5
- package/js/lang/ar.js +96 -1
- package/js/lang/ca.js +96 -1
- package/js/lang/de.js +96 -1
- package/js/lang/es.js +96 -1
- package/js/lang/fi.js +96 -1
- package/js/lang/fr.js +96 -1
- package/js/lang/he.js +96 -1
- package/js/lang/hi.js +96 -1
- package/js/lang/it.js +96 -1
- package/js/lang/ja.js +96 -1
- package/js/lang/ko.js +96 -1
- package/js/lang/pl.js +96 -1
- package/js/lang/pt-BR.js +96 -1
- package/js/lang/ru.js +96 -1
- package/js/lang/tr.js +96 -1
- package/js/lang/zh-CN.js +96 -1
- package/js/nuvio-import.js +152 -0
- package/js/nuvio-sync.js +269 -0
- package/js/player-core.js +1 -0
- package/js/player-store.js +4 -1
- package/js/profiles.js +158 -0
- package/js/stremio.js +4 -1
- package/js/util.js +19 -3
- package/js/view-core.js +89 -10
- package/js/view-detail.js +11 -10
- package/js/view-home.js +2 -1
- package/js/view-search.js +15 -1
- package/js/view-settings.js +399 -4
- package/js/views.js +1 -1
- package/package.json +1 -1
- package/tizenbrew.html +1 -1
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/* Profiles from a Nuvio account (Settings › Profiles › Import profiles from Nuvio; #23), once:
|
|
2
|
+
each profile picked becomes a StremioK profile with its name, color, addons (in their order),
|
|
3
|
+
watched marks and recent progress. Read only, nothing changed in the Nuvio account.
|
|
4
|
+
- Nuvio's backend (https://github.com/NuvioMedia/self-host, Apache License 2.0, by Nuvio) as its
|
|
5
|
+
hosted service publishes it: its client settings read at run time from
|
|
6
|
+
https://api.nuvio.tv/.well-known/nuvio (its public client key with them: none kept in
|
|
7
|
+
StremioK's code); the TV sign-in Nuvio's own TV apps use (a code and a link, approved on your
|
|
8
|
+
phone, asked every few seconds); then its profiles, addons, progress and watched marks read.
|
|
9
|
+
- The session Nuvio gives (one for the whole account) is kept in memory only, used for these
|
|
10
|
+
reads, and signed out at once, whatever happens: never saved, never logged.
|
|
11
|
+
- StremioK isn't Nuvio's and isn't affiliated with it: this relies on an interface Nuvio may
|
|
12
|
+
change, and may stop working if it does. Nuvio's own app code (GPL) isn't used.
|
|
13
|
+
Global namespace: NuvioImport */
|
|
14
|
+
var NuvioImport = (function () {
|
|
15
|
+
'use strict';
|
|
16
|
+
var DISCOVERY = 'https://api.nuvio.tv/.well-known/nuvio';
|
|
17
|
+
var LOGIN_PAGE = 'https://nuvio.tv/tv-login'; // (where Nuvio's own TV apps send you to approve)
|
|
18
|
+
var POLL_MS = 3000, WAIT_MS = 5 * 60 * 1000, MAX = 4 * 1048576, PROGRESS_MAX = 60, WATCHED_MAX = 20000;
|
|
19
|
+
|
|
20
|
+
// Its client settings: { url (its backend, https), key (its public client key) }.
|
|
21
|
+
function discover() {
|
|
22
|
+
return U.getJSON(DISCOVERY, 15000, 65536).then(function (d) {
|
|
23
|
+
var url = d && typeof d.backend_url === 'string' ? d.backend_url.replace(/\/+$/, '') : '', p = U.parseUrl(url);
|
|
24
|
+
if (!p || !/^https:\/\//i.test(url) || typeof d.publishable_key !== 'string' || d.publishable_key.length > 2000 || !(d.capabilities && d.capabilities.tv_login)) {
|
|
25
|
+
throw new Error(U.t('Nuvio’s sign-in isn’t available'));
|
|
26
|
+
}
|
|
27
|
+
return { url: url, key: d.publishable_key };
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
// One request to it: its JSON answer. token: the session (else the public key).
|
|
31
|
+
function ask(cfg, path, body, token) {
|
|
32
|
+
var h = { apikey: cfg.key, Authorization: 'Bearer ' + (token || cfg.key), 'Content-Type': 'application/json' };
|
|
33
|
+
return U.getJSON(cfg.url + path, 20000, MAX, null, h, body == null ? undefined : JSON.stringify(body), true);
|
|
34
|
+
}
|
|
35
|
+
function rpc(cfg, fn, params, token) { return ask(cfg, '/rest/v1/rpc/' + fn, params || {}, token); }
|
|
36
|
+
function first(r) { return Array.isArray(r) ? r[0] : r; }
|
|
37
|
+
|
|
38
|
+
// A code to approve: { code, link, nonce }.
|
|
39
|
+
function start(cfg) {
|
|
40
|
+
var nonce = U.randomHex(32);
|
|
41
|
+
return rpc(cfg, 'start_tv_login_session', { p_device_nonce: nonce, p_redirect_base_url: LOGIN_PAGE, p_device_name: 'StremioK' }).then(function (r) {
|
|
42
|
+
var x = first(r) || {}, link = '' + (x.web_url || '');
|
|
43
|
+
if (!/^[0-9a-f]{8,64}$/i.test(x.code || '') || !U.parseUrl(link) || link.indexOf(LOGIN_PAGE) !== 0) { throw new Error(U.t('Nuvio didn’t give a code')); }
|
|
44
|
+
return { code: x.code, link: link, nonce: nonce };
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
// The session once approved on the phone (asked every POLL_MS, WAIT_MS at most). stopped():
|
|
48
|
+
// true once the screen asking has gone (a session that arrives then is signed out).
|
|
49
|
+
function wait(cfg, s, stopped) {
|
|
50
|
+
var until = Date.now() + WAIT_MS;
|
|
51
|
+
return new Promise(function (resolve, reject) {
|
|
52
|
+
function poll() {
|
|
53
|
+
if (stopped && stopped()) { reject(new Error('stopped')); return; }
|
|
54
|
+
if (Date.now() > until) { reject(new Error(U.t('The code expired: try again'))); return; }
|
|
55
|
+
rpc(cfg, 'poll_tv_login_session', { p_code: s.code, p_device_nonce: s.nonce }).then(function (r) {
|
|
56
|
+
var st = (first(r) || {}).status;
|
|
57
|
+
if (st === 'expired' || st === 'cancelled') { reject(new Error(U.t('The code expired: try again'))); return; }
|
|
58
|
+
if (st !== 'approved') { setTimeout(poll, POLL_MS); return; }
|
|
59
|
+
ask(cfg, '/functions/v1/tv-logins-exchange', { code: s.code, device_nonce: s.nonce }).then(function (t) {
|
|
60
|
+
var token = t && typeof t.access_token === 'string' && t.access_token.length < 8192 ? t.access_token : '';
|
|
61
|
+
if (!token) { reject(new Error(U.t('Nuvio didn’t sign StremioK in'))); return; }
|
|
62
|
+
if (stopped && stopped()) { signOut(cfg, token); reject(new Error('stopped')); return; }
|
|
63
|
+
// (the session: { access, refresh (to renew it, for a profile kept in sync), expires (ms) })
|
|
64
|
+
resolve({ access: token, refresh: typeof t.refresh_token === 'string' && t.refresh_token.length < 4096 ? t.refresh_token : '', expires: Date.now() + Math.max(60, +t.expires_in || 3600) * 1000 });
|
|
65
|
+
}, reject);
|
|
66
|
+
}, function () { setTimeout(poll, POLL_MS); }); // (a dropped answer: asked again)
|
|
67
|
+
}
|
|
68
|
+
setTimeout(poll, POLL_MS);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
// The session given up (Nuvio's own sign-out): whatever happens, nothing is kept here.
|
|
72
|
+
function signOut(cfg, token) { return ask(cfg, '/auth/v1/logout', {}, token).then(function () {}, function () {}); }
|
|
73
|
+
|
|
74
|
+
// Its profiles: [{ index, name, color (#rrggbb), primaryAddons, primaryPlugins, locked }].
|
|
75
|
+
function profiles(cfg, token) {
|
|
76
|
+
return rpc(cfg, 'sync_pull_profiles', {}, token).then(function (r) {
|
|
77
|
+
return U.listOf(r, 6, function (p) { return p && typeof p.profile_index === 'number'; }).map(function (p) {
|
|
78
|
+
return { index: p.profile_index, name: U.drawable('' + (p.name || '')).trim().slice(0, 24) || U.t('Profile {n}', { n: p.profile_index }),
|
|
79
|
+
color: /^#?[0-9a-f]{6}$/i.test(p.avatar_color_hex || '') ? ('#' + ('' + p.avatar_color_hex).replace('#', '')).toLowerCase() : '',
|
|
80
|
+
primaryAddons: !!p.uses_primary_addons, primaryPlugins: !!p.uses_primary_plugins, locked: !!p.pin_enabled };
|
|
81
|
+
}).sort(function (a, b) { return a.index - b.index; });
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
// What a profile brings: { addons: [url] (in its order, the ones on), watched: { key: when },
|
|
85
|
+
// progress: [{ type, id, parent, season, episode, position (s), duration (s), updated (ms) }] }.
|
|
86
|
+
function profileData(cfg, token, p, all, plugins) {
|
|
87
|
+
var repos = (plugins || []).filter(function (a) { return a.profile_id === (p.primaryPlugins ? 1 : p.index) && a.enabled !== false && typeof a.url === 'string'; })
|
|
88
|
+
.sort(function (a, b) { return (a.sort_order || 0) - (b.sort_order || 0); }).map(function (a) { return { url: a.url.trim(), name: U.drawable('' + (a.name || '')).slice(0, 60) }; });
|
|
89
|
+
var addons = all.filter(function (a) { return a.profile_id === (p.primaryAddons ? 1 : p.index) && a.enabled !== false && typeof a.url === 'string'; })
|
|
90
|
+
.sort(function (a, b) { return (a.sort_order || 0) - (b.sort_order || 0); }).map(function (a) { return a.url.trim(); });
|
|
91
|
+
return Promise.all([
|
|
92
|
+
rpc(cfg, 'sync_pull_watched_items', { p_profile_id: p.index, p_page: 1, p_page_size: WATCHED_MAX }, token).then(null, function () { return []; }),
|
|
93
|
+
rpc(cfg, 'sync_pull_watch_progress', { p_profile_id: p.index, p_limit: PROGRESS_MAX }, token).then(null, function () { return []; })
|
|
94
|
+
]).then(function (r) {
|
|
95
|
+
var watched = {};
|
|
96
|
+
U.listOf(r[0], WATCHED_MAX, function (w) { return w && /^tt\d+$/.test(w.content_id || '') && (w.content_type === 'movie' || w.content_type === 'series'); }).forEach(function (w) {
|
|
97
|
+
var ep = w.content_type === 'series' && w.season > 0 && w.episode > 0, when = +w.watched_at || Date.now();
|
|
98
|
+
if (w.content_type === 'series' && !ep) { return; } // (a whole show: its episodes say)
|
|
99
|
+
watched[w.content_type + ':' + w.content_id + (ep ? ':' + w.season + ':' + w.episode : '')] = when < 1e11 ? when * 1000 : when;
|
|
100
|
+
});
|
|
101
|
+
var progress = U.listOf(r[1], PROGRESS_MAX, function (x) { return x && /^tt\d+$/.test(x.content_id || '') && (x.content_type === 'movie' || x.content_type === 'series') && +x.position > 0; }).map(function (x) {
|
|
102
|
+
var ep = x.content_type === 'series' && x.season > 0 && x.episode > 0, dur = +x.duration || 0, pos = +x.position || 0, ms = dur > 86400 || pos > 86400; // (in milliseconds, as Nuvio keeps them)
|
|
103
|
+
return { type: x.content_type, id: ep ? x.content_id + ':' + x.season + ':' + x.episode : x.content_id, parent: x.content_id, season: x.season, episode: x.episode,
|
|
104
|
+
position: Math.floor(ms ? pos / 1000 : pos), duration: Math.floor(ms ? dur / 1000 : dur), updated: (+x.last_watched || 0) < 1e11 ? (+x.last_watched || 0) * 1000 : +x.last_watched };
|
|
105
|
+
}).filter(function (x) { return x.type === 'movie' || x.season > 0; });
|
|
106
|
+
return { addons: addons, watched: watched, progress: progress, plugins: repos };
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
// Everything at once: [{ index, name, color, locked, addons, watched, progress, plugins }]; then
|
|
110
|
+
// signed out, unless keep (a profile kept in sync with it: js/nuvio-sync.js keeps the session).
|
|
111
|
+
function readAll(cfg, token, keep) {
|
|
112
|
+
var out = profiles(cfg, token).then(function (list) {
|
|
113
|
+
return Promise.all([
|
|
114
|
+
ask(cfg, '/rest/v1/addons?select=url,name,enabled,sort_order,profile_id&order=sort_order.asc', null, token),
|
|
115
|
+
ask(cfg, '/rest/v1/plugins?select=url,name,enabled,sort_order,profile_id&order=sort_order.asc', null, token).then(null, function () { return []; })
|
|
116
|
+
]).then(function (r) {
|
|
117
|
+
var addons = U.listOf(r[0], 2000, function (x) { return x && typeof x.url === 'string'; }), plugins = U.listOf(r[1], 200, function (x) { return x && typeof x.url === 'string'; });
|
|
118
|
+
return Promise.all(list.map(function (p) { return profileData(cfg, token, p, addons, plugins).then(function (d) { return Object.assign({}, p, d); }); }));
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
if (!keep) { out.then(function () { signOut(cfg, token); }, function () { signOut(cfg, token); }); } // (signed out as soon as it's read)
|
|
122
|
+
return out;
|
|
123
|
+
}
|
|
124
|
+
// Its profiles' addons only (Settings › Content › Addons › Import addons from Nuvio): [{ index,
|
|
125
|
+
// name, addons: [{ url, name }] (in its order, the ones on) }]; then signed out, unless keep.
|
|
126
|
+
function readAddons(cfg, token, keep) {
|
|
127
|
+
var out = Promise.all([profiles(cfg, token), ask(cfg, '/rest/v1/addons?select=url,name,enabled,sort_order,profile_id&order=sort_order.asc', null, token)]).then(function (r) {
|
|
128
|
+
var rows = U.listOf(r[1], 2000, function (x) { return x && typeof x.url === 'string'; });
|
|
129
|
+
return r[0].map(function (p) {
|
|
130
|
+
var own = p.primaryAddons ? 1 : p.index;
|
|
131
|
+
return { index: p.index, name: p.name, addons: rows.filter(function (a) { return a.profile_id === own && a.enabled !== false; })
|
|
132
|
+
.sort(function (a, b) { return (a.sort_order || 0) - (b.sort_order || 0); }).map(function (a) { return { url: a.url.trim(), name: U.drawable('' + (a.name || '')).slice(0, 60) }; }) };
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
if (!keep) { out.then(function () { signOut(cfg, token); }, function () { signOut(cfg, token); }); }
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
// The StremioK color nearest a Nuvio one.
|
|
139
|
+
function nearestColor(hex, palette) {
|
|
140
|
+
var m = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(hex || '');
|
|
141
|
+
if (!m) { return null; }
|
|
142
|
+
var best = null, bd = 1e9, r = parseInt(m[1], 16), g = parseInt(m[2], 16), b = parseInt(m[3], 16);
|
|
143
|
+
Object.keys(palette).forEach(function (k) {
|
|
144
|
+
var q = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(palette[k]);
|
|
145
|
+
if (!q) { return; }
|
|
146
|
+
var d = Math.pow(r - parseInt(q[1], 16), 2) + Math.pow(g - parseInt(q[2], 16), 2) + Math.pow(b - parseInt(q[3], 16), 2);
|
|
147
|
+
if (d < bd) { bd = d; best = k; }
|
|
148
|
+
});
|
|
149
|
+
return best;
|
|
150
|
+
}
|
|
151
|
+
return { discover: discover, start: start, wait: wait, readAll: readAll, readAddons: readAddons, signOut: signOut, nearestColor: nearestColor, ask: ask, rpc: rpc, LOGIN_PAGE: LOGIN_PAGE };
|
|
152
|
+
})();
|
package/js/nuvio-sync.js
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/* A profile kept in step with a Nuvio account's profile (Settings › Profiles › + Add a profile ›
|
|
2
|
+
A Nuvio account, or Settings › Content › Integrations › Nuvio account; #23): where you stopped and
|
|
3
|
+
what you've watched, both ways. Signing in is the import's (js/nuvio-import.js: Nuvio's TV
|
|
4
|
+
sign-in, approved on your phone); here its session is kept, for this StremioK profile only, and
|
|
5
|
+
renewed as it runs out.
|
|
6
|
+
- Each sync (launch, every EVERY_MS, a few seconds after playing or marking, before switching
|
|
7
|
+
profile): first what changed here since the last one (progress, watched marks: a mark taken
|
|
8
|
+
off as Nuvio's removed) goes to Nuvio; then what changed there comes here, the newer winning
|
|
9
|
+
(a title new here gets its name and poster from this profile's addons, a few each time).
|
|
10
|
+
Something Nuvio sent isn't sent back.
|
|
11
|
+
- Its Library too: what you add or remove here goes first, then Nuvio's whole Library comes
|
|
12
|
+
back as it now is (its own rows on Home and Discover: Nuvio Library).
|
|
13
|
+
- Only films and series with an IMDb id (tt…), as Nuvio keeps them.
|
|
14
|
+
- Nuvio's backend (github.com/NuvioMedia/self-host, Apache License 2.0, by Nuvio) as its hosted
|
|
15
|
+
service publishes it; StremioK isn't affiliated with Nuvio, and this may stop working if
|
|
16
|
+
Nuvio changes it. The session is a whole Nuvio account's: Disconnect signs it out and forgets it.
|
|
17
|
+
Global namespace: NuvioSync */
|
|
18
|
+
var NuvioSync = (function () {
|
|
19
|
+
'use strict';
|
|
20
|
+
var KEY = 'stremiok.nuvio'; // { url, key, access, refresh, exp, profile (Nuvio's 1-6), name, at }
|
|
21
|
+
var STATE_KEY = 'stremiok.nuvio.sync'; // { last, pushed, pulled, err, reconnect }
|
|
22
|
+
var EVERY_MS = 15 * 60 * 1000, SOON_MS = 10000, META_PER_SYNC = 30, PROGRESS_PULL = 300, WATCHED_PULL = 20000, MAX = 4 * 1048576;
|
|
23
|
+
var TT = /^tt\d{1,12}$/;
|
|
24
|
+
var LIB_KEY = 'stremiok.nuvio.library'; // { items: { 'type:id': { id, type, name, poster, background, added } }, out: { 'type:id': true (add) | false (remove) } }
|
|
25
|
+
var LIB_PAGE = 500, LIB_MAX = 2000;
|
|
26
|
+
|
|
27
|
+
function auth() { var a = U.store.get(KEY, null); return (a && typeof a.access === 'string' && a.access && typeof a.url === 'string') ? a : null; }
|
|
28
|
+
function isConnected() { return !!auth(); }
|
|
29
|
+
function state() { var s = U.store.get(STATE_KEY, null); return (s && typeof s === 'object') ? s : {}; }
|
|
30
|
+
function setState(patch) { var s = state(); Object.keys(patch).forEach(function (k) { s[k] = patch[k]; }); U.store.set(STATE_KEY, s); }
|
|
31
|
+
function connectStatus() { var a = auth() || {}, s = state(); return { connected: !!a.access, name: a.name || '', reconnect: !!s.reconnect, lastSync: s.last || 0, err: s.err || '' }; }
|
|
32
|
+
|
|
33
|
+
// The session kept for a profile: this one (slot undefined), or a new one's (its slot), from the
|
|
34
|
+
// sign-in (cfg: NuvioImport.discover's; s: NuvioImport.wait's) and the Nuvio profile it follows.
|
|
35
|
+
function record(cfg, s, p) { return { url: cfg.url, key: cfg.key, access: s.access, refresh: s.refresh, exp: s.expires, profile: p.index, name: p.name, at: Date.now() }; }
|
|
36
|
+
function connectFor(slot, cfg, s, p) {
|
|
37
|
+
try { window.localStorage.setItem(Profiles.keyFor(slot, KEY), JSON.stringify(record(cfg, s, p))); } catch (e) {}
|
|
38
|
+
}
|
|
39
|
+
function connect(cfg, s, p) {
|
|
40
|
+
U.store.set(KEY, record(cfg, s, p));
|
|
41
|
+
U.store.remove(STATE_KEY); U.store.remove(LIB_KEY);
|
|
42
|
+
setState({ pushed: Date.now() }); // (what's here already isn't all sent: from now on)
|
|
43
|
+
return sync();
|
|
44
|
+
}
|
|
45
|
+
function disconnect() {
|
|
46
|
+
var a = auth();
|
|
47
|
+
U.store.remove(KEY); U.store.remove(STATE_KEY); U.store.remove(LIB_KEY);
|
|
48
|
+
libChanged();
|
|
49
|
+
if (a) { ask(a, '/auth/v1/logout', {}, a.access).then(null, function () {}); }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ---- talking to it ----
|
|
53
|
+
function ask(a, path, body, token) {
|
|
54
|
+
var h = { apikey: a.key, Authorization: 'Bearer ' + (token || a.key), 'Content-Type': 'application/json' };
|
|
55
|
+
return U.getJSON(a.url + path, 20000, MAX, null, h, body == null ? undefined : JSON.stringify(body), true);
|
|
56
|
+
}
|
|
57
|
+
// A fresh session (renewed when within a minute of running out, or refused): its token.
|
|
58
|
+
function renew(a) {
|
|
59
|
+
if (!a.refresh) { return Promise.reject(new Error('no refresh')); }
|
|
60
|
+
return ask(a, '/auth/v1/token?grant_type=refresh_token', { refresh_token: a.refresh }).then(function (t) {
|
|
61
|
+
if (!t || typeof t.access_token !== 'string') { throw new Error('refused'); }
|
|
62
|
+
a.access = t.access_token; a.refresh = typeof t.refresh_token === 'string' ? t.refresh_token : a.refresh;
|
|
63
|
+
a.exp = Date.now() + Math.max(60, +t.expires_in || 3600) * 1000;
|
|
64
|
+
U.store.set(KEY, a);
|
|
65
|
+
return a.access;
|
|
66
|
+
}, function (e) { if (/HTTP 40[01]/.test((e && e.message) || '')) { setState({ reconnect: true, err: U.t('Its sign-in no longer works') }); } throw e; });
|
|
67
|
+
}
|
|
68
|
+
function token(a) { return a.exp && a.exp - Date.now() > 60000 ? Promise.resolve(a.access) : renew(a); }
|
|
69
|
+
function rpc(a, fn, params) {
|
|
70
|
+
return token(a).then(function (t) {
|
|
71
|
+
return ask(a, '/rest/v1/rpc/' + fn, params || {}, t).then(null, function (e) {
|
|
72
|
+
if (!/HTTP 401/.test((e && e.message) || '')) { throw e; }
|
|
73
|
+
return renew(a).then(function (t2) { return ask(a, '/rest/v1/rpc/' + fn, params || {}, t2); });
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// ---- here and there ----
|
|
79
|
+
function epOf(id) { var m = /^(tt\d+):(\d+):(\d+)$/.exec('' + id); return m ? { id: m[1], s: +m[2], e: +m[3] } : null; }
|
|
80
|
+
function progressKey(contentId, s, e) { return s ? contentId + '_s' + s + 'e' + e : contentId; }
|
|
81
|
+
// What changed here since `since`: { progress: [Nuvio entries], watched: [items], unwatched: [keys] }.
|
|
82
|
+
function changes(since) {
|
|
83
|
+
var out = { progress: [], watched: [], unwatched: [] };
|
|
84
|
+
var prog = U.store.get('stremiok.progress', {}) || {};
|
|
85
|
+
Object.keys(prog).forEach(function (k) {
|
|
86
|
+
var x = prog[k];
|
|
87
|
+
if (!x || !(x.updated > since) || x.updated === x.nv || !(x.position > 0)) { return; }
|
|
88
|
+
var ep = x.type === 'series' ? epOf(x.id) : null, cid = ep ? ep.id : '' + x.id;
|
|
89
|
+
if ((x.type !== 'movie' && !ep) || !TT.test(cid)) { return; }
|
|
90
|
+
out.progress.push({ content_id: cid, content_type: x.type, video_id: '' + x.id, season: ep ? ep.s : null, episode: ep ? ep.e : null,
|
|
91
|
+
position: Math.round(x.position * 1000), duration: Math.round((x.duration || 0) * 1000), last_watched: x.updated, progress_key: progressKey(cid, ep && ep.s, ep && ep.e) });
|
|
92
|
+
});
|
|
93
|
+
var w = U.store.get('stremiok.watched', {}) || {}, from = U.store.get('stremiok.nuvio.marks', {}) || {};
|
|
94
|
+
Object.keys(w).forEach(function (k) {
|
|
95
|
+
var v = w[k], m = /^(movie|series):(tt\d+)(?::(\d+):(\d+))?$/.exec(k);
|
|
96
|
+
if (!m || !(Math.abs(v) > since) || from[k] === v) { return; }
|
|
97
|
+
if (m[1] === 'series' && !m[3]) { return; }
|
|
98
|
+
if (v > 0) { out.watched.push({ content_id: m[2], content_type: m[1], title: '', season: m[3] ? +m[3] : null, episode: m[4] ? +m[4] : null, watched_at: v }); }
|
|
99
|
+
else { out.unwatched.push({ content_id: m[2], season: m[3] ? +m[3] : null, episode: m[4] ? +m[4] : null }); }
|
|
100
|
+
});
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
// ---- its Library ----
|
|
104
|
+
function lib() { var l = U.store.get(LIB_KEY, null); return { items: (l && l.items && typeof l.items === 'object') ? l.items : {}, out: (l && l.out && typeof l.out === 'object') ? l.out : {} }; }
|
|
105
|
+
function libSave(l) { U.store.set(LIB_KEY, l); }
|
|
106
|
+
function inLibrary(type, id) { return !!lib().items[type + ':' + id]; }
|
|
107
|
+
function setInLibrary(type, meta, on) {
|
|
108
|
+
if (!isConnected() || (type !== 'movie' && type !== 'series') || !meta || !meta.id) { return; }
|
|
109
|
+
var l = lib(), k = type + ':' + meta.id;
|
|
110
|
+
if (on) { l.items[k] = { id: '' + meta.id, type: type, name: ('' + (meta.name || meta.title || '')).slice(0, 200), poster: meta.poster || '', background: meta.background || '', added: Date.now() }; }
|
|
111
|
+
else { delete l.items[k]; }
|
|
112
|
+
l.out[k] = !!on;
|
|
113
|
+
libSave(l);
|
|
114
|
+
libChanged();
|
|
115
|
+
soon();
|
|
116
|
+
}
|
|
117
|
+
function libChanged() {
|
|
118
|
+
if (typeof Stremio !== 'undefined' && Stremio.addonsChanged) { Stremio.addonsChanged(); }
|
|
119
|
+
if (typeof ViewParts !== 'undefined' && ViewParts.refreshAddonRows) { ViewParts.refreshAddonRows('local:nuvio'); }
|
|
120
|
+
}
|
|
121
|
+
// What was added or removed here goes; then its whole Library, as Nuvio now has it.
|
|
122
|
+
function libSync(a) {
|
|
123
|
+
var l = lib(), adds = [], dels = [], sent = {};
|
|
124
|
+
Object.keys(l.out).forEach(function (k) {
|
|
125
|
+
var m = /^(movie|series):(.+)$/.exec(k);
|
|
126
|
+
if (!m) { return; }
|
|
127
|
+
sent[k] = l.out[k];
|
|
128
|
+
if (l.out[k] && l.items[k]) { var it = l.items[k]; adds.push({ content_id: it.id, content_type: it.type, name: it.name, poster: it.poster || null, poster_shape: 'POSTER', background: it.background || null, added_at: it.added }); }
|
|
129
|
+
else if (!l.out[k]) { dels.push({ content_id: m[2], content_type: m[1] }); }
|
|
130
|
+
});
|
|
131
|
+
var sends = [];
|
|
132
|
+
if (adds.length) { sends.push(rpc(a, 'sync_push_library_items', { p_items: adds, p_profile_id: a.profile })); }
|
|
133
|
+
if (dels.length) { sends.push(rpc(a, 'sync_delete_library_items', { p_keys: dels, p_profile_id: a.profile })); }
|
|
134
|
+
return Promise.all(sends).then(function () {
|
|
135
|
+
var got = [];
|
|
136
|
+
function page(off) {
|
|
137
|
+
return rpc(a, 'sync_pull_library', { p_profile_id: a.profile, p_limit: LIB_PAGE, p_offset: off }).then(function (r) {
|
|
138
|
+
var rows = U.listOf(r, LIB_PAGE, function (x) { return x && typeof x.content_id === 'string' && x.content_id && (x.content_type === 'movie' || x.content_type === 'series'); });
|
|
139
|
+
got = got.concat(rows);
|
|
140
|
+
return rows.length === LIB_PAGE && got.length < LIB_MAX ? page(off + LIB_PAGE) : got;
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
return page(0);
|
|
144
|
+
}).then(function (rows) {
|
|
145
|
+
var now = lib(), items = {};
|
|
146
|
+
rows.forEach(function (x) {
|
|
147
|
+
var at = +x.added_at || 0;
|
|
148
|
+
items[x.content_type + ':' + x.content_id] = { id: x.content_id, type: x.content_type, name: U.drawable('' + (x.name || '')).slice(0, 200), poster: typeof x.poster === 'string' ? x.poster : '',
|
|
149
|
+
background: typeof x.background === 'string' ? x.background : '', added: at < 1e11 ? at * 1000 : at };
|
|
150
|
+
});
|
|
151
|
+
// (a change made here meanwhile: kept, and sent next time)
|
|
152
|
+
Object.keys(now.out).forEach(function (k) {
|
|
153
|
+
if (k in sent && sent[k] === now.out[k]) { delete now.out[k]; return; }
|
|
154
|
+
if (now.out[k] && now.items[k]) { items[k] = now.items[k]; } else if (!now.out[k]) { delete items[k]; }
|
|
155
|
+
});
|
|
156
|
+
var before = JSON.stringify(Object.keys(now.items).sort()), after = JSON.stringify(Object.keys(items).sort());
|
|
157
|
+
libSave({ items: items, out: now.out });
|
|
158
|
+
if (before !== after) { libChanged(); return true; }
|
|
159
|
+
return false;
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
function libCatalog(catId, extra) {
|
|
163
|
+
var type = catId === 'nuvio_library_movies' ? 'movie' : 'series', skip = (extra && +extra.skip) || 0, items = lib().items;
|
|
164
|
+
var list = Object.keys(items).map(function (k) { return items[k]; }).filter(function (it) { return it.type === type; });
|
|
165
|
+
list.sort(function (x, y) { return (y.added || 0) - (x.added || 0); });
|
|
166
|
+
return Promise.resolve(list.slice(skip, skip + 100).map(function (it) { return { id: it.id, type: it.type, name: it.name || it.id, poster: it.poster, background: it.background, posterShape: 'poster' }; }));
|
|
167
|
+
}
|
|
168
|
+
// Its Library as an addon of StremioK's own (rows on Home and Discover), while connected.
|
|
169
|
+
function synthAddon() {
|
|
170
|
+
return {
|
|
171
|
+
__local: true, __impl: { catalog: libCatalog }, url: 'local:nuvio', base: 'local:nuvio',
|
|
172
|
+
manifest: { id: 'org.stremiok.nuvio-library', name: U.t('Nuvio Library'), version: '1.0.0', types: ['movie', 'series'], resources: ['catalog'],
|
|
173
|
+
catalogs: [{ type: 'movie', id: 'nuvio_library_movies', name: U.t('Nuvio Library'), extra: [{ name: 'skip' }] },
|
|
174
|
+
{ type: 'series', id: 'nuvio_library_shows', name: U.t('Nuvio Library'), extra: [{ name: 'skip' }] }] }
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
var _busy = null, _timer = null;
|
|
179
|
+
// One sync (one at a time: asked again meanwhile, the same one): resolves either way.
|
|
180
|
+
function sync() {
|
|
181
|
+
var a = auth();
|
|
182
|
+
if (!a || state().reconnect) { return Promise.resolve(false); }
|
|
183
|
+
if (_busy) { return _busy; }
|
|
184
|
+
var started = Date.now(), st = state(), c = changes(st.pushed || started), idx = a.profile;
|
|
185
|
+
var sends = [];
|
|
186
|
+
if (c.progress.length) { sends.push(rpc(a, 'sync_push_watch_progress', { p_entries: c.progress, p_profile_id: idx })); }
|
|
187
|
+
if (c.watched.length) { sends.push(rpc(a, 'sync_push_watched_items', { p_items: c.watched, p_profile_id: idx })); }
|
|
188
|
+
if (c.unwatched.length) { sends.push(rpc(a, 'sync_delete_watched_items', { p_keys: c.unwatched, p_profile_id: idx })); }
|
|
189
|
+
_busy = Promise.all(sends).then(function () {
|
|
190
|
+
setState({ pushed: started });
|
|
191
|
+
return Promise.all([
|
|
192
|
+
rpc(a, 'sync_pull_watch_progress', { p_profile_id: idx, p_limit: PROGRESS_PULL }),
|
|
193
|
+
rpc(a, 'sync_pull_watched_items', { p_profile_id: idx, p_page: 1, p_page_size: WATCHED_PULL })
|
|
194
|
+
]);
|
|
195
|
+
}).then(function (r) { return pullIn(r[0], r[1]); }).then(function (changed) {
|
|
196
|
+
return libSync(a).then(function (l) { return changed || l; });
|
|
197
|
+
}).then(function (changed) {
|
|
198
|
+
setState({ last: Date.now(), err: '' });
|
|
199
|
+
return changed;
|
|
200
|
+
}, function (e) {
|
|
201
|
+
if (!state().reconnect) { setState({ err: (e && e.message) || U.t('unknown') }); }
|
|
202
|
+
U.log('nuvio', 'Nuvio sync failed: ' + ((e && e.message) || 'unknown'));
|
|
203
|
+
return false;
|
|
204
|
+
}).then(function (v) { _busy = null; return v; });
|
|
205
|
+
return _busy;
|
|
206
|
+
}
|
|
207
|
+
// What Nuvio has, merged in (the newer wins): true when something changed here.
|
|
208
|
+
function pullIn(progress, watched) {
|
|
209
|
+
var changed = false, w = U.store.get('stremiok.watched', {}) || {}, from = U.store.get('stremiok.nuvio.marks', {}) || {};
|
|
210
|
+
U.listOf(watched, WATCHED_PULL, function (x) { return x && TT.test(x.content_id || '') && (x.content_type === 'movie' || x.content_type === 'series'); }).forEach(function (x) {
|
|
211
|
+
var ep = x.content_type === 'series' && x.season > 0 && x.episode > 0;
|
|
212
|
+
if (x.content_type === 'series' && !ep) { return; }
|
|
213
|
+
var k = x.content_type + ':' + x.content_id + (ep ? ':' + x.season + ':' + x.episode : ''), when = +x.watched_at || 0;
|
|
214
|
+
if (when < 1e11) { when *= 1000; }
|
|
215
|
+
if (!w[k] || when > Math.abs(w[k])) { w[k] = when; from[k] = when; changed = true; }
|
|
216
|
+
});
|
|
217
|
+
if (changed) { U.store.set('stremiok.watched', w); U.store.set('stremiok.nuvio.marks', from); }
|
|
218
|
+
var prog = U.store.get('stremiok.progress', {}) || {}, want = [];
|
|
219
|
+
U.listOf(progress, PROGRESS_PULL, function (x) { return x && TT.test(x.content_id || '') && (x.content_type === 'movie' || x.content_type === 'series') && +x.position > 0; }).forEach(function (x) {
|
|
220
|
+
var ep = x.content_type === 'series' && x.season > 0 && x.episode > 0;
|
|
221
|
+
if (x.content_type === 'series' && !ep) { return; }
|
|
222
|
+
var id = ep ? x.content_id + ':' + x.season + ':' + x.episode : x.content_id, k = x.content_type + ':' + id, at = +x.last_watched || 0;
|
|
223
|
+
if (at < 1e11) { at *= 1000; }
|
|
224
|
+
var mine = prog[k];
|
|
225
|
+
if (mine && (mine.updated || 0) >= at) { return; }
|
|
226
|
+
var done = w[k] > 0 && w[k] >= at; // (watched since: not brought back to Continue Watching)
|
|
227
|
+
if (done) { return; }
|
|
228
|
+
want.push({ k: k, x: x, id: id, ep: ep, at: at, mine: mine });
|
|
229
|
+
});
|
|
230
|
+
if (!want.length) { return Promise.resolve(changed); }
|
|
231
|
+
want.sort(function (p, q) { return q.at - p.at; });
|
|
232
|
+
var named = want.filter(function (y) { return y.mine && y.mine.name; }), unnamed = want.filter(function (y) { return !(y.mine && y.mine.name); }).slice(0, META_PER_SYNC);
|
|
233
|
+
return Promise.all(unnamed.map(function (y) {
|
|
234
|
+
return Promise.resolve(typeof Stremio !== 'undefined' && Stremio.getMeta ? Stremio.getMeta(y.x.content_type, y.x.content_id) : null).then(function (m) { y.meta = m; }, function () {});
|
|
235
|
+
})).then(function () {
|
|
236
|
+
prog = U.store.get('stremiok.progress', {}) || {};
|
|
237
|
+
named.concat(unnamed).forEach(function (y) {
|
|
238
|
+
var x = y.x, m = y.meta, ms = (+x.duration || 0) > 86400 || (+x.position || 0) > 86400;
|
|
239
|
+
var name = y.mine && y.mine.name ? y.mine.name : m && m.name ? (y.ep ? m.name + ' S' + x.season + 'E' + x.episode : m.name) : '';
|
|
240
|
+
if (!name) { return; }
|
|
241
|
+
prog[y.k] = Object.assign({}, y.mine || {}, { id: y.id, type: x.content_type, name: name, poster: (y.mine && y.mine.poster) || (m && m.poster), background: (y.mine && y.mine.background) || (m && m.background),
|
|
242
|
+
parent: y.ep ? x.content_id : undefined, position: Math.floor(ms ? x.position / 1000 : x.position), duration: Math.floor(ms ? x.duration / 1000 : x.duration), updated: y.at, nv: y.at });
|
|
243
|
+
changed = true;
|
|
244
|
+
});
|
|
245
|
+
U.store.set('stremiok.progress', prog);
|
|
246
|
+
return changed;
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
// Something changed here (played, marked): a sync a few seconds later.
|
|
250
|
+
function soon() {
|
|
251
|
+
if (!isConnected()) { return; }
|
|
252
|
+
if (_timer) { clearTimeout(_timer); }
|
|
253
|
+
_timer = setTimeout(function () { _timer = null; sync(); }, SOON_MS);
|
|
254
|
+
}
|
|
255
|
+
var _every = null;
|
|
256
|
+
function start(onChanged) {
|
|
257
|
+
if (!isConnected() || _every) { return; }
|
|
258
|
+
function go() { sync().then(function (ch) { if (ch && onChanged) { onChanged(); } }); }
|
|
259
|
+
go();
|
|
260
|
+
_every = setInterval(go, EVERY_MS);
|
|
261
|
+
}
|
|
262
|
+
// The kept session, renewed if need be, for a read of the import's: { cfg, token, profile }.
|
|
263
|
+
function session() {
|
|
264
|
+
var a = auth();
|
|
265
|
+
if (!a || state().reconnect) { return Promise.reject(new Error(U.t('Its sign-in no longer works'))); }
|
|
266
|
+
return token(a).then(function (t) { return { cfg: { url: a.url, key: a.key }, token: t, profile: a.profile }; });
|
|
267
|
+
}
|
|
268
|
+
return { session: session, inLibrary: inLibrary, setInLibrary: setInLibrary, synthAddon: synthAddon, isConnected: isConnected, connectStatus: connectStatus, connect: connect, connectFor: connectFor, disconnect: disconnect, sync: sync, soon: soon, start: start, KEY: KEY };
|
|
269
|
+
})();
|
package/js/player-core.js
CHANGED
|
@@ -189,6 +189,7 @@ var PlayerParts = {};
|
|
|
189
189
|
}
|
|
190
190
|
writeProgress(all);
|
|
191
191
|
if (typeof StremioSync !== 'undefined' && StremioSync.onProgress) { StremioSync.onProgress(S.ctx, S.cur, S.dur, !fromTicker); } // (your Stremio account's place)
|
|
192
|
+
if (!fromTicker && typeof NuvioSync !== 'undefined' && NuvioSync.soon) { NuvioSync.soon(); } // (a Nuvio account's: js/nuvio-sync.js)
|
|
192
193
|
}
|
|
193
194
|
// What's saved to resume: a converted playback's own link (not the server's conversion), and a
|
|
194
195
|
// torrent's infoHash, file and trackers (its link depends on the server set when it resumes).
|
package/js/player-store.js
CHANGED
|
@@ -62,7 +62,10 @@
|
|
|
62
62
|
tellTrakt(type, ids, on);
|
|
63
63
|
tellStremio(type, ids, on);
|
|
64
64
|
}
|
|
65
|
-
function tellStremio(type, ids, on) {
|
|
65
|
+
function tellStremio(type, ids, on) {
|
|
66
|
+
if (typeof StremioSync !== 'undefined' && StremioSync.onMarked) { StremioSync.onMarked(type, ids, !!on); }
|
|
67
|
+
if (typeof NuvioSync !== 'undefined' && NuvioSync.soon) { NuvioSync.soon(); } // (a Nuvio account's: js/nuvio-sync.js)
|
|
68
|
+
}
|
|
66
69
|
// A "not watched" Trakt has confirmed (and its copy shows): dropped here, so Trakt decides.
|
|
67
70
|
function clearUnwatched(type, id) {
|
|
68
71
|
var a = watchedAll(), k = type + ':' + id, v = a[k];
|
package/js/profiles.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/* Profiles: who's watching. Global namespace: Profiles
|
|
2
|
+
Each profile keeps its own addons, settings, progress, watched marks, history and sign-ins
|
|
3
|
+
(Trakt, a Stremio account, media servers); what's the TV's — playlists, USB drives and network
|
|
4
|
+
folders, plugins, API keys, the streaming server, what the TV plays, Diagnostics — every
|
|
5
|
+
profile shares (TV below).
|
|
6
|
+
- Kept as one list (stremiok.profiles, the TV's): each profile's id, name, color and slot.
|
|
7
|
+
- A profile's saved state is StremioK's usual keys under its slot: the first profile's are the
|
|
8
|
+
names StremioK has always used (stremiok.progress), another's carry its slot
|
|
9
|
+
(stremiok.p2.progress). With one profile nothing changes, nothing is moved, and an older
|
|
10
|
+
StremioK still finds its data.
|
|
11
|
+
- U.store and U.bigStore turn each key into the active profile's (key()): the rest of
|
|
12
|
+
StremioK keeps its key names. The active profile is the one StremioK started in: switching
|
|
13
|
+
starts StremioK again.
|
|
14
|
+
Loaded before everything else (it reads the TV's storage itself); nothing here needs U. */
|
|
15
|
+
var Profiles = (function () {
|
|
16
|
+
'use strict';
|
|
17
|
+
var KEY = 'stremiok.profiles', MAX = 5, SLOTS = ['', 'p2', 'p3', 'p4', 'p5'];
|
|
18
|
+
// The TV's own (every profile shares them): stremiok.<name>, or its own keys under <name>.
|
|
19
|
+
var TV = ['profiles', 'manifests', 'discoverempty', 'tmdbproviders', 'integrations', 'server', 'formats', 'resblock',
|
|
20
|
+
'appshortcuts', 'appsallowed', 'bigdb', 'importurl', 'aspects', 'iptv.playlists', 'iptv.stalkerall', 'iptv.stalkerkeep',
|
|
21
|
+
'iptv.every', 'iptv.size', 'liveformat', 'livefix', 'xtvod.cats', 'stvod.cats', 'local.index', 'local.indexes', 'local.matches',
|
|
22
|
+
'local.fixes', 'local.matching', 'local.hidden', 'netfolders', 'plugins', 'downloads', 'homesnap', 'homecache',
|
|
23
|
+
'homepreview', 'session', 'lastlog', 'startup'];
|
|
24
|
+
var TV_PREFIX = ['iptv.cache.', 'local.index.', 'plugins.code.'];
|
|
25
|
+
var SLOT_KEY = /^stremiok\.(p[2-9])\./; // (a profile's own key, other than the first's)
|
|
26
|
+
var COLORS = ['indigo', 'teal', 'orange', 'pink', 'green', 'red', 'purple', 'amber'];
|
|
27
|
+
|
|
28
|
+
function valid(d) {
|
|
29
|
+
if (!d || typeof d !== 'object' || !Array.isArray(d.list) || !d.list.length || d.list.length > MAX) { return false; }
|
|
30
|
+
var seen = {};
|
|
31
|
+
return d.list.every(function (p) {
|
|
32
|
+
var ok = p && typeof p.id === 'string' && /^[a-z0-9]{2,12}$/.test(p.id) && typeof p.name === 'string' && SLOTS.indexOf(p.slot) !== -1 && !seen['s' + p.slot] && !seen['i' + p.id];
|
|
33
|
+
if (ok) { seen['s' + p.slot] = 1; seen['i' + p.id] = 1; }
|
|
34
|
+
return ok;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function read() {
|
|
38
|
+
try { var d = JSON.parse(window.localStorage.getItem(KEY) || 'null'); return valid(d) ? d : null; } catch (e) { return null; }
|
|
39
|
+
}
|
|
40
|
+
var _data = read();
|
|
41
|
+
function active() {
|
|
42
|
+
if (!_data) { return null; }
|
|
43
|
+
for (var i = 0; i < _data.list.length; i++) { if (_data.list[i].id === _data.active) { return _data.list[i]; } }
|
|
44
|
+
return _data.list[0];
|
|
45
|
+
}
|
|
46
|
+
var _slot = (active() || { slot: '' }).slot; // (fixed for this run: switching starts StremioK again)
|
|
47
|
+
|
|
48
|
+
// A key of StremioK's: the TV's own, or a profile's.
|
|
49
|
+
function isTv(k) {
|
|
50
|
+
k = '' + k;
|
|
51
|
+
if (k.indexOf('stremiok.') !== 0) { return true; } // (not StremioK's: left alone)
|
|
52
|
+
var n = k.slice(9);
|
|
53
|
+
if (TV.indexOf(n) !== -1) { return true; }
|
|
54
|
+
for (var i = 0; i < TV_PREFIX.length; i++) { if (n.indexOf(TV_PREFIX[i]) === 0) { return true; } }
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
// The key as kept for the active profile: the TV's as it is; a profile's under its slot (none
|
|
58
|
+
// for the first profile). A key already under a slot is left as it is.
|
|
59
|
+
function key(k) {
|
|
60
|
+
k = '' + k;
|
|
61
|
+
if (!_slot || SLOT_KEY.test(k) || isTv(k)) { return k; }
|
|
62
|
+
return 'stremiok.' + _slot + '.' + k.slice(9);
|
|
63
|
+
}
|
|
64
|
+
function keyFor(slot, k) {
|
|
65
|
+
k = '' + k;
|
|
66
|
+
if (!slot || isTv(k)) { return k; }
|
|
67
|
+
return 'stremiok.' + slot + '.' + k.slice(9);
|
|
68
|
+
}
|
|
69
|
+
// Whose a kept key is: the slot of the profile it belongs to ('' the first's), or null (the TV's).
|
|
70
|
+
function slotOf(physical) {
|
|
71
|
+
var m = SLOT_KEY.exec('' + physical);
|
|
72
|
+
if (m) { return m[1]; }
|
|
73
|
+
return isTv(physical) ? null : '';
|
|
74
|
+
}
|
|
75
|
+
function list() { return _data ? _data.list.map(function (p) { return { id: p.id, name: p.name, color: p.color, slot: p.slot, at: p.at }; }) : []; }
|
|
76
|
+
|
|
77
|
+
// ---- changing the list (Settings › Profiles, the picker) ----
|
|
78
|
+
function save() { try { window.localStorage.setItem(KEY, JSON.stringify(_data)); return true; } catch (e) { return false; } }
|
|
79
|
+
function newId() {
|
|
80
|
+
var s = '', used = _data ? _data.list.map(function (p) { return p.id; }) : [];
|
|
81
|
+
do { s = ''; while (s.length < 6) { s += Math.floor(Math.random() * 36).toString(36); } } while (used.indexOf(s) !== -1); // (an id only: not a secret)
|
|
82
|
+
return s;
|
|
83
|
+
}
|
|
84
|
+
// The list made the first time a second profile is added: the profile StremioK has always
|
|
85
|
+
// run as becomes the first (firstName), in the slot it always had.
|
|
86
|
+
function ensure(firstName) {
|
|
87
|
+
if (_data) { return; }
|
|
88
|
+
_data = { v: 1, active: '', ask: true, list: [{ id: newId(), slot: '', name: firstName, color: COLORS[0], at: Date.now() }] };
|
|
89
|
+
_data.active = _data.list[0].id;
|
|
90
|
+
}
|
|
91
|
+
function find(id) { return _data ? _data.list.filter(function (p) { return p.id === id; })[0] || null : null; }
|
|
92
|
+
function cleanName(n) { return ('' + (n || '')).replace(/[\u0000-\u001f]/g, '').trim().slice(0, 24); }
|
|
93
|
+
// A new profile (MAX at most), in the first free slot: its entry, or null (full).
|
|
94
|
+
function add(name, color, firstName) {
|
|
95
|
+
ensure(firstName);
|
|
96
|
+
if (_data.list.length >= MAX) { return null; }
|
|
97
|
+
var taken = _data.list.map(function (p) { return p.slot; }), slot = SLOTS.filter(function (x) { return taken.indexOf(x) === -1; })[0];
|
|
98
|
+
var p = { id: newId(), slot: slot, name: cleanName(name) || 'Profile', color: COLORS.indexOf(color) !== -1 ? color : COLORS[_data.list.length % COLORS.length], at: Date.now() };
|
|
99
|
+
_data.list.push(p);
|
|
100
|
+
save();
|
|
101
|
+
return { id: p.id, name: p.name, color: p.color, slot: p.slot };
|
|
102
|
+
}
|
|
103
|
+
function rename(id, name) { var p = find(id); if (p && cleanName(name)) { p.name = cleanName(name); save(); } }
|
|
104
|
+
function recolor(id, color) { var p = find(id); if (p && COLORS.indexOf(color) !== -1) { p.color = color; save(); } }
|
|
105
|
+
function setAsk(on) { if (_data) { _data.ask = !!on; save(); } }
|
|
106
|
+
// The profile StremioK starts in next time (switching: then started again, see setActive's caller).
|
|
107
|
+
function setActive(id) { if (find(id)) { _data.active = id; save(); } }
|
|
108
|
+
// A profile's own keys in the settings storage: [physical key].
|
|
109
|
+
function keysOf(slot) {
|
|
110
|
+
var out = [];
|
|
111
|
+
try {
|
|
112
|
+
var ls = window.localStorage;
|
|
113
|
+
for (var i = 0; i < ls.length; i++) { var k = ls.key(i); if (k && slotOf(k) === slot && k !== KEY) { out.push(k); } }
|
|
114
|
+
} catch (e) {}
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
// What a new profile copies from the one it's made from, when asked to: its addons and their
|
|
118
|
+
// order, its settings and its media server sign-ins (not its history, nor its Trakt or Stremio
|
|
119
|
+
// account, nor a server's device id).
|
|
120
|
+
var COPY = ['addons', 'addonsoff', 'addonorigin', 'aiomanager', 'feeds', 'searchfeeds', 'sourcefeeds', 'ranking', 'youtube', 'prefs',
|
|
121
|
+
'subcfg', 'audiocfg', 'playercfg', 'jellyfin.servers', 'jellyfin.libs', 'plex.servers', 'plex.libs'];
|
|
122
|
+
function copyInto(toSlot, fromSlot) {
|
|
123
|
+
var n = 0;
|
|
124
|
+
COPY.forEach(function (k) {
|
|
125
|
+
try {
|
|
126
|
+
var v = window.localStorage.getItem(keyFor(fromSlot, 'stremiok.' + k));
|
|
127
|
+
if (v !== null) { window.localStorage.setItem(keyFor(toSlot, 'stremiok.' + k), v); n++; }
|
|
128
|
+
} catch (e) {}
|
|
129
|
+
});
|
|
130
|
+
return n;
|
|
131
|
+
}
|
|
132
|
+
// A profile gone: removed from the list, and everything it kept with it (the settings storage
|
|
133
|
+
// here; the TV's database through bigRemove(physical key), StremioK's own). Never the active
|
|
134
|
+
// one, nor the last.
|
|
135
|
+
var BIG = ['trakt.watched', 'media.watched', 'stremio.items'];
|
|
136
|
+
function remove(id, bigRemove) {
|
|
137
|
+
var p = find(id);
|
|
138
|
+
if (!p || p.id === _data.active || _data.list.length < 2) { return false; }
|
|
139
|
+
keysOf(p.slot).forEach(function (k) { try { window.localStorage.removeItem(k); } catch (e) {} });
|
|
140
|
+
if (bigRemove) { BIG.forEach(function (k) { bigRemove(keyFor(p.slot, 'stremiok.' + k)); }); }
|
|
141
|
+
_data.list = _data.list.filter(function (x) { return x.id !== id; });
|
|
142
|
+
save();
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
KEY: KEY, MAX: MAX, COLORS: COLORS,
|
|
148
|
+
key: key, keyFor: keyFor, isTv: isTv, slotOf: slotOf,
|
|
149
|
+
slot: function () { return _slot; },
|
|
150
|
+
list: list,
|
|
151
|
+
active: function () { var a = active(); return a ? { id: a.id, name: a.name, color: a.color, slot: a.slot } : null; },
|
|
152
|
+
count: function () { return _data ? _data.list.length : 1; },
|
|
153
|
+
ask: function () { return !!(_data && _data.list.length > 1 && _data.ask !== false); },
|
|
154
|
+
askSetting: function () { return !_data || _data.ask !== false; },
|
|
155
|
+
ensure: function (firstName) { ensure(firstName); save(); }, add: add, rename: rename, recolor: recolor, setAsk: setAsk, setActive: setActive, remove: remove, copyInto: copyInto, keysOf: keysOf, COPY: COPY,
|
|
156
|
+
__data: function () { return _data; }, __reload: function () { _data = read(); _slot = (active() || { slot: '' }).slot; }
|
|
157
|
+
};
|
|
158
|
+
})();
|