feat(epg): order EPG sources by priority (#24)

Add persistent EPG priority ordering, source reorder validation, and priority-aware EPG matching for live channels.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
blomios
2026-10-06 11:58:50 +02:00
parent 9718155e75
commit afe91089e5
8 changed files with 254 additions and 8 deletions

View File

@ -2,6 +2,7 @@ const fs = require('fs/promises');
const path = require('path');
const { existsSync, mkdirSync } = require('fs');
const { ensureBuiltinSources } = require('./services/builtinEpgRegistry');
const { applyEpgOrder, nextEpgPriority, sortEpgSources } = require('./services/epgSourceOrdering');
// Ensure data directory exists (sync is fine for startup)
const dataDir = path.join(__dirname, '..', 'data');
@ -18,7 +19,8 @@ async function loadDb() {
try {
const fileContent = await fs.readFile(dbPath, 'utf-8');
const data = JSON.parse(fileContent);
return ensureBuiltinSources({
const original = JSON.stringify(data);
const migrated = ensureBuiltinSources({
sources: data.sources || [],
hiddenItems: data.hiddenItems || [],
favorites: data.favorites || [],
@ -26,6 +28,8 @@ async function loadDb() {
users: data.users || [],
nextId: data.nextId || 1
});
if (original !== JSON.stringify(migrated)) await saveDb(migrated);
return migrated;
} catch (error) {
if (error.code === 'ENOENT') {
// File doesn't exist, return default
@ -137,7 +141,8 @@ async function saveDb(data) {
const sources = {
async getAll() {
const db = await loadDb();
return db.sources;
const epg = sortEpgSources(db.sources);
return [...db.sources.filter(source => source.type !== 'epg'), ...epg];
},
async getById(id) {
@ -147,7 +152,8 @@ const sources = {
async getByType(type) {
const db = await loadDb();
return db.sources.filter(s => s.type === type && s.enabled);
const matching = db.sources.filter(s => s.type === type && s.enabled);
return type === 'epg' ? sortEpgSources(matching) : matching;
},
async create(source) {
@ -155,6 +161,7 @@ const sources = {
const newSource = {
id: db.nextId++,
...source,
...(source.type === 'epg' ? { epgPriority: nextEpgPriority(db.sources) } : {}),
enabled: true,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString()
@ -203,6 +210,13 @@ const sources = {
await saveDb(db);
}
return source;
},
async reorderEpg(orderedIds) {
const db = await loadDb();
const ordered = applyEpgOrder(db.sources, orderedIds);
await saveDb(db);
return ordered;
}
};

View File

@ -3,6 +3,7 @@ const { requireAuth, requireAdmin } = require('../auth');
const liveProjection = require('../services/liveProjectionService');
const iptvOrgCatalog = require('../services/iptvOrgCatalogService');
const { getDb } = require('../db/sqlite');
const { sources } = require('../db');
const router = express.Router();
@ -44,7 +45,8 @@ router.get('/channels', requireAuth, async (req, res) => {
if (state.status !== 'success' && state.status !== 'building') {
state = await liveProjection.startRebuild().promise;
}
const allChannels = liveProjection.getChannels({ sourceId, includeHidden, userId: req.user.id });
const epgSources = await sources.getByType('epg');
const allChannels = liveProjection.getChannels({ sourceId, includeHidden, userId: req.user.id, epgSources });
const availableLanguages = [...new Map(allChannels.map(channel => [channel.language.code, channel.language])).values()]
.map(language => ({ code: language.code, short: language.short, label: language.label }))
.sort((left, right) => left.label.localeCompare(right.label));

View File

@ -47,6 +47,20 @@ router.get('/type/:type', async (req, res) => {
}
});
router.put('/epg/order', async (req, res) => {
try {
const ordered = await sources.reorderEpg(req.body?.orderedIds);
res.json(ordered.map(source => ({
...source,
password: source.password ? '••••••••' : null
})));
} catch (err) {
if (err.code) return res.status(400).json({ error: err.message, code: err.code });
console.error('Error reordering EPG sources:', err);
res.status(500).json({ error: 'Failed to reorder EPG sources' });
}
});
// Get single source
router.get('/:id', async (req, res) => {
try {

View File

@ -1,6 +1,6 @@
const BUILTIN_EPG_SOURCES = Object.freeze([
Object.freeze({ builtinKey: 'epgpw-fr', type: 'epg', name: 'EPG.PW France', url: 'https://epg.pw/xmltv/epg_FR.xml.gz', provider: 'EPG.PW', language: 'fr', country: 'FR', priority: 100 }),
Object.freeze({ builtinKey: 'epgpw-us', type: 'epg', name: 'EPG.PW United States', url: 'https://epg.pw/xmltv/epg_US.xml.gz', provider: 'EPG.PW', language: 'en', country: 'US', priority: 90 })
Object.freeze({ builtinKey: 'epgpw-fr', type: 'epg', name: 'EPG.PW France', url: 'https://epg.pw/xmltv/epg_FR.xml.gz', provider: 'EPG.PW', language: 'fr', country: 'FR' }),
Object.freeze({ builtinKey: 'epgpw-us', type: 'epg', name: 'EPG.PW United States', url: 'https://epg.pw/xmltv/epg_US.xml.gz', provider: 'EPG.PW', language: 'en', country: 'US' })
]);
function protectedFields(source) {
@ -9,11 +9,14 @@ function protectedFields(source) {
function ensureBuiltinSources(database) {
database.sources ||= [];
const { migrateEpgPriorities, nextEpgPriority } = require('./epgSourceOrdering');
migrateEpgPriorities(database);
for (const definition of BUILTIN_EPG_SOURCES) {
const existing = database.sources.find(source => source.builtinKey === definition.builtinKey);
if (existing) Object.assign(existing, definition, protectedFields(definition));
else database.sources.push({ id: database.nextId++, ...definition, ...protectedFields(definition), enabled: false, created_at: new Date().toISOString(), updated_at: new Date().toISOString() });
else database.sources.push({ id: database.nextId++, ...definition, epgPriority: nextEpgPriority(database.sources), ...protectedFields(definition), enabled: false, created_at: new Date().toISOString(), updated_at: new Date().toISOString() });
}
migrateEpgPriorities(database);
return database;
}

View File

@ -0,0 +1,61 @@
const { numericPriority, sortEpgSources } = require('./epgSourceOrdering');
function normalize(value) {
return String(value || '').normalize('NFKD').replace(/[\u0300-\u036f]/g, '')
.toLowerCase().replace(/[^a-z0-9]+/g, ' ').trim();
}
function candidateScore(channel, epgChannel) {
const variantIds = new Set((channel.variants || []).flatMap(variant => [variant.tvgId, variant.epg_channel_id]).filter(Boolean).map(String));
if (variantIds.has(String(epgChannel.id))) return 100;
const wantedNames = new Set([channel.standardName, channel.displayName, ...(channel.variants || []).map(variant => variant.name)].map(normalize).filter(Boolean));
const candidateName = normalize(epgChannel.name);
if (wantedNames.has(candidateName)) return 90;
if (candidateName && [...wantedNames].some(name => name.includes(candidateName) || candidateName.includes(name))) return 50;
return 0;
}
function chooseBestMatch(channel, candidates, epgSources) {
const language = channel.language?.code;
const eligible = sortEpgSources(epgSources, { enabledOnly: true }).filter(source =>
!source.language || !language || language === 'und' || source.language === language
);
const priorities = new Map(eligible.map(source => [source.id, numericPriority(source)]));
return candidates
.filter(candidate => priorities.has(candidate.sourceId))
.map(candidate => ({ ...candidate, matchConfidence: candidateScore(channel, candidate) }))
.filter(candidate => candidate.matchConfidence > 0)
.sort((left, right) => right.matchConfidence - left.matchConfidence ||
priorities.get(left.sourceId) - priorities.get(right.sourceId) || left.sourceId - right.sourceId)[0] || null;
}
function findBestMatch(database, channel, epgSources, now = Date.now()) {
const language = channel.language?.code;
const enabled = sortEpgSources(epgSources, { enabledOnly: true }).filter(source =>
!source.language || !language || language === 'und' || source.language === language
);
if (!enabled.length) return null;
const ids = enabled.map(source => source.id);
const placeholders = ids.map(() => '?').join(',');
const epgChannels = database.prepare(`
SELECT source_id AS sourceId, item_id AS id, name
FROM playlist_items
WHERE type = 'epg_channel' AND source_id IN (${placeholders})
`).all(...ids);
const best = chooseBestMatch(channel, epgChannels, enabled);
if (!best) return null;
const current = database.prepare(`
SELECT title, description, start_time AS startTime, end_time AS endTime
FROM epg_programs
WHERE source_id = ? AND channel_id = ? AND start_time <= ? AND end_time > ?
ORDER BY start_time DESC LIMIT 1
`).get(best.sourceId, best.id, now, now);
return {
sourceId: best.sourceId,
channelId: best.id,
matchConfidence: best.matchConfidence,
currentProgram: current || undefined
};
}
module.exports = { candidateScore, chooseBestMatch, findBestMatch, normalize };

View File

@ -0,0 +1,61 @@
const PRIORITY_STEP = 10;
function numericPriority(source) {
return Number.isInteger(source.epgPriority) && source.epgPriority >= 0
? source.epgPriority
: Number.MAX_SAFE_INTEGER;
}
function sortEpgSources(list, { enabledOnly = false } = {}) {
return list
.filter(source => source.type === 'epg' && (!enabledOnly || source.enabled))
.slice()
.sort((left, right) => numericPriority(left) - numericPriority(right) || left.id - right.id);
}
function migrateEpgPriorities(database) {
const epgSources = database.sources.filter(source => source.type === 'epg');
let next = epgSources.reduce((maximum, source) => Number.isInteger(source.epgPriority)
? Math.max(maximum, source.epgPriority + PRIORITY_STEP)
: maximum, 0);
for (const source of epgSources) {
if (!Number.isInteger(source.epgPriority) || source.epgPriority < 0) {
source.epgPriority = next;
next += PRIORITY_STEP;
}
}
return database;
}
function nextEpgPriority(sources) {
return sortEpgSources(sources).reduce((maximum, source) => Math.max(maximum, numericPriority(source) + PRIORITY_STEP), 0);
}
function validateCompleteOrder(allSources, orderedIds) {
if (!Array.isArray(orderedIds)) return { code: 'INVALID_ORDER', message: 'orderedIds must be an array' };
if (orderedIds.some(id => !Number.isInteger(id) || id <= 0)) return { code: 'INVALID_ORDER_ID', message: 'orderedIds must contain positive integer IDs' };
if (new Set(orderedIds).size !== orderedIds.length) return { code: 'DUPLICATE_SOURCE_ID', message: 'orderedIds contains duplicate IDs' };
const byId = new Map(allSources.map(source => [source.id, source]));
const unknown = orderedIds.find(id => !byId.has(id));
if (unknown !== undefined) return { code: 'UNKNOWN_SOURCE_ID', message: `Unknown source ID: ${unknown}` };
const nonEpg = orderedIds.find(id => byId.get(id).type !== 'epg');
if (nonEpg !== undefined) return { code: 'SOURCE_NOT_EPG', message: `Source ${nonEpg} is not an EPG source` };
const expected = allSources.filter(source => source.type === 'epg').map(source => source.id);
if (orderedIds.length !== expected.length || expected.some(id => !orderedIds.includes(id))) {
return { code: 'INCOMPLETE_EPG_ORDER', message: 'orderedIds must contain every EPG source' };
}
return null;
}
function applyEpgOrder(allSources, orderedIds, updatedAt = new Date().toISOString()) {
const error = validateCompleteOrder(allSources, orderedIds);
if (error) throw Object.assign(new Error(error.message), error);
orderedIds.forEach((id, index) => {
const source = allSources.find(item => item.id === id);
source.epgPriority = index * PRIORITY_STEP;
source.updated_at = updatedAt;
});
return sortEpgSources(allSources);
}
module.exports = { applyEpgOrder, migrateEpgPriorities, nextEpgPriority, numericPriority, PRIORITY_STEP, sortEpgSources, validateCompleteOrder };

View File

@ -3,6 +3,7 @@ const { pairKey, scoreCandidate, identity } = require('./liveGroupingScoring');
const { detectLanguage } = require('./channelLanguage');
const { matchesSearch } = require('./flexibleChannelSearch');
const iptvOrgCatalog = require('./iptvOrgCatalogService');
const epgMatching = require('./epgMatchingService');
const QUALITY_TOKENS = new Set([
'4k', 'uhd', 'fhd', 'fullhd', 'hd', 'sd', 'hevc', 'h265', 'h264',
@ -345,7 +346,7 @@ function parseData(value) {
try { return value ? JSON.parse(value) : {}; } catch { return {}; }
}
function getChannels({ sourceId = null, includeHidden = false, userId = null, q = '', languages = [] }, database = null) {
function getChannels({ sourceId = null, includeHidden = false, userId = null, q = '', languages = [], epgSources = [] }, database = null) {
database ||= defaultDatabase();
const params = [];
let sourceFilter = '';
@ -371,6 +372,8 @@ function getChannels({ sourceId = null, includeHidden = false, userId = null, q
channel.standardName = match.name;
channel.standard = { provider: 'iptv-org', channelId: match.channelId, country: match.country, confidence: match.confidence };
}
const epgMatch = epgMatching.findBestMatch(database, channel, epgSources);
if (epgMatch) channel.epg = { ...channel.epg, ...epgMatch, language: channel.language.code };
return channel;
}).filter(channel =>
(!languages.length || languages.includes(channel.language.code)) && matchesSearch(channel, q)