Last active
October 10, 2016 15:42
-
-
Save datadavev/9506b3ba1988f7cea0434ec7a8ac49f5 to your computer and use it in GitHub Desktop.
BASH script to automate image evaluation using Keegan.
This file contains hidden or 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/sh | |
## | |
# Keegan is a very nice AI image evaluation tool. | |
# https://keegan.regaind.io/ | |
# | |
# Keeganize automates processing of an image and capturing the results. | |
# https://gist.github.com/vdave/9506b3ba1988f7cea0434ec7a8ac49f5 | |
# | |
# Output is stored as a finder comment in the metadata for the image file. | |
# Note that there's no error checking so things can go fubar | |
# Dependencies: | |
# curl | |
# pup: https://github.com/ericchiang/pup | |
# optional: comment https://gist.github.com/vdave/b6289c7edcd831b3ba75c6d11347e4ff | |
# | |
VERSION="0.3.1" | |
IMAGE='' | |
SET_COMMENT=0 | |
PUP=$(which pup) | |
COMMENTER=$(which comment) | |
APP_NAME="$(basename $0)" | |
TMP_FILE="/tmp/${APP_NAME}.$$.json" | |
COOKIES="/tmp/${APP_NAME}.$$.cookies" | |
CSRF_TOKEN="" | |
MIDDLE_TOKEN="" | |
usage() { | |
echo "${APP_NAME} [-c] IMAGE" | |
echo " -c Set JSON comment in file (requires 'comment' tool)" | |
} | |
showVersion() { | |
echo "${APP_NAME} v${VERSION}" | |
} | |
getCookies() { | |
MIDDLE_TOKEN=$(curl -s -c "${COOKIES}" "https://regaind.io/#results_demo_container" \ | |
| ${PUP} 'input[name="csrfmiddlewaretoken"]:first-of-type attr{value}') | |
} | |
# -H 'Cookie: csrftoken=${CSRF_TOKEN}' \ | |
doKeeganize() { | |
curl 'https://regaind.io/b2b/demo/' \ | |
-b ${COOKIES} \ | |
-H 'Origin: https://regaind.io' \ | |
-H 'Accept-Encoding: gzip, deflate, br' \ | |
-H 'Accept-Language: en-US,en;q=0.8' \ | |
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36' \ | |
-H 'Accept: */*' \ | |
-H 'Referer: https://regaind.io/' \ | |
-H 'X-Requested-With: XMLHttpRequest' \ | |
-H 'Connection: keep-alive' \ | |
-H 'DNT: 1' \ | |
--compressed \ | |
-F csrfmiddlewaretoken=${MIDDLE_TOKEN} \ | |
-F file=@${IMAGE} > ${TMP_FILE} | |
cat ${TMP_FILE} | jq | |
if [ "${SET_COMMENT}" -eq "1" ]; then | |
if [ -x "${COMMENTER}" ]; then | |
cat ${TMP_FILE} | ${COMMENTER} -j ${IMAGE} | |
fi | |
fi | |
rm ${TMP_FILE} | |
rm ${COOKIES} | |
} | |
while getopts "hvc" OPTION | |
do | |
case ${OPTION} in | |
h) usage; exit 1;; | |
c) SET_COMMENT=1;; | |
v) showVersion; exit 1;; | |
\?) usage; exit 1;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
IMAGE=${1} | |
if [[ -f "${IMAGE}" ]]; then | |
ftype=$(file -b --mime-type "${IMAGE}") | |
if [[ "${ftype}" == "image/jpeg" ]]; then | |
getCookies | |
doKeeganize ${IMAGE} | |
else | |
echo "Error: expecting JPEG image file not ${ftype}" | |
fi | |
else | |
echo "Error: Image not found: ${IMAGE}" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment