Skip to content

Instantly share code, notes, and snippets.

@gburgett
Created November 14, 2018 22:34
Show Gist options
  • Save gburgett/279bea7e02dc8ed42e7cf6c0a07d4b81 to your computer and use it in GitHub Desktop.
Save gburgett/279bea7e02dc8ed42e7cf6c0a07d4b81 to your computer and use it in GitHub Desktop.
Compassion kids extractor
#! /bin/bash
COLOR_NC='\033[0m' # No Color
COLOR_LGREEN='\033[1;32m'
COLOR_GRAY='\033[1;30m'
COLOR_LGRAY='\033[0;37m'
COLOR_RED='\033[0;31m'
logv() {
[[ -z "$VERBOSE" ]] && return 0;
>&2 echo -e "${COLOR_GRAY}$@${COLOR_NC}" || true
}
logerr() {
>&2 echo -e "${COLOR_RED}$@${COLOR_NC}"
}
log() {
>&2 echo -e "${COLOR_LGREEN}$@${COLOR_NC}"
}
panic() {
logerr "$@"
exit -1;
}
curlv() {
execv curl $@
}
execv() {
logv "$@"
$@
}
# Write your usage
usage() {
echo "$0 [url]
Parses the Compassion watermark landing page and generates a JSON array of
kids with resulting page links
url: the URL of the compassion WM landing page
defaults to https://www.compassion.com/watermark
" && \
grep " .)\ #" $0; exit 0;
}
# Parse additional args here
while getopts ":hv" arg; do
case $arg in
v) # Verbose mode - extra output
VERBOSE=true
FLAGS="$FLAGS -v"
;;
h | *) # Display help.
usage
exit 0
;;
esac
done
shift $(($OPTIND - 1))
set -e
command -v jq >/dev/null 2>&1 || panic "I require 'jq' but it's not installed."
URL="$1"
[[ -z "$URL" ]] && URL="https://www.compassion.com/watermark"
mkdir -p tmp/
curlv -L -c tmp/cookies.txt "$URL" > tmp/landing-page.html
curl -L -b tmp/cookies.txt \
'https://www.compassion.com/api/beneficiaries?CountryFilter=&AreaFilter=&ProjectKeyFilter=&ProjectNameFilter=&GenderFilter=&AgeMinFilter=&AgeMaxFilter=&BirthMonthFilter=&BirthDayFilter=&BirthYearFilter=&HandicappedFilter=&HivFilter=&HvcFilter=&HvcMismatch=false&OrphanFilter=&ChildNameFilter=&LongestWaitingFilter=&NumberRequested=999&CurrentPage=1&SortOrder=0&CustomizedFilters=false&Paginate=true&BirthdayTodayFilter=false' \
> tmp/compassion-kids.json
log "Loaded $(cat tmp/compassion-kids.json | jq '.end') kids"
cat tmp/compassion-kids.json \
| jq '.results[] | { globalId: .globalId, consignmentSourceCode: .consignmentSourceCode }' \
| jq -s --raw-output '"<script>window.compassion_kids=\(.)</script>"' \
> data-section.html
log "Please open the file 'data-section.html' and copy the contents"
if command -v pbcopy >/dev/null 2>&1; then
cat data-section.html | pbcopy
log "The contents have already been copied to your clipboard"
fi
log "Please paste the data into the first Embed Code section at https://www.watermark.org/admin/entries/pages/17497-compassion-kids"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment