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:
@ -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;
|
||||
}
|
||||
|
||||
|
||||
61
server/services/epgMatchingService.js
Normal file
61
server/services/epgMatchingService.js
Normal 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 };
|
||||
61
server/services/epgSourceOrdering.js
Normal file
61
server/services/epgSourceOrdering.js
Normal 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 };
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user