Skip to content

Instantly share code, notes, and snippets.

@tbaschak
Forked from goodevilgenius/my_imgurup.sh
Last active January 17, 2017 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbaschak/0e093adb6d2492a3d3a005639fc7937c to your computer and use it in GitHub Desktop.
Save tbaschak/0e093adb6d2492a3d3a005639fc7937c to your computer and use it in GitHub Desktop.
upload images to your imgur account from the command line
#!/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 -n 's/^CLIENT_ID=//p' ~/.myimgurup|head -1)"
CLIENT_SECRET="$(sed -n 's/^CLIENT_SECRET=//p' ~/.myimgurup|head -1)"
PIN="$(sed -n 's/^PIN=//p' ~/.myimgurup|head -1)"
ACCESS_TOKEN="$(sed -n 's/^ACCESS_TOKEN=//p' ~/.myimgurup|head -1)"
command="$1"
shift
function upconf() {
tok="$1"
val="$2"
current="$(sed -n"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 pbcopy && echo "$link" | pbcopy
else
echo "Upload failed"
fi
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment