Skip to content

Instantly share code, notes, and snippets.

@innocenzi
Last active September 22, 2020 14:30
Show Gist options
  • Save innocenzi/23ec48a138bed6321f12426db8b89f4e to your computer and use it in GitHub Desktop.
Save innocenzi/23ec48a138bed6321f12426db8b89f4e to your computer and use it in GitHub Desktop.
Clipboard to Imgur
#!/usr/bin/env bash
errors=false
screenshot_path=/tmp/screenshot.jpeg
default_client_id=c9a6efb3d7932fd
client_id="${IMGUR_CLIENT_ID:=$default_client_id}"
# Save as a file
xclip -se c -t image/jpeg -o > $screenshot_path 2>/dev/null
# No image
if [ $? -ne 0 ]; then
echo "The clipboard does not contain an image"
exit
fi
# Upload
response=$(curl -s -H "Authorization: Client-ID $client_id" -H "Expect: " -F "image=@$screenshot_path" https://api.imgur.com/3/image.xml 2>/dev/null)
# Handle response
if [ $? -ne 0 ]; then
echo "Upload failed" >&2
errors=true
continue
elif echo "$response" | grep -q 'success="0"'; then
echo "Error message from imgur:" >&2
msg="${response##*<error>}"
echo "${msg%%</error>*}" >&2
errors=true
continue
fi
# Parse the response and output our stuff
url="${response##*<link>}"
url="${url%%</link>*}"
delete_hash="${response##*<deletehash>}"
delete_hash="${delete_hash%%</deletehash>*}"
url=$(echo "$url" | sed 's/^http:/https:/')
# echo "Delete page: https://imgur.com/delete/$delete_hash" >&2
delete_url="https://imgur.com/delete/$delete_hash"
# Exit if errors
if $errors; then
exit 1
fi
# Append the URL to a string so we can put them all on the clipboard later
echo -n "$url" | xclip -selection clipboard
# Notify
notify-send "Clipboard has been upload to Imgur. Delete at $delete_url."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment