Instantly share code, notes, and snippets.

Embed
What would you like to do?
[imgr upload] upload images to your imgur account from the command line #media #images
#!/bin/bash
# To use, create ~/.myimgurup, and put in your client ID and secret
# E.g.:
# CLIENT_ID=80238f8092
# CLIENT_SECRET=239a482b034c820
#
# Next run `base /path/to/my_imgurup.sh auth` to get your PIN and access token
# Finally, `base /path/to/my_imgurup.sh up /path/to/image.png` to upload an image
# URL: https://gist.github.com/92de52472c97532034a34fa3402f2aa0
CLIENT_ID="$(sed -nr 's/^CLIENT_ID=//p' ~/.myimgurup|head -1)"
CLIENT_SECRET="$(sed -nr 's/^CLIENT_SECRET=//p' ~/.myimgurup|head -1)"
PIN="$(sed -nr 's/^PIN=//p' ~/.myimgurup|head -1)"
ACCESS_TOKEN="$(sed -nr 's/^ACCESS_TOKEN=//p' ~/.myimgurup|head -1)"
command="$1"
shift
function upconf() {
tok="$1"
val="$2"
current="$(sed -nr "s/^$tok=/$tok=/p" ~/.myimgurup|head -1)"
if [ -n "$current" ]; then
sed -i "s/^$tok=.*/$tok=${val}/" ~/.myimgurup
else
echo "$tok=${val}" >>~/.myimgurup
fi
}
function upaccess() {
tmp=`mktemp`
curl -X POST -F "client_id=$CLIENT_ID" \
-F "client_secret=$CLIENT_SECRET" \
-F "grant_type=pin" -F "pin=$PIN" \
https://api.imgur.com/oauth2/token > "$tmp"
ls "$tmp"
token="$(jshon -e access_token -u < "$tmp")"
upconf ACCESS_TOKEN "$token"
}
case "$command" in
"auth")
url="https://api.imgur.com/oauth2/authorize?client_id=${CLIENT_ID}&response_type=pin"
echo "Opening $url"
echo "Copy the PIN from the window, and paste here"
xdg-open "$url"
read -p "PIN? " PIN
upconf PIN "$PIN"
upaccess
;;
"upaccess")
upaccess
;;
"up")
file="$1"
shift
tmp=`mktemp`
curl -X POST -H "Authorization: Bearer $ACCESS_TOKEN" -F "image=@$file" https://api.imgur.com/3/upload > "$tmp"
success="$(jshon -e success -u < "$tmp")"
if [ "$success" == "true" ]; then
link="$(jshon -e data -e link -u < "$tmp")"
echo "New image: $link"
which xclip && echo "$link" | xclip -i
else
echo "Upload failed"
fi
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment