Skip to content

Instantly share code, notes, and snippets.

@vadixidav
Created July 11, 2016 17:39
Show Gist options
  • Save vadixidav/f8f493c9e4bdd08971d8cdc526693f8a to your computer and use it in GitHub Desktop.
Save vadixidav/f8f493c9e4bdd08971d8cdc526693f8a to your computer and use it in GitHub Desktop.
#!/bin/bash
CLIENT_ID="e442433569512b1"
CLIENT_SECRET="b088d1efe225fb9cfa8bad79290a77209c13de5e"
EXPIRE_TIME="3500"
CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/imgur"
mkdir -p "$CACHE_DIR"
ACCESS_TOKEN_FILE="$CACHE_DIR/token-access"
REFRESH_TOKEN_FILE="$CACHE_DIR/token-refresh"
function json {
echo "$1" | grep -Po "\"$2\":\".*?\"[,}]" | sed "s/^\"$2\":\"//;s/\"[,}]$//;s:\\\\/:/:g"
}
function show_error {
notify-send "imgur uploader" "$1"
}
function check_for_error {
if [ "$(echo "$1" | grep error)" ]; then
error=$(json "$1" "error")
return 0
fi
return 1
}
# check arguments
if [ $# -lt 2 ]; then
echo "usage: imgur [ALBUM ID] [FILES...]"
exit 1
fi
# get album
album="$1"
shift
# authenticate
if [[ -f "$REFRESH_TOKEN_FILE" && -f "$ACCESS_TOKEN_FILE" ]]; then
# see if the token's expired
if [ "$(expr $(date +%s) - $(date +%s -r "$ACCESS_TOKEN_FILE"))" -gt "$EXPIRE_TIME" ]; then
echo "expired"
# Get new access token
refresh_token=$(cat "$REFRESH_TOKEN_FILE")
response=$(curl -d "refresh_token=$refresh_token" -d "client_id=$CLIENT_ID" -d "client_secret=$CLIENT_SECRET" -d "grant_type=refresh_token" https://api.imgur.com/oauth2/token 2>/dev/null)
if check_for_error "$response"; then
show_error "$error"
exit 1
fi
# Record token
access_token=$(json "$response" "access_token")
echo "$access_token" > "$ACCESS_TOKEN_FILE"
else
# Read the existing token from the disk
access_token=$(cat "$ACCESS_TOKEN_FILE")
fi
else
# Get the PIN from the user
xdg-open "https://api.imgur.com/oauth2/authorize?client_id=$CLIENT_ID&response_type=pin" >/dev/null 2>&1 &
pin=$(zenity --entry --title="imgur uploader" --text="Enter the PIN" 2>/dev/null)
if [ ! "$pin" ]; then
show_error "no pin selected"
exit 1
fi
# Get tokens
response=$(curl -d "client_id=$CLIENT_ID" -d "client_secret=$CLIENT_SECRET" -d "grant_type=pin" -d "pin=$pin" https://api.imgur.com/oauth2/token/ 2>/dev/null)
if check_for_error "$response"; then
show_error "$error"
exit 1
fi
# Save values
access_token=$(json "$response" "access_token")
refresh_token=$(json "$response" "refresh_token")
echo "$access_token" > "$ACCESS_TOKEN_FILE"
echo "$refresh_token" > "$REFRESH_TOKEN_FILE"
fi
error=
clip=
# loop through arguments
for file; do
# check file exists
if [ ! -f "$file" ]; then
error="$file doesn't exist"
break
fi
# upload the image
response=$(curl -H "Authorization: Bearer $access_token" -F "image=@$file" -F "album=$album" -F "title=$(basename "$file")" https://api.imgur.com/3/image 2>/dev/null)
# check for error
if check_for_error "$response"; then
error="$(json "$response" "error")"
break
fi
# parse the response and output our stuff
url=$(json "$response" "link")
echo "$url"
if [ "$clip" ]; then
clip="$clip
$url"
else
clip="$url"
fi
done
if [ "$clip" ]; then
echo -n "$clip" | xclip -selection c
notify-send "imgur uploader" "$clip"
fi
if [ "$error" ]; then
show_error "$error"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment