Skip to content

Instantly share code, notes, and snippets.

@blangenfeld
Last active December 5, 2018 18:12
Show Gist options
  • Save blangenfeld/26088df5e27db8bea6400e36efb595db to your computer and use it in GitHub Desktop.
Save blangenfeld/26088df5e27db8bea6400e36efb595db to your computer and use it in GitHub Desktop.
Bash script for fetching ALL of a Drip account's "whatevers". Echoes results to stdout as a JSON array as they come in. Not fancy, but it works.
#! /bin/bash
# Bash script for fetching ALL of a Drip account's whatevers to file.
# Not fancy, but it works.
#
# Usage: ./drip-fetch-all.sh <resource>
#
# brian@knotfield.com
RESOURCE=$1 # subscribers, campaigns, tags, ...
PAGE=1 # Page number we're starting with
TOTAL_PAGES="$PAGE" # Initializing to $PAGE ensures at least one loop pass
PER_PAGE=1000 # Max is 1000
DATA_KEY="$RESOURCE" # JSON key containing the data we're after
if [ -z "$DRIP_ACCOUNT_ID" ]; then
>&2 echo "Environment variable DRIP_ACCOUNT_ID is required!"
>&2 echo "$ export DRIP_ACCOUNT_ID=your-drip-account-id"
>&2 echo "and try again."
exit 1
fi
if [ -z "$DRIP_API_KEY" ]; then
>&2 echo "Environment variable DRIP_API_KEY is required!"
>&2 echo "$ export DRIP_API_KEY=your-drip-account-id"
>&2 echo "and try again."
exit 1
fi
echo "["
until [ $PAGE -gt $TOTAL_PAGES ]; do
# Log progress to stderr.
if [ $PAGE -eq 1 ]; then
>&2 echo "Fetching page $PAGE..."
else
>&2 echo "Fetching page $PAGE of $TOTAL_PAGES..."
fi
# Fetch the next page of results.
RESPONSE=$(curl -s -w "HTTPSTATUS:%{http_code}" \
-H "Authorization: Basic $DRIP_API_KEY" \
"https://api.getdrip.com/v2/$DRIP_ACCOUNT_ID/$RESOURCE?page=$PAGE&per_page=$PER_PAGE")
# Extract status code and body from the response.
STATUS=$(echo $RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
BODY=$(echo $RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')
# Upon failure, log to stderr and exit.
if [ $STATUS -ne 200 ]; then
>&2 echo "Failed to fetch page $PAGE (HTTP status code $STATUS): $BODY"
exit $STATUS
fi
# Echo data to stdout. For pages 2+, echo a comma first.
if [ $PAGE -gt 1 ]; then
echo ","
fi
echo $BODY | grep -oP "$DATA_KEY\":\[\K.+(?=\]\})"
# Extract total page count from the response body.
TOTAL_PAGES=$(echo $BODY | grep -oP 'total_pages":\K\w+(?=,)')
if [ -z "$TOTAL_PAGES" ]; then
TOTAL_PAGES=1 # Not a paginated resource, apparently; short-circuit the loop
fi
PAGE=$(($PAGE + 1))
done
echo "]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment