Skip to content

Instantly share code, notes, and snippets.

@banister
Created January 23, 2018 20:17
Show Gist options
  • Save banister/454dcea827c5404d471015bbecf4102f to your computer and use it in GitHub Desktop.
Save banister/454dcea827c5404d471015bbecf4102f to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
CLIENT_ID=<REDACTED>
LOG_FILE=~/.image_uploader.log
touch $LOG_FILE
exec 1>>$LOG_FILE # redirect stdout to the log file
exec 2>&1 # ditto for stderr
# folder to watch
SCREENSHOT_FOLDER=/home/john/Pictures/screenshots
created_file() {
inotifywait -e create $SCREENSHOT_FOLDER --format "%f"
}
upload_to_dropbox() {
local upload_path=/Screenshots/$(basename "$1")
set -x
curl "https://content.dropboxapi.com/2/files/upload" \
-H "Authorization: Bearer ${CLIENT_ID}" \
""-H 'Content-Type: application/octet-stream' \
-H "Dropbox-API-Arg: {\"path\":\"$upload_path\"}" \
--data-binary @"$1"
set +x
}
create_link() {
local upload_path=/Screenshots/$(basename "$1")
set -x
curl -X POST https://api.dropboxapi.com/2/sharing/create_shared_link \
--header "Authorization: Bearer ${CLIENT_ID}" \
--header "Content-Type: application/json" \
--data "{\"path\": \"$upload_path\"}" | tee /dev/tty | jq '.url'
set +x
}
dialog() {
zenity --info --text="$1"
}
while true; do
screenshot_file=$(created_file)
sleep 1 # without the sleep we end up with an empty file.... file system not ready?
if [[ "$screenshot_file" =~ "png" ]]; then
upload_to_dropbox "${SCREENSHOT_FOLDER}/${screenshot_file}"
image_url=$(create_link "${SCREENSHOT_FOLDER}/${screenshot_file}")
if [ $? -eq 0 ]; then
dialog "Screenshot upload: $image_url saved to clipboard"
echo $image_url | sed 's/"//g' | xsel --clipboard
else
dialog "Something went wrong $(tail -1 $LOG_FILE)"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment