Skip to content

Instantly share code, notes, and snippets.

@micha
Last active December 12, 2018 16:43
Show Gist options
  • Save micha/74f6e573315eae123584e5819901f1cc to your computer and use it in GitHub Desktop.
Save micha/74f6e573315eae123584e5819901f1cc to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
abort() { exec 1>&2 ; [[ $1 ]] && echo "$(basename $0): $1" ; exit 1 ; }
usage() {
local prog=$(basename $0)
exec 1>&2
[[ $1 ]] && echo "$prog: $1" && echo
cat <<EOT
USAGE:
$prog [-h]
$prog [-k <apikey>] [-d <domain>] [<json>]
OVERVIEW:
Takes a decision API request JSON on stdin or as a positional argument and
displays explainer output in TSV format, with column headers. The adserver
explainer feature requires authentication which must be provided either by
setting the ADZERK_EXPLAIN_KEY environment variable or by passing your API
key via the -k option.
OPTIONS:
-d <domain> Set the ad request domain (default engine.adzerk.net).
-k <apikey> Your Adzerk API key (see also ENVIRONMENT below).
-h Print this and exit.
ENVIRONMENT:
You may set the following environment variables:
* ADZERK_EXPLAIN_KEY -- Your Adzerk API key. The -k option overrides this.
EXIT STATUS:
Exits with status of 0 on success, or non-zero if the ad request raised an
error.
EOT
exit 0;
}
domain=engine.adzerk.net
apikey=$ADZERK_EXPLAIN_KEY
while getopts "hd:k:" o; do
case "${o}" in
d) domain=$OPTARG ;;
k) apikey=$OPTARG ;;
h) usage ;;
?) abort ;;
esac
done
shift $((OPTIND-1))
[ -n "$apikey" ] || abort "API key required"
[ -n "$domain" ] || abort "domain required"
set -o pipefail
curl \
-sfd "${1:-$(cat)}" \
-H x-adzerk-explain:$apikey \
"https://${domain}/api/v2" \
|jt -j explain \
. ^=divname results \
[ phase %=phase ] \
[ channel %=channel ] \
[ priority %=priority ] \
[ advertiser %=advertiser ] \
[ campaign %=campaign ] \
[ flight %=flight ] \
[ ad %=ad ] \
[ ecpm %=ecpm ] \
[ weight %=weight ] \
[ info %=info ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment