From 3018c1e30a432ee168eed74d7838ae72e8188f54 Mon Sep 17 00:00:00 2001 From: Matt Way Date: Sun, 28 Jul 2024 19:05:20 +1000 Subject: [PATCH] Add make-council-pages.sh script --- Readme.md | 4 ++++ make-council-pages.sh | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 make-council-pages.sh diff --git a/Readme.md b/Readme.md index 3a8c3fd..7290d14 100644 --- a/Readme.md +++ b/Readme.md @@ -13,3 +13,7 @@ The file can be generated using the jq tool and the VEC data: ''' jq '[.[] | {name: .electorateName, electorateId: .electorateId, shortName: .parentElectorateName | match("(.*?)(?:(?: Rural)?(?: City| Shire) Council)").captures[0].string, parentElectorateId: .parentElectorateId, councilName: .parentElectorateName }] | group_by(.parentElectorateId) | map({shortName: .[0].shortName, councilName: .[0].councilName, wardNames: . | map(.name) }) | sort_by(.shortName)' "VEC Data\wards.json" > council_names.json ''' + +## make-council-pages.sh + +This is a bash script for creating a page in WordPress for each council. If a page for a council already exists, the page will be updated instead. The source of councils for this script is the "council_names.json" file. The script needs the `jq` and `wp` tools. \ No newline at end of file diff --git a/make-council-pages.sh b/make-council-pages.sh new file mode 100644 index 0000000..d24b9a6 --- /dev/null +++ b/make-council-pages.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +# This script uses the wp and jq commands, make sure they are installed before running this script. + +# The council_names.json file must contain a list of objects (one for each council). +# Each object must have a "shortName" field and a "wardNames" field. +# The "shortName" field must be a string. +# The "wardNames" field must be a list of strings. +JSON_FILE="council_names.json" + +# Controls the flags that are passed to every usage of the wp command. +WP_FLAGS="--allow-root --path=/var/www/html" + +function create_or_update_page() { + local council_block="$1" + local page_id="$2" + + short_name=$(echo "$council_block" | jq -r '.shortName') + + # TODO: Generate better content! This just outputs the name of each ward. + content=$(echo "$council_block" | jq -r '.wardNames | .[]') + + if [[ -n "$page_id" ]]; then + echo "Update page $short_name (post $page_id)" + wp post update "$page_id" --post_content="$content" $WP_FLAGS + else + echo "Create page $short_name" + wp post create --post_type=page --post_title="$short_name" --post_status=publish --post_content="$content" $WP_FLAGS + fi +} + +# Read JSON data +data=$(cat "$JSON_FILE") + +# Get all page IDs in one go because the wp command is pretty slow +wp_posts=$(wp post list --post_type=page --format=json $WP_FLAGS) + +# Iterate over JSON objects +jq -c '.[] | .' <<< "$data" | while IFS=' ' read -r council_block; do + short_name=$(echo "$council_block" | jq -r '.shortName') + page_id=$(echo $wp_posts | jq '.[] | select(.post_title == "'"$short_name"'") | .ID' | head -n 1) + create_or_update_page "$council_block" "$page_id" +done \ No newline at end of file