From a54fff1d4c0daba2563c05f6633a75686d56bf88 Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com> Date: Wed, 1 May 2024 23:58:04 +0200 Subject: [PATCH] Fixed parser --- application/controllers/Update.php | 56 +++++++++++++++++--------- application/models/Satellite_model.php | 1 + 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/application/controllers/Update.php b/application/controllers/Update.php index 79df6c015..5dd69d4ca 100644 --- a/application/controllers/Update.php +++ b/application/controllers/Update.php @@ -584,28 +584,44 @@ class Update extends CI_Controller { $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; - $file = 'https://www.amsat.org/tle/dailytle.txt'; + $url = 'https://www.amsat.org/tle/dailytle.txt'; + $curl = curl_init($url); - $handle = fopen($file, "r"); - if ($handle === FALSE) { - echo "Something went wrong with fetching the Amsat TLE file"; - return; - } - $this->db->empty_table("tle"); - $i = 0; - $satname = ''; - $tleline1 = ''; - $tleline2 = ''; - while (($line = fgets($handle)) !== false) { - $i++; - $satname = $line; - $tleline1 = fgets($handle); - $tleline2 = fgets($handle); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); - $sql = "INSERT INTO tle (satelliteid, tle) select id, '" . $tleline1 . "\n" . $tleline2 . "' from satellite where name = '" . $satname . "'"; - $this->db->query($sql); + $response = curl_exec($curl); + + $count = 0; + + if ($response === false) { + echo 'Error: ' . curl_error($curl); + } else { + $this->db->empty_table("tle"); + // Split the response into an array of lines + $lines = explode("\n", $response); + + $satname = ''; + $tleline1 = ''; + $tleline2 = ''; + + // Process each line + for ($i = 0; $i < count($lines); $i += 3) { + $count++; + // Check if there are at least three lines remaining + if (isset($lines[$i], $lines[$i + 1], $lines[$i + 2])) { + // Get the three lines + $satname = $lines[$i]; + $tleline1 = $lines[$i + 1]; + $tleline2 = $lines[$i + 2]; + + $sql = "INSERT INTO tle (satelliteid, tle) select id, '" . $tleline1 . "\n" . $tleline2 . "' from satellite where name = '" . $satname . "'"; + $this->db->query($sql); + } + } } - fclose($handle); + + curl_close($curl); $mtime = microtime(); $mtime = explode(" ",$mtime); @@ -613,7 +629,7 @@ class Update extends CI_Controller { $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "This page was created in ".$totaltime." seconds
"; - echo "Records inserted: " . $i . "
"; + echo "Records inserted: " . $count . "
"; $datetime = new DateTime("now", new DateTimeZone('UTC')); $datetime = $datetime->format('Ymd h:i'); $this->optionslib->update('tle_update', $datetime , 'no'); diff --git a/application/models/Satellite_model.php b/application/models/Satellite_model.php index 46ece46b7..a566e5dc1 100644 --- a/application/models/Satellite_model.php +++ b/application/models/Satellite_model.php @@ -15,6 +15,7 @@ class Satellite_model extends CI_Model { $sql = "select satellite.id, satellite.name as satname, tle.tle from satellite join tle on satellite.id = tle.satelliteid + order by satellite.name "; return $this->db->query($sql)->result();