Channel Logos
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
$channelsFile = '/opt/tvguide-docker/app/config/channels.json';
|
||||
$sourceDir = $argv[1] ?? null;
|
||||
$targetDir = '/opt/tvguide-docker/app/public/assets/logos';
|
||||
|
||||
if (!$sourceDir || !is_dir($sourceDir)) {
|
||||
fwrite(STDERR, "Usage: php app/bin/import-logos.php /path/to/logo-repo\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$channels = json_decode(file_get_contents($channelsFile), true);
|
||||
|
||||
if (!is_array($channels)) {
|
||||
fwrite(STDERR, "Could not read channels.json\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@mkdir($targetDir, 0775, true);
|
||||
|
||||
$extensions = ['svg', 'png', 'jpg', 'jpeg', 'webp'];
|
||||
$files = [];
|
||||
|
||||
$iterator = new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator($sourceDir, FilesystemIterator::SKIP_DOTS)
|
||||
);
|
||||
|
||||
foreach ($iterator as $file) {
|
||||
if (!$file->isFile()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$ext = strtolower($file->getExtension());
|
||||
|
||||
if (!in_array($ext, $extensions, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$basename = pathinfo($file->getFilename(), PATHINFO_FILENAME);
|
||||
$key = normalize($basename);
|
||||
|
||||
if (!isset($files[$key])) {
|
||||
$files[$key] = [];
|
||||
}
|
||||
|
||||
$files[$key][] = $file->getPathname();
|
||||
}
|
||||
|
||||
$copied = 0;
|
||||
$missing = 0;
|
||||
$skipped = 0;
|
||||
|
||||
foreach ($channels as $id => $channel) {
|
||||
if (hasExistingLogo($targetDir, $id, $extensions)) {
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$names = candidateNames($id, $channel);
|
||||
$match = null;
|
||||
|
||||
foreach ($names as $name) {
|
||||
$key = normalize($name);
|
||||
|
||||
if (isset($files[$key])) {
|
||||
$match = pickBestFile($files[$key]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$match) {
|
||||
$missing++;
|
||||
echo "missing: {$id} | " . ($channel['name'] ?? '') . PHP_EOL;
|
||||
continue;
|
||||
}
|
||||
|
||||
$ext = strtolower(pathinfo($match, PATHINFO_EXTENSION));
|
||||
$target = $targetDir . '/' . $id . '.' . $ext;
|
||||
|
||||
copy($match, $target);
|
||||
$copied++;
|
||||
|
||||
echo "copied: {$id} <- {$match}" . PHP_EOL;
|
||||
}
|
||||
|
||||
echo PHP_EOL;
|
||||
echo "copied: {$copied}" . PHP_EOL;
|
||||
echo "skipped: {$skipped}" . PHP_EOL;
|
||||
echo "missing: {$missing}" . PHP_EOL;
|
||||
|
||||
function candidateNames(string $id, array $channel): array
|
||||
{
|
||||
$name = $channel['name'] ?? $id;
|
||||
|
||||
$baseId = preg_replace('/\.(at|de|ch)$/i', '', $id);
|
||||
|
||||
return array_unique(array_filter([
|
||||
$id,
|
||||
$baseId,
|
||||
$name,
|
||||
str_replace(['+', '&'], ['plus', 'and'], $name),
|
||||
str_replace([' HD', ' SD', ' UHD'], '', $name),
|
||||
str_replace([' eins'], [' 1'], $name),
|
||||
str_replace(['III'], ['3'], $name),
|
||||
str_replace(['II'], ['2'], $name),
|
||||
]));
|
||||
}
|
||||
|
||||
function normalize(string $value): string
|
||||
{
|
||||
$value = strtolower($value);
|
||||
$value = str_replace(['ä', 'ö', 'ü', 'ß'], ['ae', 'oe', 'ue', 'ss'], $value);
|
||||
$value = preg_replace('/\.(at|de|ch)$/', '', $value);
|
||||
$value = preg_replace('/[^a-z0-9]+/', '', $value);
|
||||
return trim($value);
|
||||
}
|
||||
|
||||
function hasExistingLogo(string $dir, string $id, array $extensions): bool
|
||||
{
|
||||
foreach ($extensions as $ext) {
|
||||
if (file_exists($dir . '/' . $id . '.' . $ext)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function pickBestFile(array $files): string
|
||||
{
|
||||
usort($files, function ($a, $b) {
|
||||
$priority = ['svg' => 0, 'png' => 1, 'webp' => 2, 'jpg' => 3, 'jpeg' => 4];
|
||||
|
||||
$ea = strtolower(pathinfo($a, PATHINFO_EXTENSION));
|
||||
$eb = strtolower(pathinfo($b, PATHINFO_EXTENSION));
|
||||
|
||||
return ($priority[$ea] ?? 99) <=> ($priority[$eb] ?? 99);
|
||||
});
|
||||
|
||||
return $files[0];
|
||||
}
|
||||
Reference in New Issue
Block a user