Skip to content

Instantly share code, notes, and snippets.

@TSedlar
Created August 19, 2014 01:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TSedlar/aee61dc0d164e2b0474a to your computer and use it in GitHub Desktop.
Save TSedlar/aee61dc0d164e2b0474a to your computer and use it in GitHub Desktop.
Bash script for uploading screenshots to imgur or clipboard content to pastie
#!/bin/bash
function get_clipboard_content {
xclip -selection c -o
}
function upload_pastie {
local lang="plain_text"
local content=$(get_clipboard_content)
local private=1
local auth="burger"
url=$(curl -X POST http://pastie.org/pastes \
-F "paste[parser_id]=$lang" \
-F "paste[body]=$content" \
-F "paste[restricted]=$private" \
-F "paste[authorization]=$auth" \
-s -L -o /dev/null -w "%{url_effective}")
echo "$url" | xclip -selection c
nohup xdg-open "$url" > nohup.out 2>&1
}
function upload_imgur {
local client_id="550953eefa6bdbd"
curl -X POST https://api.imgur.com/3/upload \
-H "Authorization: Client-ID $client_id" \
-F "image=@$1"
}
function get_imgur_id {
echo "$1" | grep -o -P '(?<="id":").*?(?=",)'
}
function imgur_process {
local json_output=$(upload_imgur $2)
local imgur_id=$(get_imgur_id $json_output)
local renamed="$1$imgur_id.png"
local url="http://i.imgur.com/$imgur_id.png"
mv $2 $renamed
echo "$url" | xclip -selection c
nohup xdg-open "$url" > nohup.out 2>&1
}
if [ "$1" = "-p" ]; then
upload_pastie
else
tmp_dir="/tmp/clippy/"
tmp_screenshot="${tmp_dir}screenshot.png"
mkdir -p "$tmp_dir"
if [ "$1" = "-s" ]; then
gnome-screenshot -f $tmp_screenshot
elif [ "$1" = "-w" ]; then
gnome-screenshot -w -f $tmp_screenshot
elif [ "$1" = "-a" ]; then
gnome-screenshot -a -f $tmp_screenshot
fi
if [ -f $tmp_screenshot ]; then
imgur_process $tmp_dir $tmp_screenshot
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment