Skip to content

Instantly share code, notes, and snippets.

@JayBrown
Last active March 2, 2021 20:54
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 JayBrown/e3f6152b275d7488f75fc00ba4af8dfa to your computer and use it in GitHub Desktop.
Save JayBrown/e3f6152b275d7488f75fc00ba4af8dfa to your computer and use it in GitHub Desktop.
imgurAU – imgur anonymous uploader for online images (shell script to use in Firefox using the OpenWith browser extension)
#!/bin/zsh
# shellcheck shell=bash
# imgur-au
# v0.4
# imgur anonymous uploader
export LANG=en_US.UTF-8
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin:/opt/local/bin:/opt/homebrew/bin:$HOME/.local/bin:$HOME/bin:$HOME/local/bin
# process="imgur-au"
procid="local.lcars.imgurAU"
uiprocess="imgurAU"
account=$(id -u)
logloc="/tmp/$procid.log"
currentdate=$(date)
if ! [[ -f $logloc ]] ; then
echo "++++++++ $currentdate ++++++++" > "$logloc"
else
echo -e "\n++++++++ $currentdate ++++++++" >> "$logloc"
fi
exec > >(tee -a "$logloc") 2>&1
if ! command -v imguru &>/dev/null ; then
echo "ERROR: imguru not installed"
_beep &
_notify "❌ Error: requisites!" "imguru is not installed"
exit 1
fi
savedir="$HOME/Pictures/imgurAU"
! [[ -d "$savedir" ]] && mkdir "$savedir"
tmpdir="/tmp/$procid"
! [[ -d "$tmpdir" ]] && mkdir "$tmpdir"
# https://help.imgur.com/hc/en-us/articles/115000083326-What-files-can-I-upload-What-is-the-size-limit-
# GIF: 200 MB max
# other: 20 MB max
# PNG > 5 MB > convert to JPEG
gifmax=209715200
othermax=20971520
pngmax=5242880
_beep () {
osascript -e 'beep' -e 'delay 0.5' &>/dev/null
}
_notify () {
osascript &>/dev/null << EOT
tell application "System Events"
display notification "$2" with title "$uiprocess [" & "$account" & "]" subtitle "$1"
end tell
EOT
}
if ! [[ $* ]] ; then
echo "No input: asking user..."
if pgrep "screencaptureui" &>/dev/null ; then
echo "Screen Shot is active"
sg_loc=$(/usr/libexec/PlistBuddy -c "Print:location" "$HOME/Library/Preferences/com.apple.screencapture.plist" 2>/dev/null)
! [[ $sg_loc ]] && sg_loc="$HOME/Pictures"
else
echo "Screen Shot is inactive"
sg_loc="$HOME/Pictures"
fi
uploadpath=$(osascript 2>/dev/null << EOF
tell application "System Events"
activate
set theDefaultPath to "$sg_loc" as string
set theUploadImage to POSIX path of (choose file with prompt "Please select an image file for upload to imgur…" ¬
of type {"png", "jpg", "jpeg", "gif", "tif", "tiff", "apng", "webm", "mp4", "m4v", "avi", "jpeg"} ¬
default location theDefaultPath)
end tell
theUploadImage
EOF
)
if ! [[ $uploadpath ]] || [[ $uploadpath == "false" ]] ; then
echo "User canceled"
else
echo -e "Selected file: $uploadpath\nUploading to imgur..."
movename=$(basename "$uploadpath")
shareurl=$(imguru "$uploadpath" 2>/dev/null | grep -v "^$")
if [[ $shareurl != "https://i.imgur.com/"* ]] ; then
echo -e "ERROR: upload failed\nimguru returned: $shareurl"
_beep &
_notify "⚠️ Upload error!" "$movename"
else
echo "Success: $shareurl"
echo "$shareurl" | pbcopy
_notify "✅ Uploaded & URL copied" "$shareurl"
fi
fi
exit
fi
for target in "$@"
do
if [[ $target == "http"* ]] ; then
url="$target"
localimg=false
elif [[ $target == "/"* ]] ; then
uploadpath="$target"
localimg=true
else
echo "ERROR: unknown input"
_beep &
_notify "❌ Error: unknown input!" "$target"
continue
fi
posixdate=$(date +%s)
converted=false
if $localimg ; then
if ! echo "$uploadpath" | grep -q -i -e "\.png$" -e "\.jpg$" -e "\.jpeg$" -e "\.tif$" -e "\.tiff$" -e "\.gif$" -e "\.apng$" -e "\.webm$" -e "\.mp4$" -e "\.m4v$" -e "\.avi$" &>/dev/null ; then
echo "ERROR: wrong input or format"
_beep &
_notify "❌ Error: wrong format!" "Not supported by imgur"
continue
fi
fsize=$(stat -f%z "$uploadpath" 2>/dev/null)
echo "File size: $fsize"
filename=$(basename "$uploadpath")
if ! [[ $fsize ]] || [[ $fsize -eq 0 ]] ; then
echo "ERROR: no file content"
_beep &
_notify "⚠️ Error: no file content!" "$filename"
continue
fi
else
echo "URL: $url"
### cleanup URL: needs testing
if ! echo "$url" | grep -q -i -e "\.png$" -e "\.jpg$" -e "\.jpeg" -e "\.tif$" -e "\.tiff$" -e "\.gif$" -e "\.apng" &>/dev/null ; then
echo "ERROR: wrong format"
_beep &
_notify "❌ Error: wrong format!" "Not supported by imgur"
continue
fi
client_id="51f229880e3ea84"
echo "Uploading to imgur directly..."
imgur_raw=$(curl -k -L -s --connect-timeout 10 -H "Authorization: Client-ID $client_id" -H "Expect: " -F "image=$url" "https://api.imgur.com/3/image.xml" 2>/dev/null)
shareurl=$(echo "$imgur_raw" | tail -n +2 | awk -F"<link>" '{print $NF}' | awk -F"</link>" '{print $1}' 2>/dev/null)
if [[ $shareurl != "https://i.imgur.com/"* ]] ; then
echo "ERROR: direct upload with cURL"
urlfilename=$(basename "$url")
filename="$posixdate-$urlfilename"
rm -f "$tmpdir/$filename" 2>/dev/null
echo "Caching: $tmpdir/$filename"
if ! curl -o "$tmpdir/$filename" -k -L -s --connect-timeout 10 "$url" &>/dev/null ; then
echo "ERROR: cURL exited with error"
_beep &
_notify "⚠️ cURL: cache error!" "$urlfilename"
rm -f "$tmpdir/$filename" 2>/dev/null
continue
fi
if ! [[ -f "$tmpdir/$filename" ]] ; then
echo "ERROR: cache file not found"
_beep &
_notify "⚠️ cURL: cache error!" "$urlfilename"
rm -f "$tmpdir/$filename" 2>/dev/null
continue
fi
fsize=$(stat -f%z "$tmpdir/$filename" 2>/dev/null)
echo "File size: $fsize"
if ! [[ $fsize ]] || [[ $fsize -eq 0 ]] ; then
echo "ERROR: cache file not properly downloaded"
_beep &
_notify "⚠️ cURL: cache error!" "$urlfilename"
rm -f "$tmpdir/$filename" 2>/dev/null
continue
fi
else
echo "Success: $shareurl"
echo "$shareurl" | pbcopy
_notify "✅ Uploaded & URL copied" "$shareurl"
continue
fi
fi
suffix="${filename##*.}"
echo "Extension: $suffix"
abort=false
if [[ $suffix =~ ^(gif|GIF)$ ]] ; then
[[ $fsize -gt "$gifmax" ]] && abort=true
else
[[ $fsize -gt "$othermax" ]] && abort=true
fi
if $abort ; then
echo "ERROR: file too large"
_beep &
_notify "⚠️ Error: file too large!" "$urlfilename"
continue
fi
if [[ $suffix =~ ^(png|PNG)$ ]] && [[ $fsize -gt "$pngmax" ]] ; then
echo "Converting PNG to JPEG..."
shortname="${filename%.*}"
if $localimg ; then
movename="$posixdate-$shortname.jpg"
if sips -s format jpeg "$uploadpath" --out "$tmpdir/$movename" &>/dev/null ; then
uploadpath="$tmpdir/$movename"
converted=true
fi
else
movename="$shortname.jpg"
if sips -s format jpeg "$tmpdir/$filename" --out "$tmpdir/$movename" &>/dev/null ; then
uploadpath="$tmpdir/$movename"
converted=true
fi
fi
else
if ! $localimg ; then
uploadpath="$tmpdir/$filename"
movename="$filename"
else
movename="$filename"
fi
fi
echo "Uploading to imgur..."
shareurl=$(imguru "$uploadpath" 2>/dev/null | grep -v "^$")
if [[ $shareurl != "https://i.imgur.com/"* ]] ; then
echo -e "ERROR: upload failed\nimguru returned: $shareurl"
_beep &
if ! $localimg ; then
_notify "⚠️ Upload error!" "$movename"
mv "$uploadpath" "$savedir/$movename"
else
_notify "⚠️ Upload error!" "$movename"
fi
else
echo "Success: $shareurl"
echo "$shareurl" | pbcopy
if ! $localimg ; then
_notify "✅ Uploaded & URL copied" "$shareurl"
rm -f "$uploadpath" 2>/dev/null
$converted && rm -f "$tmpdir/$filename" 2>/dev/null
else
_notify "✅ Uploaded & URL copied" "$shareurl"
$converted && rm -f "$uploadpath" 2>/dev/null
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment