Created
April 5, 2015 00:35
-
-
Save colby/9045bb6e88eb4d0a7196 to your computer and use it in GitHub Desktop.
A simple shell script to upload images to Imgur.
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
#!/usr/bin/env bash | |
# variables | |
img="$1" | |
tmp=/tmp/imgur | |
id='3e7a4deb7ac67da' | |
link='https://api.imgur.com/3/upload' | |
# functions | |
errcho() { | |
>&2 echo "$*" | |
exit 1 | |
} | |
check() { | |
which "$1" || errcho "imgur: missing dependency: $1" | |
} | |
# deps | |
grep=$(check grep) | |
curl=$(check curl) | |
tr=$(check tr) | |
# args | |
[ $# -lt 1 ] && errcho "imgur: requires an image file path." | |
# main | |
$curl -sH "Authorization: Client-ID $id" -F "image=@${img}" "$link" > "$tmp" || errcho "imgur: curl failed." | |
if [[ $(cat "$tmp") =~ http ]]; then | |
$grep -oE "http:.*\.[a-z]{3,4}" "$tmp" | $tr -d '\\' | |
else | |
errcho "imgur: $(cut -d'"' -f6 "$tmp" | $tr '[:upper:]' '[:lower:]')" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment