Merge branch 'pledges'

This commit is contained in:
Kim Taylor
2024-09-14 15:43:24 +10:00
3 changed files with 128 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.swp

100
pledge-update/main.php Normal file
View File

@@ -0,0 +1,100 @@
<?php
$options = getopt("", ["pledges-files:"]);
if (isset($options['pledges-files'])) {
$pledges_files = $options['pledges-files'];
} else {
error_log("Error: Missing required option '--pledges-files'.");
exit(1);
}
$files = explode(" ", $pledges_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);
}
$candidate_file = dirname($file)."/candidates.csv";
if (($handle = fopen($candidate_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);
}
if (($handle = fopen($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);

27
update-pledges.sh Executable file
View File

@@ -0,0 +1,27 @@
#!/bin/bash
# If this runs as a cron job - might want to limit the number of revisions
# wordpress stores:
# wp-config.php:
# define( 'WP_POST_REVISIONS', 3 );
#wp post list --post_type=page
#wp post get 426 --field=content > current-homepage
#wp post get 1409 --field=content > movie-homepage
#wp post create --post_type=page --post_title="test_pledge" movie-homepage
#wp post update 1803 ../spl-data/movie-homepage
DATA_PATH="../spl-data"
pledges_files=()
for folder in "$DATA_PATH"/*; do
if test -f "$folder"/pledges.csv; then
pledges_files+=("$folder"/pledges.csv)
fi
done
pledge_sed=$(php pledge-update/main.php --pledges-files "${pledges_files[*]}")
content=$(sed "$pledge_sed" ../spl-data/movie-homepage)
wp post update 1803 --post_content="$content"