Last active
December 4, 2015 21:48
-
-
Save LunaSquee/5dcf1d5701399bd04398 to your computer and use it in GitHub Desktop.
icy_screenshot - imgur_screenshot-based image uploader (to my server)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Based on imgur-screenshot by jomo | |
# https://github.com/jomo/imgur-screenshot | |
# | |
# Uploads images to https://lunasquee.eu.org/upload/ | |
# Deps: scrot, xclip, libnotify-bin (also grep and curl) | |
# Works only on linux-based systems | |
# | |
# $HOME/.config/icy-screenshot/settings.conf file can override options below | |
screenshot_select_command="scrot -s %img" | |
screenshot_window_command="scrot %img" | |
open_command="xdg-open %url" | |
file_name_format="icy-%Y_%m_%d-%H:%M:%S.png" | |
file_dir="$HOME/Pictures" | |
settings_path="$HOME/.config/icy-screenshot/settings.conf" | |
icy_icon_path="$HOME/Pictures/icycutie.png" | |
log_file="$HOME/.icy-screenshot.log" | |
copy_url="true" | |
keep_file="true" | |
open="true" | |
upload_files=("$@") | |
auto_delete="" | |
exit_on_selection_fail="false" | |
# end of options | |
if [ -f "$settings_path" ]; then | |
source "$settings_path" | |
fi | |
function notify() { | |
if [ "$1" = "error" ]; then | |
notify-send -a IcyScreenshot -u critical -c "im.error" -i "$icy_icon_path" -t 500 "icy: $2" "$3" | |
else | |
notify-send -a IcyScreenshot -u low -c "transfer.complete" -i "$icy_icon_path" -t 500 "icy: $2" "$3" | |
fi | |
} | |
function take_screenshot() { | |
echo "Please select area" | |
screenshot_select_cmd=${screenshot_select_command//\%img/$1} | |
screenshot_window_cmd=${screenshot_window_command//\%img/$1} | |
shot_err="$($screenshot_select_cmd &>/dev/null)" #takes a screenshot with selection | |
if [ "$?" != "0" ]; then | |
if [ "$shot_err" == "giblib error: no image grabbed" ]; then # scrot specific | |
echo "You cancelled the selection. Exiting." | |
exit 1 | |
else | |
echo "$shot_err" >&2 | |
echo "Couldn't make selective shot (mouse trapped?)." | |
if [ "$exit_on_selection_fail" = "false" ]; then | |
echo "Trying to grab active window instead." | |
if ! ($screenshot_window_cmd &>/dev/null); then | |
echo "Error for image '$1': '$shot_err'." | tee "$log_file" | |
notify error "Something went wrong :(" "Information has been logged" | |
exit 1 | |
fi | |
else | |
exit 1 | |
fi | |
fi | |
fi | |
} | |
function upload_image() { | |
echo "Uploading '$1'..." | |
response="$(curl -fsSL --stderr - -F "image=@$1" https://api.lunasqu.ee/upload)" | |
# JSON parser premium edition (not really) | |
if egrep -q '"success":\s*"Image' <<<"$response"; then | |
img_id="$(egrep -o '"image":\s*"[^"]+"' <<<"$response" | cut -d "\"" -f 4)" | |
del_id="$(egrep -o '"delete_hash":\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 $del_id $log_file" & | |
fi | |
handle_upload_success "https://lunasqu.ee/upload/${img_id}" "https://api.lunasqu.ee/upload/delete/${del_id}" "$1" | |
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 "" | |
echo "image URL: $1" | |
echo "delete URL: $2" | |
if [ "$copy_url" = "true" ]; then | |
echo -n "$1" | xclip -selection clipboard | |
echo "URL copied to clipboard" | |
fi | |
# print to log file: image link, image location, delete link | |
echo -e "$1\t$3\t$2" >> "$log_file" | |
notify ok "Upload done!" "$1" | |
if [ ! -z "$open_command" ] && [ "$open" = "true" ]; then | |
open_cmd=${open_command//\%url/$1} | |
open_cmd=${open_cmd//\%img/$2} | |
echo "Opening '$open_cmd'" | |
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" | |
} | |
if [ -z "$upload_files" ]; then | |
upload_files[0]="" | |
fi | |
for upload_file in "${upload_files[@]}"; do | |
if [ -z "$upload_file" ]; then | |
cd "$file_dir" | |
# 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 | |
upload_image "$img_file" | |
if [ "$keep_file" = "false" ] && [ -z "$1" ]; then | |
echo "Deleting temp file ${file_dir}/${img_file}" | |
rm -rf "$img_file" | |
fi | |
echo "" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment