Files
sdk/src/sdk.js

98 lines
3.6 KiB
JavaScript

import SOURCES, { AGGREGATOR_SOURCES, DEDICATED_SOURCES, SOURCE_MAP } from "./config.js";
import EMBEDS, { EMBED_MAP } from "./embed_config.js";
import { fetchDownloads } from "./downloads/downloads.js";
import { probeSource } from "./health.js";
import { fetchSubtitles, getSubPathsMovie, getSubPathsTv } from "./subtitles.js";
import { createStreamArgs } from "./utils/helpers.js";
const ACTIVE_SOURCES = SOURCES.filter(cfg => !cfg.disabled);
const SOURCE_MODULES = Object.fromEntries(
await Promise.all(ACTIVE_SOURCES.map(async cfg => [cfg.key, await import(`./sources/${cfg.sourceFile}.js`)]))
);
export default class VylaSDK {
tmdbApiKey = null;
penguManifest = null;
constructor({ tmdbApiKey, penguManifest } = {}) {
this.tmdbApiKey = tmdbApiKey || null;
this.penguManifest = penguManifest || null;
}
getSources(excludeDisabled = false, dedicated = null) {
let list = SOURCES;
if (dedicated === true) {
list = DEDICATED_SOURCES;
} else if (dedicated === false) {
list = AGGREGATOR_SOURCES;
}
if (excludeDisabled) {
return list.filter(cfg => !cfg.disabled);
}
return list;
}
getEmbeds({ ads = null } = {}) {
if (typeof ads === 'boolean') {
return EMBEDS.filter(cfg => cfg.ads === ads);
}
return EMBEDS;
}
getEmbedUrl(key, id, s = null, e = null) {
const cfg = EMBED_MAP[key];
if (!cfg) throw new Error(`Embed with key "${key}" not found`);
const isTv = s != null && e != null;
const template = isTv ? cfg.tv : cfg.movie;
if (!template || id == null) return null;
let url = template.replace('{id}', encodeURIComponent(String(id)));
if (isTv) {
url = url
.replace('{s}', encodeURIComponent(String(s)))
.replace('{e}', encodeURIComponent(String(e)));
}
return url;
}
async probeAllSources() {
const active = SOURCES.filter(cfg => !cfg.disabled);
const results = await Promise.allSettled(
active.map(async cfg => await probeSource(this, cfg, SOURCE_MODULES[cfg.key]))
);
const sources = Object.fromEntries(
active.map((cfg, i) => [
cfg.key,
results[i].status === 'fulfilled' ? results[i].value : { ok: false, ms: null },
])
);
return sources;
}
async probeSource(key) {
const cfg = SOURCE_MAP[key];
if (!cfg) throw new Error(`Source with key "${key}" not found`);
const mod = SOURCE_MODULES[key];
if (!mod) throw new Error(`Source module for key "${key}" not found`);
return await probeSource(this, cfg, mod);
}
async getStream(key, id, s = null, e = null, clientIP = null) {
const cfg = SOURCE_MAP[key];
if (!cfg) throw new Error(`Source with key "${key}" not found`);
const mod = SOURCE_MODULES[key];
if (!mod) throw new Error(`Source module for key "${key}" not found`);
const streamArgs = await createStreamArgs(cfg, this, id, s, e, clientIP);
const result = await mod.getStream(streamArgs);
if (result && typeof result === 'object') {
delete result.subtitles;
}
return result;
}
async getSubtitles(id, s = null, e = null) {
const paths = s != null && e != null
? await getSubPathsTv(id, s, e)
: await getSubPathsMovie(id);
return await fetchSubtitles(paths);
}
async getDownloads(id, s = null, e = null) {
return await fetchDownloads(this, id, s, e);
}
}