Skip to content

Instantly share code, notes, and snippets.

@jkitching
Created March 12, 2024 09:32
Show Gist options
  • Save jkitching/236f62745c00a8d0578759e12b3f4502 to your computer and use it in GitHub Desktop.
Save jkitching/236f62745c00a8d0578759e12b3f4502 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Initialize variables
TARGET_ARRAY=""
COMMAND=""
OUTPUT_NAME=""
TOKEN_FILE=""
PAGE_TOKEN_ARG=""
# Parse long-form command-line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--target-array)
TARGET_ARRAY="$2"
shift 2
;;
--command)
COMMAND="$2"
shift 2
;;
--output-name)
OUTPUT_NAME="$2"
shift 2
;;
--page-token-arg)
PAGE_TOKEN_ARG="$2"
shift 2
;;
*)
echo "Unknown parameter: $1"
exit 1
;;
esac
done
# Check if required arguments are provided
if [ -z "$TARGET_ARRAY" ] || [ -z "$COMMAND" ] || [ -z "$OUTPUT_NAME" ] || [ -z "$PAGE_TOKEN_ARG" ]; then
echo "Usage: $0 --target-array <target_array_name> --command <command_without_pagination> --output-name <output_file_name> --page-token-arg <page_token_arg>"
exit 1
fi
# Append extensions to output file and token file names
OUTPUT_FILE="$OUTPUT_NAME.json"
TOKEN_FILE="$OUTPUT_NAME.token"
# Read the last saved page token from the file
if [ -f "$TOKEN_FILE" ]; then
PAGE_TOKEN=$(cat "$TOKEN_FILE")
else
PAGE_TOKEN=""
fi
# Function to handle Ctrl+C and save the current page token
handle_interrupt() {
echo "Ctrl+C detected; saving current page token to $TOKEN_FILE"
echo "$PAGE_TOKEN" > "$TOKEN_FILE"
exit 1
}
# Set up trap to catch Ctrl+C and call the handle_interrupt function
trap 'handle_interrupt' SIGINT
# Main loop
while true; do
# Build the command with or without the -p argument based on page token presence
if [ -n "$PAGE_TOKEN" ]; then
COMMAND_WITH_TOKEN="photoslibrary1 $COMMAND -$PAGE_TOKEN_ARG page-token=$PAGE_TOKEN"
else
COMMAND_WITH_TOKEN="photoslibrary1 $COMMAND"
fi
echo "[$(date)] $COMMAND_WITH_TOKEN"
# Make API call
response=$($COMMAND_WITH_TOKEN)
# Use jq to count the number of target array items
items_count=$(echo "$response" | jq -r ".$TARGET_ARRAY | length")
# Extract next page token from the JSON response
PAGE_TOKEN=$(echo "$response" | jq -r '.nextPageToken')
# Check if the file exists, if not, create it
touch "$OUTPUT_FILE"
# Use jq to format and append the target array items to the file
echo "$response" | jq -c ".$TARGET_ARRAY[]" >> "$OUTPUT_FILE"
# Save the current page token to the file for resuming
echo "$PAGE_TOKEN" > "$TOKEN_FILE"
# Print the status
echo "[$(date)] Processed $items_count $TARGET_ARRAY"
# Check if there is a next page token
if [ "$PAGE_TOKEN" == "null" ]; then
# No next page token, break out of the loop
break
fi
done
# Token file is no longer needed, so delete it
rm "$TOKEN_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment