From d9cb183b2acbaeafbf4d916891ba1408f7c9e015 Mon Sep 17 00:00:00 2001 From: Matt Way Date: Thu, 15 Aug 2024 22:10:03 +1000 Subject: [PATCH] Implement CSV normaliser logic --- csv-normaliser/main.php | 169 +++++++++++++++++++++++++++++----------- 1 file changed, 122 insertions(+), 47 deletions(-) diff --git a/csv-normaliser/main.php b/csv-normaliser/main.php index 39cec97..ae1d4f2 100644 --- a/csv-normaliser/main.php +++ b/csv-normaliser/main.php @@ -1,69 +1,144 @@ $value) { - $candidate[$value] = $data[$key]; + $currentLine++; + //echo var_dump($data); + if ($data[0] == "Ward") { + // CSV contains ward names in uppercase, convert them to a more readable form + $currentWard = ucwords(strtolower($data[1])); + + // Handle some special cases where the above logic doesn't match the expected names + // Note that we cannot just convert every letter after a '-' character to uppercase + // because there are some ward names like "Bulleke-bek" + if ($currentWard == "Warrk-warrk") { + $currentWard = "Warrk-Warrk"; + } + if ($currentWard == "Djirri-djirri") { + $currentWard = "Djirri-Djirri"; + } + if ($currentWard == "Coastal-promontory") { + $currentWard = "Coastal-Promontory"; + } + } + if ($data[0] == "Candidate") { + if ($currentWard == null) { + error_log("No ward found, skipping data on line " . $currentLine); + continue; + } + + $candidateName = $data[1]; + + if ($candidateName == " example name") { + error_log("Skipping line ". $currentLine); + continue; + } + + print("Adding candidate to " . $candidateName . " to ". $currentWard . "\n"); + + $name_split = explode(" ", $data[1]); + + $name_patterns = [ + implode(".*", $name_split), + implode(".*", array_reverse($name_split)), + ]; + + $regex_groups = array_map(function($x) { return "(?:.*" . $x . ".*)"; }, $name_patterns); + + $regex_pattern = "/" . implode("|", $regex_groups) . "/i"; + + $picture = ""; + foreach ($mediaFiles as $mediaFile) { + if (preg_match($regex_pattern, $mediaFile)) { + $picture = $mediaFile; + break; + } + } + if ($picture === "") { + print("Failed to identify picture for " . $candidateName); + } + + array_push( + $candidates, + [ + "Ward" => $currentWard, + "Candidate Name" => $candidateName, + "Rating" => $data[2], + "Picture" => $picture + ] + ); } - $candidateData[] = $candidate; } fclose($handle); } else { - error_log('Error opening candidates file'); + error_log('Error opening input file'); exit(1); } -$candidateData = array_filter($candidateData, function ($candidate) use ($councilData) { - return isset($candidate["Council"]) && $candidate["Council"] === $councilData['shortName']; -}); - -if (empty($candidateData)) { - error_log("Failed to load any candidates for " . $councilData['shortName']); -} - -if (isset($options['media-file'])) { - $mediaFileContents = file_get_contents($options['media-file']); -} else { - error_log("Error: Missing required option '--media-file'."); - exit(1); -} - -$mediaData = json_decode($mediaFileContents, true); - -$renderer = new SPLPageRenderer(); -$pageContent = $renderer->renderCouncilPage($councilData, $candidateData, $mediaData); -if ($pageContent === null) { +if (empty($candidates)) { + error_log("Failed to find any candidates"); exit(2); } -echo $pageContent; +if (($handle = fopen($outputFile, "w")) !== FALSE) { + $headers = array( + "Ward", + "Candidate Name", + "Rating", + "Picture" + ); + if (fputcsv($handle, $headers) === FALSE) { + error_log('Error writing headers to output file'); + exit(3); + } + foreach ($candidates as $candidate) { + $fields = array( + $candidate["Ward"], + $candidate["Candidate Name"], + $candidate["Rating"], + $candidate["Picture"] + ); + if (fputcsv($handle, $fields) === FALSE) { + error_log('Error writing candidate to output file'); + exit(3); + } + } +} else { + error_log('Error opening output file'); + exit(1); +} + +print("Data written to " . $outputFile); + exit(0);