From f67c55eac8fb721c1c892449c22b965a130caff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 18 Jun 2021 21:12:45 +0200 Subject: Add imgur script --- imgur | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 imgur (limited to 'imgur') diff --git a/imgur b/imgur new file mode 100644 index 0000000..f459fc3 --- /dev/null +++ b/imgur @@ -0,0 +1,123 @@ +#!/bin/sh + +# Imgur script by Bart Nagel +# Improvements by Tino Sino +# Made POSIX-compliant by John Ankarström +# 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 envionment variable +# to avoid limits +default_client_id=c9a6efb3d7932fd +client_id="${IMGUR_CLIENT_ID:=$default_client_id}" + +# Function to output usage instructions +usage() { + echo "Usage: $(basename $0) [ [...]]" >&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 +} + +# Function to upload a path +# First argument should be a content spec understood by curl's -F option +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 [ x"$1" = x"-h" -o x"$1" = x"--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 2>&1 >/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 + case "$file" + http://*|https://*) + # URL -> imgur + response=$(upload "$file") 2>/dev/null + ;; + *) + # File -> imgur + # Check file exists + if [ x"$file" != x"-" -a ! -f "$file" ]; then + echo "File '$file' doesn't exist; skipping" >&2 + errors=true + continue + fi + response=$(upload "@$file") 2>/dev/null + esac + + 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##*}" + echo "${msg%%*}" >&2 + errors=true + continue + fi + + # Parse the response and output our stuff + url="${response##*}" + url="${url%%*}" + delete_hash="${response##*}" + delete_hash="${delete_hash%%*}" + 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 + clip+="$url" + if [ $# -gt 0 ]; then + clip+=$'\n' + fi +done + +# Put the URLs on the clipboard if we can +if type pbcopy 2>&1 >/dev/null; then + echo -n "$clip" | pbcopy +elif type clip 2>&1 >/dev/null; then + echo -n "$clip" | clip +elif [ $DISPLAY ]; then + if type xsel 2>&1 >/dev/null; then + echo -n "$clip" | xsel -i + elif type xclip 2>&1 >/dev/null; then + echo -n "$clip" | xclip + 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 -- cgit v1.2.3