Use channel configuration for visibility and ordering

This commit is contained in:
Gogs
2026-07-06 17:33:26 +02:00
parent 566ddacb2f
commit 249bcb1f1b
3 changed files with 95 additions and 8 deletions
+24 -1
View File
@@ -1 +1,24 @@
{}
{
"orf1.at": {
"uuid": "58a965b21f48319d1a4051476667f5e2",
"name": "ORF 1",
"icon": "\/logos\/orf1.png",
"country": "",
"language": "",
"enabled": true,
"favorite": false,
"number": 1,
"group": "Unknown"
},
"orf2.at": {
"uuid": "4c9dba34a821b5b25c8e1e2b65abc4df",
"name": "ORF 2",
"icon": "\/logos\/orf2.png",
"country": "",
"language": "",
"enabled": true,
"favorite": false,
"number": 2,
"group": "Unknown"
}
}
+5 -4
View File
@@ -1,8 +1,8 @@
<?php
require_once '/var/www/src/Config.php';
require_once '/var/www/src/XmlTv.php';
require_once '/var/www/src/View.php';
require_once '/var/www/src/Config.php';
$config = new Config(
'/var/www/config/config.json',
@@ -14,13 +14,14 @@ $xmltv = new XmlTv(
$config->get('timezone', 'Europe/Vienna')
);
$xmltv = new XmlTv('/var/www/epg/guide.xml', 'Europe/Vienna');
[$channels, $programmes] = $xmltv->load();
$xmltv->synchronizeChannels($config);
[$channels, $programmes] = $xmltv->load($config);
$contentTemplate = '/var/www/templates/home.php';
View::render('layout', [
'title' => $config->get('title'),
'title' => $config->get('title', 'TVGuide'),
'channels' => $channels,
'programmes' => $programmes,
'contentTemplate' => $contentTemplate,
+66 -3
View File
@@ -11,10 +11,11 @@ class XmlTv
$this->timezone = $timezone;
}
public function load(): array
public function load(?Config $config = null): array
{
$channels = [];
$programmes = [];
$channelConfig = $config ? $config->getChannels() : [];
if (!file_exists($this->file)) {
return [$channels, $programmes];
@@ -25,15 +26,27 @@ class XmlTv
foreach ($xml->channel as $channel) {
$id = (string)$channel['id'];
$cfg = $channelConfig[$id] ?? [];
if (($cfg['enabled'] ?? true) !== true) {
continue;
}
$channels[$id] = [
'id' => $id,
'name' => (string)$channel->{'display-name'},
'icon' => isset($channel->icon) ? (string)$channel->icon['src'] : null,
'name' => $cfg['name'] ?? (string)$channel->{'display-name'},
'icon' => $cfg['icon'] ?? (isset($channel->icon) ? (string)$channel->icon['src'] : null),
'number' => (int)($cfg['number'] ?? 999999),
'favorite' => (bool)($cfg['favorite'] ?? false),
];
}
foreach ($xml->programme as $programme) {
$channelId = (string)$programme['channel'];
if (!isset($channels[$channelId])) {
continue;
}
$start = $this->parseTime((string)$programme['start']);
$stop = $this->parseTime((string)$programme['stop']);
@@ -50,9 +63,59 @@ class XmlTv
];
}
uasort($channels, function ($a, $b) {
return ($a['number'] ?? 999999) <=> ($b['number'] ?? 999999);
});
return [$channels, $programmes];
}
public function synchronizeChannels(Config $config): void
{
if (!file_exists($this->file)) {
return;
}
$xml = simplexml_load_file($this->file);
$storedChannels = $config->getChannels();
$maxNumber = 0;
foreach ($storedChannels as $stored) {
$maxNumber = max($maxNumber, (int)($stored['number'] ?? 0));
}
foreach ($xml->channel as $channel) {
$id = (string)$channel['id'];
$name = (string)$channel->{'display-name'};
$icon = isset($channel->icon) ? (string)$channel->icon['src'] : '';
if (!isset($storedChannels[$id])) {
$maxNumber++;
$storedChannels[$id] = [
'uuid' => bin2hex(random_bytes(16)),
'name' => $name,
'icon' => $icon,
'country' => '',
'language' => '',
'enabled' => true,
'favorite' => false,
'number' => $maxNumber,
'group' => 'Unknown',
];
} else {
$storedChannels[$id]['name'] = $name;
$storedChannels[$id]['icon'] = $icon;
}
}
uasort($storedChannels, function ($a, $b) {
return ((int)($a['number'] ?? 999999)) <=> ((int)($b['number'] ?? 999999));
});
$config->saveChannels($storedChannels);
}
private function parseTime(string $value): ?DateTime
{
if (!preg_match('/^(\d{14})\s+([+-]\d{4})$/', trim($value), $m)) {