Created
December 16, 2019 20:03
-
-
Save dumrauf/8ae7916ceba67630488197fd2806304e to your computer and use it in GitHub Desktop.
Get all draft post URLs from a given Ghost installation and output them to a given file; see https://www.how-hard-can-it.be/getting-all-draft-posts-ghost-blog/ for details
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This Bash script gets all draft post URLs from a given Ghost installation and | |
# outputs them to a given file; see <https://www.how-hard-can-it.be/getting-all-draft-posts-ghost-blog/> | |
# for details | |
# Stop on all errors | |
set -e | |
# Static variable defintions | |
DRAFT_POSTS_JSON_FILE="posts.json" | |
DRAFT_POSTS_PREFIX="/p" | |
############################################################################### | |
# Helper Functions # | |
############################################################################### | |
# Helper function for displaying help text (note the irony!) | |
function display_help { | |
script_name=$(basename "$0") | |
echo "Usage: ${script_name} -u <ghost-base-url> -o <output-file>" | |
echo "Example: ${script_name} -u https://www.how-hard-can-it.be -o drafts.paths" | |
echo "Remark: This script expects to retrieve the username and password from " | |
echo " the corresponding environment variables 'username' and 'password'." | |
} | |
############################################################################### | |
# Input Argument Handling # | |
############################################################################### | |
# Input argument parsing | |
while getopts ":hu:o:" option | |
do | |
case "${option}" in | |
u) ghost_base_url=${OPTARG};; | |
o) output_file=$OPTARG;; | |
h) display_help; exit -1;; | |
\?) echo "Invalid option: $OPTARG" >&2; exit 1;; | |
:) echo "Missing option argument for -$OPTARG" >&2; exit 1;; | |
esac | |
done | |
# Mandatory input arguments check | |
if [[ -z "${ghost_base_url}" || -z "${output_file}" ]]; then | |
display_help | |
exit -1 | |
fi | |
############################################################################### | |
# Mandatory Environment Variable Handling # | |
############################################################################### | |
# Environment variable validations | |
if [ -z ${username} ] | |
then | |
echo "Environment variable 'username' not set. Use 'export username=<username>' to export <username>." | |
exit 1 | |
fi | |
if [ -z ${password} ] | |
then | |
echo "Environment variable 'password' not set. Use 'export password=<password>' to export <password>." | |
exit 1 | |
fi | |
############################################################################### | |
# Draft Posts Retrieval via API # | |
############################################################################### | |
# Create a session and store the cookie in a temporary variable (avoiding writing it to disk!); | |
# see <https://www.how-hard-can-it.be/curl-cookie-memory-only-not-disk/> for details | |
cookie=$(curl -c - -d username="${username}" -d password="${password}" \ | |
-H "Origin: ${ghost_base_url}" \ | |
"${ghost_base_url}/ghost/api/v2/admin/session/") | |
# Use the session cookie to query all draft posts and save them in ${DRAFT_POSTS_JSON_FILE} | |
echo "${cookie}" | curl -b - \ | |
-H "Content-Type: application/json" \ | |
-H "Origin: ${ghost_base_url}" \ | |
"${ghost_base_url}/ghost/api/v2/admin/posts/?limit=all&filter=status:draft&fields=uuid" > "${DRAFT_POSTS_JSON_FILE}" | |
############################################################################### | |
# Draft Posts Extraction via jq # | |
############################################################################### | |
# Remove any previous leftover ${output_file} | |
rm -f "${output_file}" | |
# Extract all draft posts and store them in ${output_file} | |
for uuid in $(jq -r '.posts[].uuid' "${DRAFT_POSTS_JSON_FILE}") | |
do | |
echo "${DRAFT_POSTS_PREFIX}/${uuid}/" >> "${output_file}" | |
done | |
# Delete the temporary file ${DRAFT_POSTS_JSON_FILE} at this point (and not inside a trap) | |
# to allow for debugging in case things (can and will) go wrong | |
rm -f "${DRAFT_POSTS_JSON_FILE}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment