46 lines
1.9 KiB
JavaScript
46 lines
1.9 KiB
JavaScript
// Stream filters (ported from mod/filter.js). Works on internal or public stream records.
|
|
const QUALITY_MIN = { any: 0, 480: 480, 720: 720, 1080: 1080, 2160: 2160 };
|
|
const AUDIO_OPTIONS = ['any', 'aac', 'ac3', 'dts', 'opus', 'mp3', 'flac', 'multi'];
|
|
const SUBTITLE_OPTIONS = ['any', 'required', 'lang'];
|
|
|
|
const langCode = (lang) => String(lang || '').toLowerCase().replace(/[^a-z]/g, '').slice(0, 2);
|
|
|
|
function hasSubtitleLang(subs, wanted) {
|
|
const want = langCode(wanted);
|
|
if (!want) return true;
|
|
return subs.some((s) => (langCode(s.lang ?? s.language) || langCode(s.label ?? s.name)) === want);
|
|
}
|
|
|
|
// query params override cfg.filters
|
|
function filtersFrom(searchParams, defaults = {}) {
|
|
const pick = (k, d) => searchParams.get(k) || defaults[k] || d;
|
|
return {
|
|
minQuality: String(pick('minQuality', 'any')).replace(/p$/i, ''),
|
|
audioCodec: pick('audioCodec', 'any'),
|
|
subtitles: pick('subtitles', 'any'),
|
|
subtitleLang: pick('subtitleLang', ''),
|
|
};
|
|
}
|
|
|
|
function applyFilters(streams, filters = {}) {
|
|
const minPixels = QUALITY_MIN[filters.minQuality] ?? 0;
|
|
return streams.filter((s) => {
|
|
if (minPixels > 0 && (s.pixels == null || s.pixels < minPixels)) return false;
|
|
if (filters.audioCodec && filters.audioCodec !== 'any' && s.audio !== filters.audioCodec) return false;
|
|
if (filters.subtitles && filters.subtitles !== 'any') {
|
|
if (!s.subtitles.length) return false;
|
|
if (filters.subtitles === 'lang' && filters.subtitleLang && !hasSubtitleLang(s.subtitles, filters.subtitleLang)) return false;
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
|
|
// pixels desc, then provider score desc (scoreOf(name) -> number)
|
|
function rankStreams(streams, scoreOf = () => 0) {
|
|
return streams
|
|
.slice()
|
|
.sort((a, b) => (b.pixels || 0) - (a.pixels || 0) || scoreOf(b.provider) - scoreOf(a.provider) || (b.seeds || 0) - (a.seeds || 0));
|
|
}
|
|
|
|
module.exports = { applyFilters, rankStreams, filtersFrom, QUALITY_MIN, AUDIO_OPTIONS, SUBTITLE_OPTIONS };
|