push
This commit is contained in:
1 parent
bcf05e8063
commit
f37985b4c8
109 files changed
+22810
-6143
No files matched your search
+96
-171
@@ -1,182 +1,107 @@
|
||||
/*
|
||||
CONFIG — where the API lives + which providers to query.
|
||||
config.js — where the API lives, the TMDB key, and which providers to query.
|
||||
|
||||
API base resolution order:
|
||||
1. ?api=<url> query param on any page (session override)
|
||||
2. localStorage (saved from Settings)
|
||||
3. built-in default
|
||||
1. ?api=<url> query param (session override, also read from a hash route)
|
||||
2. localStorage (saved from Settings) key: bug-tv.apiBase.v1
|
||||
3. <meta name="bug-tv-api" content="…"> in index.html (substituted at build time by nix)
|
||||
4. built-in default https://api-tv.bug.tools
|
||||
|
||||
Providers: until the user toggles anything in Settings, the server's own
|
||||
enabled list is used. Once customized, a {name: bool} map is stored in
|
||||
localStorage; names missing from the map default to enabled.
|
||||
Providers: until the user toggles anything in Settings, getProviders() returns null and
|
||||
the server's defaults apply. Once customized a {id: bool} map is stored (same format as v4,
|
||||
key bug-tv.providers.v1); getProviders() returns the enabled ids.
|
||||
|
||||
TMDB key: search/trending/details/images go straight to TMDB from the
|
||||
browser (the server only serves providers, streams and the media proxy).
|
||||
The key resolves from localStorage (Settings) or the built-in default.
|
||||
TMDB key: catalog calls go straight from the browser to TMDB.
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
import { getParam, safe } from './util.js';
|
||||
|
||||
const DEFAULT_BASE = 'https://api-tv.bug.tools';
|
||||
const STORAGE_KEY = 'bug-tv.apiBase.v1';
|
||||
const PROVIDER_KEY = 'bug-tv.providers.v1';
|
||||
export const FALLBACK_BASE = 'https://api-tv.bug.tools';
|
||||
export const STORAGE_KEY = 'bug-tv.apiBase.v1';
|
||||
export const PROVIDER_KEY = 'bug-tv.providers.v1';
|
||||
export const DEFAULT_TMDB_KEY = 'ac014b130a8e6344c91dff4e68b18d47';
|
||||
export const TMDB_KEY_STORAGE_KEY = 'bug-tv.tmdbKey.v1';
|
||||
|
||||
// Search/details/images go straight to TMDB from the browser; this key
|
||||
// only ever reaches api.themoviedb.org.
|
||||
const DEFAULT_TMDB_KEY = 'ac014b130a8e6344c91dff4e68b18d47';
|
||||
const TMDB_KEY_STORAGE_KEY = 'bug-tv.tmdbKey.v1';
|
||||
function normalize(value) {
|
||||
let v = String(value || '').trim();
|
||||
if (!v) return '';
|
||||
if (!/^https?:\/\//i.test(v)) v = `${/^(localhost|127\.|\[::1\])/.test(v) ? 'http' : 'https'}://${v}`;
|
||||
return v.replace(/\/+$/, '');
|
||||
}
|
||||
|
||||
function normalize(value) {
|
||||
let v = String(value || '').trim();
|
||||
if (!v) return '';
|
||||
if (!/^https?:\/\//i.test(v)) v = `https://${v}`;
|
||||
return v.replace(/\/+$/, '');
|
||||
function metaBase() {
|
||||
const el = typeof document !== 'undefined' && document.querySelector('meta[name="bug-tv-api"]');
|
||||
const v = el && el.getAttribute('content');
|
||||
// ignore unsubstituted placeholders like "@API_BASE@"
|
||||
return v && !/^@.*@$/.test(v.trim()) ? normalize(v) : '';
|
||||
}
|
||||
|
||||
export const DEFAULT_BASE = metaBase() || FALLBACK_BASE;
|
||||
|
||||
export function getBase() {
|
||||
const fromQuery = getParam('api');
|
||||
if (fromQuery) return normalize(fromQuery);
|
||||
const stored = safe(() => localStorage.getItem(STORAGE_KEY), null);
|
||||
if (stored) return normalize(stored);
|
||||
return DEFAULT_BASE;
|
||||
}
|
||||
|
||||
export function setBase(value) {
|
||||
const v = normalize(value);
|
||||
safe(() => (v ? localStorage.setItem(STORAGE_KEY, v) : localStorage.removeItem(STORAGE_KEY)));
|
||||
return getBase();
|
||||
}
|
||||
|
||||
export function clearBase() {
|
||||
safe(() => localStorage.removeItem(STORAGE_KEY));
|
||||
}
|
||||
|
||||
// Resolves API-relative URLs ("/v1/p/…") against the configured base; absolute URLs pass through.
|
||||
export function resolveUrl(relOrAbs) {
|
||||
const s = String(relOrAbs || '');
|
||||
if (/^[a-z][a-z0-9+.-]*:/i.test(s)) return s;
|
||||
return getBase() + (s.startsWith('/') ? s : `/${s}`);
|
||||
}
|
||||
|
||||
export function getTmdbKey() {
|
||||
const stored = safe(() => localStorage.getItem(TMDB_KEY_STORAGE_KEY), null);
|
||||
return stored && stored.trim() ? stored.trim() : DEFAULT_TMDB_KEY;
|
||||
}
|
||||
|
||||
export function setTmdbKey(value) {
|
||||
const v = String(value || '').trim();
|
||||
safe(() => (v ? localStorage.setItem(TMDB_KEY_STORAGE_KEY, v) : localStorage.removeItem(TMDB_KEY_STORAGE_KEY)));
|
||||
return getTmdbKey();
|
||||
}
|
||||
|
||||
export function clearTmdbKey() {
|
||||
safe(() => localStorage.removeItem(TMDB_KEY_STORAGE_KEY));
|
||||
}
|
||||
|
||||
// Raw {id: bool} map or null when the user never customized.
|
||||
export function getProviderMap() {
|
||||
const raw = safe(() => localStorage.getItem(PROVIDER_KEY), null);
|
||||
if (!raw) return null;
|
||||
const parsed = safe(() => JSON.parse(raw), null);
|
||||
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null;
|
||||
}
|
||||
|
||||
// Enabled provider ids, or null → use the server's defaults.
|
||||
export function getProviders() {
|
||||
const map = getProviderMap();
|
||||
return map ? Object.keys(map).filter((n) => map[n] !== false) : null;
|
||||
}
|
||||
|
||||
// Accepts a list of enabled ids or an {id: bool} map; null/undefined clears the override.
|
||||
export function setProviders(listOrMap) {
|
||||
if (listOrMap == null) return clearProviders();
|
||||
let map = listOrMap;
|
||||
if (Array.isArray(listOrMap)) {
|
||||
map = {};
|
||||
for (const id of listOrMap) map[id] = true;
|
||||
}
|
||||
safe(() => localStorage.setItem(PROVIDER_KEY, JSON.stringify(map)));
|
||||
}
|
||||
|
||||
function getBase() {
|
||||
const fromQuery = SR.util.getParam('api');
|
||||
if (fromQuery) return normalize(fromQuery);
|
||||
try {
|
||||
const stored = localStorage.getItem(STORAGE_KEY);
|
||||
if (stored) return normalize(stored);
|
||||
} catch (e) {
|
||||
/* private mode etc. */
|
||||
}
|
||||
return DEFAULT_BASE;
|
||||
}
|
||||
|
||||
function setBase(value) {
|
||||
const v = normalize(value);
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, v);
|
||||
} catch (e) {
|
||||
/* ignore */
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
function clearBase() {
|
||||
try {
|
||||
localStorage.removeItem(STORAGE_KEY);
|
||||
} catch (e) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
function getTmdbKey() {
|
||||
try {
|
||||
const stored = localStorage.getItem(TMDB_KEY_STORAGE_KEY);
|
||||
if (stored) return stored.trim();
|
||||
} catch (e) {
|
||||
/* private mode etc. */
|
||||
}
|
||||
return DEFAULT_TMDB_KEY;
|
||||
}
|
||||
|
||||
function setTmdbKey(value) {
|
||||
const v = String(value || '').trim();
|
||||
try {
|
||||
localStorage.setItem(TMDB_KEY_STORAGE_KEY, v);
|
||||
} catch (e) {
|
||||
/* ignore */
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
function clearTmdbKey() {
|
||||
try {
|
||||
localStorage.removeItem(TMDB_KEY_STORAGE_KEY);
|
||||
} catch (e) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
// null → user has not customized; caller should fall back to server defaults
|
||||
function getProviders() {
|
||||
try {
|
||||
const raw = localStorage.getItem(PROVIDER_KEY);
|
||||
if (!raw) return null;
|
||||
const parsed = JSON.parse(raw);
|
||||
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function setProviders(map) {
|
||||
try {
|
||||
localStorage.setItem(PROVIDER_KEY, JSON.stringify(map));
|
||||
} catch (e) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
function clearProviders() {
|
||||
try {
|
||||
localStorage.removeItem(PROVIDER_KEY);
|
||||
} catch (e) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
const PREFERRED_PROVIDERS = ['cineby', 'playimdb', 'vidrock'];
|
||||
|
||||
// Merge the server's provider lists with the user's toggles.
|
||||
// Returns the enabled provider names, in the server's `all` order.
|
||||
function enabledProviders(allNames, serverEnabled) {
|
||||
const all = Array.isArray(allNames) ? allNames : [];
|
||||
const overrides = getProviders();
|
||||
if (overrides) {
|
||||
return Object.keys(overrides).filter((n) => overrides[n] !== false);
|
||||
}
|
||||
const server = Array.isArray(serverEnabled) ? serverEnabled : [];
|
||||
const wanted = new Set([...PREFERRED_PROVIDERS, ...server]);
|
||||
return (all.length ? all : PREFERRED_PROVIDERS.slice()).filter((n) =>
|
||||
wanted.has(n)
|
||||
);
|
||||
}
|
||||
|
||||
// Watch-page resolver. Re-reads localStorage on every call so provider
|
||||
// changes made in Settings apply to the next lookup without a reload.
|
||||
function resolveProviders(catalog) {
|
||||
const all = Array.isArray(catalog && catalog.all) ? catalog.all : [];
|
||||
const serverEnabled = Array.isArray(catalog && catalog.enabled) ? catalog.enabled : [];
|
||||
const overrides = getProviders();
|
||||
if (overrides) {
|
||||
return {
|
||||
names: Object.keys(overrides).filter((n) => overrides[n] !== false),
|
||||
customized: true,
|
||||
useServerDefaults: false,
|
||||
};
|
||||
}
|
||||
const wanted = new Set([...PREFERRED_PROVIDERS, ...serverEnabled]);
|
||||
const names = all.length
|
||||
? all.filter((n) => wanted.has(n))
|
||||
: PREFERRED_PROVIDERS.slice();
|
||||
return {
|
||||
names,
|
||||
customized: false,
|
||||
useServerDefaults: !names.length,
|
||||
};
|
||||
}
|
||||
|
||||
window.SR = window.SR || {};
|
||||
SR.config = {
|
||||
DEFAULT_BASE,
|
||||
STORAGE_KEY,
|
||||
PROVIDER_KEY,
|
||||
DEFAULT_TMDB_KEY,
|
||||
TMDB_KEY_STORAGE_KEY,
|
||||
getBase,
|
||||
setBase,
|
||||
clearBase,
|
||||
getTmdbKey,
|
||||
setTmdbKey,
|
||||
clearTmdbKey,
|
||||
getProviders,
|
||||
setProviders,
|
||||
clearProviders,
|
||||
enabledProviders,
|
||||
resolveProviders,
|
||||
};
|
||||
})();
|
||||
export function clearProviders() {
|
||||
safe(() => localStorage.removeItem(PROVIDER_KEY));
|
||||
}
|
||||
Reference in new issue
Block a user