index verbessert

This commit is contained in:
Eskimue
2026-05-15 16:37:53 +02:00
parent ce1781624a
commit 65be4bdf9b
+107 -88
View File
@@ -119,6 +119,68 @@ if (!function_exists('planetVisibilityClass')) {
return ['class' => 'planet-visibility-bad', 'label' => 'am Tag']; return ['class' => 'planet-visibility-bad', 'label' => 'am Tag'];
} }
} }
if (!function_exists('indexPythonCandidates')) {
function indexPythonCandidates(): array
{
if (PHP_OS_FAMILY === 'Windows') {
$candidates = [];
$localAppData = getenv('LOCALAPPDATA');
if (is_string($localAppData) && $localAppData !== '') {
$localPython = $localAppData . '\\Python\\bin\\python.exe';
if (is_file($localPython)) {
$candidates[] = escapeshellarg($localPython);
}
}
return array_values(array_unique(array_merge(
$candidates,
['py -3', 'py', 'python']
)));
}
return ['/usr/bin/python3', '/usr/bin/python', 'python3', 'python'];
}
}
if (!function_exists('runIndexPythonCommand')) {
function runIndexPythonCommand(string $scriptPath, array $args): array
{
putenv('PYTHONUTF8=1');
putenv('PYTHONIOENCODING=utf-8');
$escapedArguments = array_map('escapeshellarg', array_merge([$scriptPath], $args));
$lastError = 'Python konnte nicht gestartet werden.';
foreach (indexPythonCandidates() as $pythonBinary) {
$command = $pythonBinary . ' ' . implode(' ', $escapedArguments) . ' 2>&1';
$output = [];
$resultCode = 0;
exec($command, $output, $resultCode);
$joined = trim(implode("\n", $output));
$decoded = json_decode($joined, true);
if (is_array($decoded)) {
return [
'ok' => (bool) ($decoded['ok'] ?? false),
'data' => $decoded,
'raw' => $joined,
'command' => $command,
];
}
if ($joined !== '') {
$lastError = $joined;
} elseif ($resultCode !== 0) {
$lastError = 'Fehlercode ' . $resultCode . ' bei ' . $pythonBinary;
}
}
return [
'ok' => false,
'error' => $lastError,
];
}
}
function idx_tleChecksum(string $line): int function idx_tleChecksum(string $line): int
{ {
@@ -227,82 +289,47 @@ if ($loggedIn) {
$date = (new DateTimeImmutable('now', new DateTimeZone($tz)))->format('Y-m-d'); $date = (new DateTimeImmutable('now', new DateTimeZone($tz)))->format('Y-m-d');
$scriptPath = realpath(__DIR__ . '/py/api.py'); $scriptPath = realpath(__DIR__ . '/py/api.py');
if (PHP_OS_FAMILY === 'Windows') {
$pythonBin = 'python';
} elseif (file_exists('/usr/bin/python3')) {
$pythonBin = '/usr/bin/python3';
} elseif (file_exists('/usr/bin/python')) {
$pythonBin = '/usr/bin/python';
} else {
$pythonBin = 'python';
}
putenv('PYTHONUTF8=1');
putenv('PYTHONIOENCODING=utf-8');
// Sonne + Mond // Sonne + Mond
$args = [ $pythonResult = runIndexPythonCommand($scriptPath, [
$pythonBin, 'sun_moon_rise_set',
escapeshellarg($scriptPath), (string)$lat,
escapeshellarg('sun_moon_rise_set'), (string)$lon,
escapeshellarg((string)$lat), (string)$elev,
escapeshellarg((string)$lon), $date,
escapeshellarg((string)$elev), $tz,
escapeshellarg($date), ]);
escapeshellarg($tz), if ($pythonResult['ok'] ?? false) {
]; $skyData = $pythonResult['data'];
$cmd = implode(' ', $args) . ' 2>&1';
$out = [];
$resultCode = 0;
exec($cmd, $out, $resultCode);
$rawText = trim(implode("\n", $out));
$raw = json_decode($rawText, true);
if (is_array($raw) && ($raw['ok'] ?? false)) {
$skyData = $raw;
$nextDate = (new DateTimeImmutable($date, new DateTimeZone($tz)))->modify('+1 day')->format('Y-m-d'); $nextDate = (new DateTimeImmutable($date, new DateTimeZone($tz)))->modify('+1 day')->format('Y-m-d');
$argsNext = [ $pythonResultNext = runIndexPythonCommand($scriptPath, [
$pythonBin, 'sun_moon_rise_set',
escapeshellarg($scriptPath), (string)$lat,
escapeshellarg('sun_moon_rise_set'), (string)$lon,
escapeshellarg((string)$lat), (string)$elev,
escapeshellarg((string)$lon), $nextDate,
escapeshellarg((string)$elev), $tz,
escapeshellarg($nextDate), ]);
escapeshellarg($tz), if ($pythonResultNext['ok'] ?? false) {
]; $skyDataNext = $pythonResultNext['data'];
$cmdNext = implode(' ', $argsNext) . ' 2>&1';
$outNext = [];
$resultCodeNext = 0;
exec($cmdNext, $outNext, $resultCodeNext);
$rawTextNext = trim(implode("\n", $outNext));
$rawNext = json_decode($rawTextNext, true);
if (is_array($rawNext) && ($rawNext['ok'] ?? false)) {
$skyDataNext = $rawNext;
} }
} else { } else {
$pyError = $raw['error'] ?? ($rawText !== '' ? $rawText : 'Fehler beim Berechnen der Aufgangszeiten.'); $pyError = $pythonResult['data']['error'] ?? $pythonResult['error'] ?? 'Fehler beim Berechnen der Aufgangszeiten.';
} }
// Planeten // Planeten
$args2 = [ $pythonResult2 = runIndexPythonCommand($scriptPath, [
$pythonBin, 'planet_rise_set',
escapeshellarg($scriptPath), (string)$lat,
escapeshellarg('planet_rise_set'), (string)$lon,
escapeshellarg((string)$lat), (string)$elev,
escapeshellarg((string)$lon), $date,
escapeshellarg((string)$elev), $tz,
escapeshellarg($date), ]);
escapeshellarg($tz), if ($pythonResult2['ok'] ?? false) {
]; $planetData = $pythonResult2['data'];
$cmd2 = implode(' ', $args2) . ' 2>&1';
$out2 = [];
$resultCode2 = 0;
exec($cmd2, $out2, $resultCode2);
$rawText2 = trim(implode("\n", $out2));
$raw2 = json_decode($rawText2, true);
if (is_array($raw2) && ($raw2['ok'] ?? false)) {
$planetData = $raw2;
} elseif ($pyError === null) { } elseif ($pyError === null) {
$pyError = $raw2['error'] ?? ($rawText2 !== '' ? $rawText2 : 'Fehler beim Berechnen der Planetenzeiten.'); $pyError = $pythonResult2['data']['error'] ?? $pythonResult2['error'] ?? 'Fehler beim Berechnen der Planetenzeiten.';
} }
// Satelliten-Überflüge heute — Favorit zuerst, dann alle gemerkten // Satelliten-Überflüge heute — Favorit zuerst, dann alle gemerkten
@@ -341,28 +368,20 @@ if ($loggedIn) {
], $userSats), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); ], $userSats), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$satsPayload = base64_encode((string) $satsJson); $satsPayload = base64_encode((string) $satsJson);
$args3 = [ $pythonResult3 = runIndexPythonCommand($scriptPath, [
$pythonBin, 'satellite_passes',
escapeshellarg($scriptPath), (string)$lat,
escapeshellarg('satellite_passes'), (string)$lon,
escapeshellarg((string)$lat), (string)$elev,
escapeshellarg((string)$lon), $tz,
escapeshellarg((string)$elev), $dayStart->format('Y-m-d\TH:i:s\Z'),
escapeshellarg($tz), $dayEnd->format('Y-m-d\TH:i:s\Z'),
escapeshellarg($dayStart->format('Y-m-d\TH:i:s\Z')), $satsPayload,
escapeshellarg($dayEnd->format('Y-m-d\TH:i:s\Z')), ]);
escapeshellarg($satsPayload), if ($pythonResult3['ok'] ?? false) {
]; $satData = $pythonResult3['data'];
$cmd3 = implode(' ', $args3) . ' 2>&1';
$out3 = [];
$resultCode3 = 0;
exec($cmd3, $out3, $resultCode3);
$rawText3 = trim(implode("\n", $out3));
$raw3 = json_decode($rawText3, true);
if (is_array($raw3) && ($raw3['ok'] ?? false)) {
$satData = $raw3;
} else { } else {
$satError = $raw3['error'] ?? ($rawText3 !== '' ? $rawText3 : 'Fehler beim Berechnen der Satellitenüberflüge.'); $satError = $pythonResult3['data']['error'] ?? $pythonResult3['error'] ?? 'Fehler beim Berechnen der Satellitenüberflüge.';
} }
} }
} }