Move pledge data parsing to separate file.
This commit is contained in:
19
make-pledge-page.sh
Executable file
19
make-pledge-page.sh
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
|
||||
# This script uses the jq, wp, and php commands, make sure they are installed before running this script.
|
||||
|
||||
# The folder containing data for each council.
|
||||
# Includes the list of candidates and any media.
|
||||
DATA_PATH="../spl-data"
|
||||
|
||||
# Iterate over folders in data path
|
||||
candidates_files=()
|
||||
for folder in "$DATA_PATH"/*; do
|
||||
if test -f "$folder"/candidates.csv; then
|
||||
candidates_files+=("$folder"/candidates.csv)
|
||||
fi
|
||||
done
|
||||
|
||||
content=$(php pledge-update/pledge-page.php --candidates-files "${candidates_files[*]}")
|
||||
|
||||
echo $content;
|
||||
42
pledge-update/homepage.php
Normal file
42
pledge-update/homepage.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
require_once("parse_pledge_data.php");
|
||||
|
||||
$options = getopt("", ["candidates-files:"]);
|
||||
|
||||
if (isset($options['candidates-files'])) {
|
||||
$candidates_files = $options['candidates-files'];
|
||||
} else {
|
||||
error_log("Error: Missing required option '--candidates-files'.");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$candidate_data = parse_pledge_data(explode(" ", $candidates_files));
|
||||
|
||||
/* Select people who have taken the pledge */
|
||||
$pledgeCandidates = array_filter($candidate_data, function ($candidate) {
|
||||
return $candidate['Pledge'] === 'y';
|
||||
});
|
||||
|
||||
/* Select 9 random candidates */
|
||||
$pledgeKeys = array_rand($pledgeCandidates, 9);
|
||||
shuffle($pledgeKeys);
|
||||
|
||||
$i = 0;
|
||||
foreach ($pledgeKeys as $key) {
|
||||
$image_url = $pledgeCandidates[$key]['image_url'];
|
||||
$image_id = $pledgeCandidates[$key]['image_id'];
|
||||
|
||||
echo "s|pledge_img_".$i."|".$image_url."|\n";
|
||||
echo "s|pledge_id_".$i."|".$image_id."|\n";
|
||||
|
||||
echo "s|pledge_string_".$i."|";
|
||||
echo $pledgeCandidates[$key]['Candidate Name'].
|
||||
" (".
|
||||
$pledgeCandidates[$key]['Council'].
|
||||
") has taken the pledge!|\n";
|
||||
|
||||
$i++;
|
||||
}
|
||||
|
||||
exit(0);
|
||||
@@ -1,102 +0,0 @@
|
||||
<?php
|
||||
|
||||
$options = getopt("", ["candidates-files:"]);
|
||||
|
||||
if (isset($options['candidates-files'])) {
|
||||
$candidates_files = $options['candidates-files'];
|
||||
} else {
|
||||
error_log("Error: Missing required option '--candidates-files'.");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$files = explode(" ", $candidates_files);
|
||||
|
||||
$candidate_data = [];
|
||||
foreach ($files as $key => $file) {
|
||||
$config_file = dirname($file)."/config.json";
|
||||
$config_string = file_get_contents($config_file);
|
||||
|
||||
if ($config_string !== FALSE) {
|
||||
$config = json_decode($config_string, true);
|
||||
} else {
|
||||
error_log("Error opening config.json.");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (($handle = fopen($file, "r")) !== FALSE) {
|
||||
$headers = fgetcsv($handle);
|
||||
while (($data = fgetcsv($handle)) !== FALSE) {
|
||||
$candidate = [];
|
||||
$candidate['Pledge'] = 'n';
|
||||
foreach ($headers as $key => $value) {
|
||||
$candidate[$value] = $data[$key];
|
||||
}
|
||||
$candidate['Council'] = $config['councilName'];
|
||||
$candidate['Path'] = dirname($file);
|
||||
$candidate_data[$candidate['Candidate Name']] = $candidate;
|
||||
}
|
||||
fclose($handle);
|
||||
} else {
|
||||
error_log('Error opening candidates file');
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$pledges_file = dirname($file)."/pledges.csv";
|
||||
|
||||
if (!file_exists($pledges_file)) continue;
|
||||
|
||||
if (($handle = fopen($pledges_file, "r")) !== FALSE) {
|
||||
$headers = fgetcsv($handle);
|
||||
while (($data = fgetcsv($handle)) !== FALSE) {
|
||||
$candidate = [];
|
||||
foreach ($headers as $key => $value) {
|
||||
$candidate[$value] = $data[$key];
|
||||
}
|
||||
$candidate_data[$candidate['Candidate Name']]['Pledge'] =
|
||||
$candidate['Pledge'];
|
||||
}
|
||||
fclose($handle);
|
||||
} else {
|
||||
error_log('Error opening pledges file');
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Select people who have taken the pledge */
|
||||
$pledgeCandidates = array_filter($candidate_data, function ($candidate) {
|
||||
return $candidate['Pledge'] === 'y';
|
||||
});
|
||||
|
||||
/* Select 9 random candidates */
|
||||
$pledgeKeys = array_rand($pledgeCandidates, 9);
|
||||
shuffle($pledgeKeys);
|
||||
|
||||
$i = 0;
|
||||
foreach ($pledgeKeys as $key) {
|
||||
$media_desc = $pledgeCandidates[$key]['Path']."/".
|
||||
$pledgeCandidates[$key]['Picture'].".json";
|
||||
$media_string = file_get_contents($media_desc);
|
||||
|
||||
if ($media_string !== FALSE) {
|
||||
$media = json_decode($media_string, true);
|
||||
} else {
|
||||
error_log("Error opening image descriptor.");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$image_url = $media['url'];
|
||||
$image_id = $media['id'];
|
||||
|
||||
echo "s|pledge_img_".$i."|".$image_url."|\n";
|
||||
echo "s|pledge_id_".$i."|".$image_id."|\n";
|
||||
|
||||
echo "s|pledge_string_".$i."|";
|
||||
echo $pledgeCandidates[$key]['Candidate Name'].
|
||||
" (".
|
||||
$pledgeCandidates[$key]['Council'].
|
||||
") has taken the pledge!|\n";
|
||||
|
||||
$i++;
|
||||
}
|
||||
|
||||
exit(0);
|
||||
73
pledge-update/parse_pledge_data.php
Normal file
73
pledge-update/parse_pledge_data.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
function parse_pledge_data($candidates_files) {
|
||||
$candidate_data = [];
|
||||
foreach ($candidates_files as $key => $file) {
|
||||
$config_file = dirname($file)."/config.json";
|
||||
$config_string = file_get_contents($config_file);
|
||||
|
||||
if ($config_string !== FALSE) {
|
||||
$config = json_decode($config_string, true);
|
||||
} else {
|
||||
error_log("Error opening config.json.");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (($handle = fopen($file, "r")) !== FALSE) {
|
||||
$headers = fgetcsv($handle);
|
||||
while (($data = fgetcsv($handle)) !== FALSE) {
|
||||
$candidate = [];
|
||||
$candidate['Pledge'] = 'n';
|
||||
$candidate['Picture'] = "";
|
||||
$candidate['image_url'] = "";
|
||||
$candidate['image_id'] = "";
|
||||
foreach ($headers as $key => $value) {
|
||||
$candidate[$value] = $data[$key];
|
||||
}
|
||||
$candidate['Council'] = $config['councilName'];
|
||||
$candidate['Path'] = dirname($file);
|
||||
$media_desc = $candidate['Path']."/".
|
||||
$candidate['Picture'].".json";
|
||||
if (file_exists($media_desc)) {
|
||||
$media_string = file_get_contents($media_desc);
|
||||
if ($media_string !== FALSE) {
|
||||
$media = json_decode($media_string, true);
|
||||
} else {
|
||||
error_log("Error opening image descriptor.");
|
||||
exit(1);
|
||||
}
|
||||
/* Get photo URL and ID */
|
||||
$candidate['image_url'] = $media['url'];
|
||||
$candidate['image_id'] = $media['id'];
|
||||
}
|
||||
$candidate_data[$candidate['Candidate Name']] = $candidate;
|
||||
}
|
||||
fclose($handle);
|
||||
} else {
|
||||
error_log('Error opening candidates file');
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Override pledge columns if pledges.csv is present */
|
||||
$pledges_file = dirname($file)."/pledges.csv";
|
||||
if (!file_exists($pledges_file)) continue;
|
||||
|
||||
if (($handle = fopen($pledges_file, "r")) !== FALSE) {
|
||||
$headers = fgetcsv($handle);
|
||||
while (($data = fgetcsv($handle)) !== FALSE) {
|
||||
$candidate = [];
|
||||
foreach ($headers as $key => $value) {
|
||||
$candidate[$value] = $data[$key];
|
||||
}
|
||||
$candidate_data[$candidate['Candidate Name']]['Pledge'] =
|
||||
$candidate['Pledge'];
|
||||
}
|
||||
fclose($handle);
|
||||
} else {
|
||||
error_log('Error opening pledges file');
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
return $candidate_data;
|
||||
}
|
||||
44
pledge-update/pledge-page.php
Normal file
44
pledge-update/pledge-page.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
require_once("parse_pledge_data.php");
|
||||
|
||||
$options = getopt("", ["candidates-files:"]);
|
||||
|
||||
if (isset($options['candidates-files'])) {
|
||||
$candidates_files = $options['candidates-files'];
|
||||
} else {
|
||||
error_log("Error: Missing required option '--candidates-files'.");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$candidate_data = parse_pledge_data(explode(" ", $candidates_files));
|
||||
|
||||
/* Select people who have taken the pledge */
|
||||
$pledgeCandidates = array_filter($candidate_data, function ($candidate) {
|
||||
return $candidate['Pledge'] === 'y';
|
||||
});
|
||||
|
||||
print_r($pledgeCandidates);
|
||||
|
||||
/* Select 9 random candidates */
|
||||
//$pledgeKeys = array_rand($pledgeCandidates, 9);
|
||||
//shuffle($pledgeKeys);
|
||||
//
|
||||
//$i = 0;
|
||||
//foreach ($pledgeKeys as $key) {
|
||||
// $image_url = $pledgeCandidates[$key]['image_url'];
|
||||
// $image_id = $pledgeCandidates[$key]['image_id'];
|
||||
//
|
||||
// echo "s|pledge_img_".$i."|".$image_url."|\n";
|
||||
// echo "s|pledge_id_".$i."|".$image_id."|\n";
|
||||
//
|
||||
// echo "s|pledge_string_".$i."|";
|
||||
// echo $pledgeCandidates[$key]['Candidate Name'].
|
||||
// " (".
|
||||
// $pledgeCandidates[$key]['Council'].
|
||||
// ") has taken the pledge!|\n";
|
||||
//
|
||||
// $i++;
|
||||
//}
|
||||
|
||||
exit(0);
|
||||
@@ -20,7 +20,7 @@ for folder in "$DATA_PATH"/*; do
|
||||
fi
|
||||
done
|
||||
|
||||
pledge_sed=$(php pledge-update/main.php --candidates-files "${candidates_files[*]}")
|
||||
pledge_sed=$(php pledge-update/homepage.php --candidates-files "${candidates_files[*]}")
|
||||
|
||||
content=$(sed "$pledge_sed" ../spl-data/movie-homepage)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user