First Version
Edited by Dober
This commit is contained in:
parent
3c73fc8c86
commit
0d7db854c6
307
imgur-screenshot.sh
Normal file
307
imgur-screenshot.sh
Normal file
@ -0,0 +1,307 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# https://github.com/jomo/imgur-screenshot
|
||||||
|
# https://imgur.com/tools
|
||||||
|
|
||||||
|
if [ "${1}" = "--debug" ]; then
|
||||||
|
echo "########################################"
|
||||||
|
echo "Enabling debug mode"
|
||||||
|
echo "Please remove credentials before pasting"
|
||||||
|
echo "########################################"
|
||||||
|
echo ""
|
||||||
|
uname -a
|
||||||
|
for arg in ${0} "${@}"; do
|
||||||
|
echo -n "'${arg}' "
|
||||||
|
done
|
||||||
|
echo -e "\n"
|
||||||
|
shift
|
||||||
|
set -x
|
||||||
|
fi
|
||||||
|
|
||||||
|
current_version="v1.7.4"
|
||||||
|
|
||||||
|
function is_mac() {
|
||||||
|
uname | grep -q "Darwin"
|
||||||
|
}
|
||||||
|
|
||||||
|
### IMGUR-SCREENSHOT DEFAULT CONFIG ####
|
||||||
|
|
||||||
|
# You can override the config in ~/.config/imgur-screenshot/settings.conf
|
||||||
|
|
||||||
|
imgur_anon_id="ea6c0ef2987808e"
|
||||||
|
imgur_icon_path="${HOME}/Pictures/imgur.png"
|
||||||
|
|
||||||
|
imgur_acct_key=""
|
||||||
|
imgur_secret=""
|
||||||
|
login="false"
|
||||||
|
album_title=""
|
||||||
|
album_id=""
|
||||||
|
credentials_file="${HOME}/.config/imgur-screenshot/credentials.conf"
|
||||||
|
|
||||||
|
file_name_format="imgur-%Y_%m_%d-%H:%M:%S.png" # when using scrot, must end with .png!
|
||||||
|
file_dir="${HOME}/Pictures"
|
||||||
|
|
||||||
|
upload_connect_timeout="5"
|
||||||
|
upload_timeout="120"
|
||||||
|
upload_retries="1"
|
||||||
|
|
||||||
|
if is_mac; then
|
||||||
|
screenshot_select_command="screencapture -i %img"
|
||||||
|
screenshot_window_command="screencapture -iWa %img"
|
||||||
|
screenshot_full_command="screencapture %img"
|
||||||
|
open_command="open %url"
|
||||||
|
else
|
||||||
|
screenshot_select_command="scrot -s %img"
|
||||||
|
screenshot_window_command="scrot %img"
|
||||||
|
screenshot_full_command="scrot %img"
|
||||||
|
open_command="xdg-open %url"
|
||||||
|
fi
|
||||||
|
open="true"
|
||||||
|
|
||||||
|
mode="select"
|
||||||
|
edit_command="gimp %img"
|
||||||
|
edit="false"
|
||||||
|
exit_on_album_creation_fail="true"
|
||||||
|
|
||||||
|
log_file="${HOME}/.imgur-screenshot.log"
|
||||||
|
|
||||||
|
auto_delete=""
|
||||||
|
copy_url="true"
|
||||||
|
keep_file="true"
|
||||||
|
check_update="true"
|
||||||
|
|
||||||
|
# NOTICE: if you make changes here, also edit the docs at
|
||||||
|
# https://github.com/jomo/imgur-screenshot/wiki/Config
|
||||||
|
|
||||||
|
# You can override the config in ~/.config/imgur-screenshot/settings.conf
|
||||||
|
|
||||||
|
############## END CONFIG ##############
|
||||||
|
|
||||||
|
settings_path="${HOME}/.config/imgur-screenshot/settings.conf"
|
||||||
|
if [ -f "${settings_path}" ]; then
|
||||||
|
source "${settings_path}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# dependency check
|
||||||
|
if [ "${1}" = "--check" ]; then
|
||||||
|
(which grep &>/dev/null && echo "OK: found grep") || echo "ERROR: grep not found"
|
||||||
|
if is_mac; then
|
||||||
|
if which growlnotify &>/dev/null; then
|
||||||
|
echo "OK: found growlnotify"
|
||||||
|
elif which terminal-notifier &>/dev/null; then
|
||||||
|
echo "OK: found terminal-notifier"
|
||||||
|
else
|
||||||
|
echo "ERROR: growlnotify nor terminal-notifier found"
|
||||||
|
fi
|
||||||
|
(which screencapture &>/dev/null && echo "OK: found screencapture") || echo "ERROR: screencapture not found"
|
||||||
|
(which pbcopy &>/dev/null && echo "OK: found pbcopy") || echo "ERROR: pbcopy not found"
|
||||||
|
else
|
||||||
|
(which notify-send &>/dev/null && echo "OK: found notify-send") || echo "ERROR: notify-send (from libnotify-bin) not found"
|
||||||
|
(which scrot &>/dev/null && echo "OK: found scrot") || echo "ERROR: scrot not found"
|
||||||
|
(which xclip &>/dev/null && echo "OK: found xclip") || echo "ERROR: xclip not found"
|
||||||
|
fi
|
||||||
|
(which curl &>/dev/null && echo "OK: found curl") || echo "ERROR: curl not found"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# notify <'ok'|'error'> <title> <text>
|
||||||
|
function notify() {
|
||||||
|
if is_mac; then
|
||||||
|
if which growlnotify &>/dev/null; then
|
||||||
|
growlnotify --icon "${imgur_icon_path}" --iconpath "${imgur_icon_path}" --title "${2}" --message "${3}"
|
||||||
|
else
|
||||||
|
terminal-notifier -appIcon "${imgur_icon_path}" -contentImage "${imgur_icon_path}" -title "imgur: ${2}" -message "${3}"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [ "${1}" = "error" ]; then
|
||||||
|
notify-send -a ImgurScreenshot -u critical -c "im.error" -i "${imgur_icon_path}" -t 500 "imgur: ${2}" "${3}"
|
||||||
|
else
|
||||||
|
notify-send -a ImgurScreenshot -u low -c "transfer.complete" -i "${imgur_icon_path}" -t 500 "imgur: ${2}" "${3}"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function take_screenshot() {
|
||||||
|
echo "Please select area"
|
||||||
|
is_mac || sleep 0.1 # https://bbs.archlinux.org/viewtopic.php?pid=1246173#p1246173
|
||||||
|
|
||||||
|
cmd="screenshot_${mode}_command"
|
||||||
|
cmd=${!cmd//\%img/${1}}
|
||||||
|
|
||||||
|
shot_err="$(${cmd} &>/dev/null)" #takes a screenshot with selection
|
||||||
|
if [ "${?}" != "0" ]; then
|
||||||
|
echo "Failed to take screenshot '${1}': '${shot_err}'. For more information visit https://github.com/jomo/imgur-screenshot/wiki/Troubleshooting" | tee -a "${log_file}"
|
||||||
|
notify error "Something went wrong :(" "Information has been logged"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function delete_image() {
|
||||||
|
response="$(curl --compressed -X DELETE -fsSL --stderr - -H "Authorization: Client-ID ${1}" "https://api.imgur.com/3/image/${2}")"
|
||||||
|
if egrep -q '"success":\s*true' <<<"${response}"; then
|
||||||
|
echo "Image successfully deleted (delete hash: ${2})." >> "${3}"
|
||||||
|
else
|
||||||
|
echo "The Image could not be deleted: ${response}." >> "${3}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function upload_anonymous_image() {
|
||||||
|
echo "Uploading '${1}'..."
|
||||||
|
title="$(echo "${1}" | rev | cut -d "/" -f 1 | cut -d "." -f 2- | rev)"
|
||||||
|
|
||||||
|
response="$(curl --compressed --connect-timeout "${upload_connect_timeout}" -m "${upload_timeout}" --retry "${upload_retries}" -fsSL --stderr - -F "filename=${title}" -F "files[]=@\"${1}\"" https://imhst.ru/upload.php)"
|
||||||
|
# JSON parser premium edition (not really)
|
||||||
|
if egrep -q '"success":\s*true' <<<"${response}"; then
|
||||||
|
img_id="$(egrep -o '"url":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
|
||||||
|
#img_ext="$(egrep -o '"link":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4 | rev | cut -d "." -f 1 | rev)" # "link" itself has ugly '\/' escaping and no https!
|
||||||
|
#del_id="$(egrep -o '"deletehash":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
|
||||||
|
|
||||||
|
if [ ! -z "${auto_delete}" ]; then
|
||||||
|
export -f delete_image
|
||||||
|
echo "Deleting image in ${auto_delete} seconds."
|
||||||
|
nohup /bin/bash -c "sleep ${auto_delete} && delete_image ${imgur_anon_id} ${del_id} ${log_file}" &
|
||||||
|
fi
|
||||||
|
|
||||||
|
handle_upload_success "${img_id}" "${1}"#"https://imgur.com/delete/${del_id}"
|
||||||
|
else # upload failed
|
||||||
|
err_msg="$(egrep -o '"error":\s*"[^"]+"' <<<"${response}" | cut -d "\"" -f 4)"
|
||||||
|
test -z "${err_msg}" && err_msg="${response}"
|
||||||
|
handle_upload_error "${err_msg}" "${1}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_upload_success() {
|
||||||
|
echo ""
|
||||||
|
delwin="$(echo ${1} | cut -d "/" -f 4-)"
|
||||||
|
delwin="$(echo https://i.imhst.ru/${delwin})"
|
||||||
|
echo "image link: ${delwin}"
|
||||||
|
#echo "delete link: ${2}"
|
||||||
|
|
||||||
|
if [ "${copy_url}" = "true" ] && [ -z "${album_title}" ]; then
|
||||||
|
if is_mac; then
|
||||||
|
echo -n "${delwin}" | pbcopy
|
||||||
|
else
|
||||||
|
echo -n "${delwin}" | xclip -selection clipboard
|
||||||
|
fi
|
||||||
|
echo "URL copied to clipboard"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# print to log file: image link, image location, delete link
|
||||||
|
echo -e "${delwin}\t${2}" >> "${log_file}"
|
||||||
|
|
||||||
|
notify ok "Upload done!" "${delwin}"
|
||||||
|
|
||||||
|
if [ ! -z "${open_command}" ] && [ "${open}" = "true" ]; then
|
||||||
|
open_cmd=${open_command//\%url/${1}}
|
||||||
|
#open_cmd=${open_cmd//\%img/${2}}
|
||||||
|
echo "Opening '${delwin}'"
|
||||||
|
eval "${open_cmd}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_upload_error() {
|
||||||
|
error="Upload failed: \"${1}\""
|
||||||
|
echo "${error}"
|
||||||
|
echo -e "Error\t${2}\t${error}" >> "${log_file}"
|
||||||
|
notify error "Upload failed :(" "${1}"
|
||||||
|
}
|
||||||
|
|
||||||
|
while [ ${#} != 0 ]; do
|
||||||
|
case "${1}" in
|
||||||
|
-h | --help)
|
||||||
|
echo "usage: ${0} [--debug] [-c | --check | -v | -h | -u]"
|
||||||
|
echo " ${0} [--debug] [option]... [file]..."
|
||||||
|
echo ""
|
||||||
|
echo " --debug Enable debugging, must be first option"
|
||||||
|
echo " -h, --help Show this help, exit"
|
||||||
|
echo " -v, --version Show current version, exit"
|
||||||
|
echo " --check Check if all dependencies are installed, exit"
|
||||||
|
echo " -o, --open <true|false> Override 'open' config"
|
||||||
|
echo " -e, --edit <true|false> Override 'edit' config"
|
||||||
|
echo " -i, --edit-command <command> Override 'edit_command' config (include '%img'), sets --edit 'true'"
|
||||||
|
echo " -k, --keep-file <true|false> Override 'keep_file' config"
|
||||||
|
echo " -d, --auto-delete <s> Automatically delete image after <s> seconds"
|
||||||
|
echo " file Upload file instead of taking a screenshot"
|
||||||
|
exit 0;;
|
||||||
|
-v | --version)
|
||||||
|
echo "${current_version}"
|
||||||
|
exit 0;;
|
||||||
|
-s | --select)
|
||||||
|
mode="select"
|
||||||
|
shift;;
|
||||||
|
-w | --window)
|
||||||
|
mode="window"
|
||||||
|
shift;;
|
||||||
|
-f | --full)
|
||||||
|
mode="full"
|
||||||
|
shift;;
|
||||||
|
-o | --open)
|
||||||
|
open="${2}"
|
||||||
|
shift 2;;
|
||||||
|
-e | --edit)
|
||||||
|
edit="${2}"
|
||||||
|
shift 2;;
|
||||||
|
-i | --edit-command)
|
||||||
|
edit_command="${2}"
|
||||||
|
edit="true"
|
||||||
|
shift 2;;
|
||||||
|
-k | --keep-file)
|
||||||
|
keep_file="${2}"
|
||||||
|
shift 2;;
|
||||||
|
-d | --auto-delete)
|
||||||
|
auto_delete="${2}"
|
||||||
|
shift 2;;
|
||||||
|
*)
|
||||||
|
upload_files=("${@}")
|
||||||
|
break;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ -z "${upload_files}" ]; then
|
||||||
|
upload_files[0]=""
|
||||||
|
fi
|
||||||
|
|
||||||
|
for upload_file in "${upload_files[@]}"; do
|
||||||
|
|
||||||
|
if [ -z "${upload_file}" ]; then
|
||||||
|
cd "${file_dir}" || exit 1
|
||||||
|
|
||||||
|
# new filename with date
|
||||||
|
img_file="$(date +"${file_name_format}")"
|
||||||
|
take_screenshot "${img_file}"
|
||||||
|
else
|
||||||
|
# upload file instead of screenshot
|
||||||
|
img_file="${upload_file}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# get full path
|
||||||
|
img_file="$(cd "$( dirname "${img_file}")" && echo "$(pwd)/$(basename "${img_file}")")"
|
||||||
|
|
||||||
|
# check if file exists
|
||||||
|
if [ ! -f "${img_file}" ]; then
|
||||||
|
echo "file '${img_file}' doesn't exist !"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# open image in editor if configured
|
||||||
|
if [ "${edit}" = "true" ]; then
|
||||||
|
edit_cmd=${edit_command//\%img/${img_file}}
|
||||||
|
echo "Opening editor '${edit_cmd}'"
|
||||||
|
if ! (eval "${edit_cmd}"); then
|
||||||
|
echo "Error for image '${img_file}': command '${edit_cmd}' failed, not uploading. For more information visit https://github.com/jomo/imgur-screenshot/wiki/Troubleshooting" | tee -a "${log_file}"
|
||||||
|
notify error "Something went wrong :(" "Information has been logged"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
upload_anonymous_image "${img_file}"
|
||||||
|
|
||||||
|
|
||||||
|
# delete file if configured
|
||||||
|
if [ "${keep_file}" = "false" ] && [ -z "${1}" ]; then
|
||||||
|
echo "Deleting temp file ${file_dir}/${img_file}"
|
||||||
|
rm -rf "${img_file}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
done
|
||||||
Reference in New Issue
Block a user