<NME> imgur.sh
<BEF> #!/usr/bin/env bash

# Imgur script by Bart Nagel <bart@tremby.net>
# Improvements by Tino Sino <robottinosino@gmail.com>
# Version 6 or more
# I release this into the public domain. Do with it what you will.
# The latest version can be found at https://github.com/tremby/imgur.sh

# API Key provided by Bart;
# replace with your own or specify yours as IMGUR_CLIENT_ID environment variable
# to avoid limits
default_client_id=c9a6efb3d7932fd
client_id="${IMGUR_CLIENT_ID:=$default_client_id}"

# Function to output usage instructions
function usage {
	echo "Usage: $(basename $0) [<filename|URL> [...]]" >&2
	echo
	echo "Upload images to imgur and output their new URLs to stdout. Each one's" >&2
	echo "delete page is output to stderr between the view URLs." >&2
	echo
	echo "A filename can be - to read from stdin. If no filename is given, stdin is read." >&2
	echo
	echo "If xsel, xclip, pbcopy, or clip is available," >&2
	echo "the URLs are put on the X selection or clipboard for easy pasting." >&2
	echo "Use environment variables to set special options for your clipboard program (see code)." >&2
}

# Function to upload a path
# First argument should be a content spec understood by curl's -F option
function upload {
	curl -s -H "Authorization: Client-ID $client_id" -H "Expect: " -F "image=$1" https://api.imgur.com/3/image.xml
	# The "Expect: " header is to get around a problem when using this through
	# the Squid proxy. Not sure if it's a Squid bug or what.
}

# Check arguments
if [ "$1" == "-h" -o "$1" == "--help" ]; then
	usage
	exit 0
elif [ $# -eq 0 ]; then
	echo "No file specified; reading from stdin" >&2
	exec "$0" -
fi

# Check curl is available
type curl &>/dev/null || {
	echo "Couldn't find curl, which is required." >&2
	exit 17
}

clip=""
errors=false

# Loop through arguments
while [ $# -gt 0 ]; do
	file="$1"
	shift

	# Upload the image
	if [[ "$file" =~ ^https?:// ]]; then
		# URL -> imgur
		response=$(upload "$file") 2>/dev/null
	else
		# File -> imgur
		# Check file exists
		if [ "$file" != "-" -a ! -f "$file" ]; then
			echo "File '$file' doesn't exist; skipping" >&2
			errors=true
			continue
		fi
		response=$(upload "@$file") 2>/dev/null
	url="${url%%</link>*}"
	delete_hash="${response##*<deletehash>}"
	delete_hash="${delete_hash%%</deletehash>*}"
	echo $url
	echo "Delete page: https://imgur.com/delete/$delete_hash" >&2

	# Append the URL to a string so we can put them all on the clipboard later
		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>*}"
	echo $url
	echo "Delete page: https://imgur.com/delete/$delete_hash" >&2

	# Append the URL to a string so we can put them all on the clipboard later
	clip+="$url"
	if [ $# -gt 0 ]; then
		clip+=$'\n'
	fi
done

# Put the URLs on the clipboard if we can
if type pbcopy &>/dev/null; then
	echo -n "$clip" | pbcopy $IMGUR_PBCOPY_OPTIONS
elif type clip &>/dev/null; then
	echo -n "$clip" | clip $IMGUR_CLIP_OPTIONS
elif [ $DISPLAY ]; then
	if type xsel &>/dev/null; then
		echo -n "$clip" | xsel -i $IMGUR_XSEL_OPTIONS
	elif type xclip &>/dev/null; then
		echo -n "$clip" | xclip $IMGUR_XCLIP_OPTIONS
	else
		echo "Haven't copied to the clipboard: no xsel or xclip" >&2
	fi
else
	echo "Haven't copied to the clipboard: no \$DISPLAY or pbcopy or clip" >&2
fi

if $errors; then
	exit 1
fi

<MSG> Merge pull request #5 from x89/https

HTTPS on URL link
<DFF> @@ -73,7 +73,7 @@ while [ $# -gt 0 ]; do
 	url="${url%%</link>*}"
 	delete_hash="${response##*<deletehash>}"
 	delete_hash="${delete_hash%%</deletehash>*}"
-	echo $url
+	echo $url | sed 's/^http:/https:/'
 	echo "Delete page: https://imgur.com/delete/$delete_hash" >&2
 
 	# Append the URL to a string so we can put them all on the clipboard later

{'commit': 'diff --git a/imgur.sh b/imgur.sh\nindex b5d3d74..a562d0f 100755\n--- a/imgur.sh\n+++ b/imgur.sh\n@@ -73,7 +73,7 @@ while [ $# -gt 0 ]; do\n \turl="${url%%</link>*}"\n \tdelete_hash="${response##*<deletehash>}"\n \tdelete_hash="${delete_hash%%</deletehash>*}"\n-\techo $url\n+\techo $url | sed \'s/^http:/https:/\'\n \techo "Delete page: https://imgur.com/delete/$delete_hash" >&2\n \n \t# Append the URL to a string so we can put them all on the clipboard later\n', 'commit_subject': 'Merge pull request #5 from x89/https', 'repo_name': 'tremby/imgur.sh', 'committer': {'name': 'GitHub', 'email': '1505422b2465e9a84f6fdfaa161078890c593f06@github.com', 'time_sec': '1475097802', 'tz_offset': '-420', 'date': {'seconds': '1475097802'}}, 'commit_tree': '60d974cba4965287f1e3229b1224b479bbca9b3f', 'commit_message': 'Merge pull request #5 from x89/https\n\nHTTPS on URL link', 'language_name': 'Shell', 'license': 'unlicense', 'hunks': '@@ -73,7 +73,7 @@ while [ $# -gt 0 ]; do\n \turl="${url%%</link>*}"\n \tdelete_hash="${response##*<deletehash>}"\n \tdelete_hash="${delete_hash%%</deletehash>*}"\n-\techo $url\n+\techo $url | sed \'s/^http:/https:/\'\n \techo "Delete page: https://imgur.com/delete/$delete_hash" >&2\n \n \t# Append the URL to a string so we can put them all on the clipboard later\n', 'addition_count': 1, 'deletion_count': 1, 'src_file': 'a/imgur.sh', 'tgt_file': 'b/imgur.sh', 'file_extension': '.sh', 'before_file': '#!/usr/bin/env bash\n\n# Imgur script by Bart Nagel <bart@tremby.net>\n# Improvements by Tino Sino <robottinosino@gmail.com>\n# Version 6 or more\n# I release this into the public domain. Do with it what you will.\n# The latest version can be found at https://github.com/tremby/imgur.sh\n\n# API Key provided by Bart;\n# replace with your own or specify yours as IMGUR_CLIENT_ID environment variable\n# to avoid limits\ndefault_client_id=c9a6efb3d7932fd\nclient_id="${IMGUR_CLIENT_ID:=$default_client_id}"\n\n# Function to output usage instructions\nfunction usage {\n\techo "Usage: $(basename $0) [<filename|URL> [...]]" >&2\n\techo\n\techo "Upload images to imgur and output their new URLs to stdout. Each one\'s" >&2\n\techo "delete page is output to stderr between the view URLs." >&2\n\techo\n\techo "A filename can be - to read from stdin. If no filename is given, stdin is read." >&2\n\techo\n\techo "If xsel, xclip, pbcopy, or clip is available," >&2\n\techo "the URLs are put on the X selection or clipboard for easy pasting." >&2\n\techo "Use environment variables to set special options for your clipboard program (see code)." >&2\n}\n\n# Function to upload a path\n# First argument should be a content spec understood by curl\'s -F option\nfunction upload {\n\tcurl -s -H "Authorization: Client-ID $client_id" -H "Expect: " -F "image=$1" https://api.imgur.com/3/image.xml\n\t# The "Expect: " header is to get around a problem when using this through\n\t# the Squid proxy. Not sure if it\'s a Squid bug or what.\n}\n\n# Check arguments\nif [ "$1" == "-h" -o "$1" == "--help" ]; then\n\tusage\n\texit 0\nelif [ $# -eq 0 ]; then\n\techo "No file specified; reading from stdin" >&2\n\texec "$0" -\nfi\n\n# Check curl is available\ntype curl &>/dev/null || {\n\techo "Couldn\'t find curl, which is required." >&2\n\texit 17\n}\n\nclip=""\nerrors=false\n\n# Loop through arguments\nwhile [ $# -gt 0 ]; do\n\tfile="$1"\n\tshift\n\n\t# Upload the image\n\tif [[ "$file" =~ ^https?:// ]]; then\n\t\t# URL -> imgur\n\t\tresponse=$(upload "$file") 2>/dev/null\n\telse\n\t\t# File -> imgur\n\t\t# Check file exists\n\t\tif [ "$file" != "-" -a ! -f "$file" ]; then\n\t\t\techo "File \'$file\' doesn\'t exist; skipping" >&2\n\t\t\terrors=true\n\t\t\tcontinue\n\t\tfi\n\t\tresponse=$(upload "@$file") 2>/dev/null\n\turl="${url%%</link>*}"\n\tdelete_hash="${response##*<deletehash>}"\n\tdelete_hash="${delete_hash%%</deletehash>*}"\n\techo $url\n\techo "Delete page: https://imgur.com/delete/$delete_hash" >&2\n\n\t# Append the URL to a string so we can put them all on the clipboard later\n\t\techo "Error message from imgur:" >&2\n\t\tmsg="${response##*<error>}"\n\t\techo "${msg%%</error>*}" >&2\n\t\terrors=true\n\t\tcontinue\n\tfi\n\n\t# Parse the response and output our stuff\n\turl="${response##*<link>}"\n\turl="${url%%</link>*}"\n\tdelete_hash="${response##*<deletehash>}"\n\tdelete_hash="${delete_hash%%</deletehash>*}"\n\techo $url\n\techo "Delete page: https://imgur.com/delete/$delete_hash" >&2\n\n\t# Append the URL to a string so we can put them all on the clipboard later\n\tclip+="$url"\n\tif [ $# -gt 0 ]; then\n\t\tclip+=$\'\\n\'\n\tfi\ndone\n\n# Put the URLs on the clipboard if we can\nif type pbcopy &>/dev/null; then\n\techo -n "$clip" | pbcopy $IMGUR_PBCOPY_OPTIONS\nelif type clip &>/dev/null; then\n\techo -n "$clip" | clip $IMGUR_CLIP_OPTIONS\nelif [ $DISPLAY ]; then\n\tif type xsel &>/dev/null; then\n\t\techo -n "$clip" | xsel -i $IMGUR_XSEL_OPTIONS\n\telif type xclip &>/dev/null; then\n\t\techo -n "$clip" | xclip $IMGUR_XCLIP_OPTIONS\n\telse\n\t\techo "Haven\'t copied to the clipboard: no xsel or xclip" >&2\n\tfi\nelse\n\techo "Haven\'t copied to the clipboard: no \\$DISPLAY or pbcopy or clip" >&2\nfi\n\nif $errors; then\n\texit 1\nfi\n', 'message': 'Merge pull request #5 from x89/https\n\nHTTPS on URL link', 'author_name': 'GitHub', 'lang': 'sh'}