Screenie version 2.0: Now with bit.ly URL shortening

by Mark on

#!/bin/bash

check_dependencies() {
    # Takes screenshot.
    dep[1]="scrot"
    # Parses the URL.
    dep[2]="curl"
    # Preferred method to store files.
    dep[3]="dropbox"
    # Copies content to the clipboard.
    dep[4]="xclip" 

    for n in $(seq ${#dep[@]}); do
        command -v ${dep[$n]} > /dev/null 2>&1

        if [ $? == 1 ]; then
            echo "ERROR: Command '${dep[$n]}' not found"
            dep_missing=1
        fi
    done

    if [[ $dep_missing == 1 ]]; then
        exit 1
    fi
}

encode_url() {
    # Replace colons, slashes and periods.
    echo $1 | sed 's,:,%3A,g;s,/,%2F,g;s,\.,%2E,g'
}

bit_url() {
    # Parse the URL with bit.ly's old API.
    log="login=$1"
    key="apiKey=$2"
    url="longUrl=$(encode_url $3)"
    fmt="format=txt"

    curl -S -s "http://api.bit.ly/v3/shorten?$log&$key&$url&$fmt"
}

timestamp="$(date +%Y%m%d%H%M%S)"
bit_key="ABC123"
bit_user="ABC123"
db_user="1234567"
db_pub="$HOME/Dropbox/Public/screenshots"

if [[ ! -d "$db_pub" ]]; then
    mkdir -p "$db_pub"
fi

cd "$db_pub"
scrot -d 0 -q 100 $timestamp.jpg
bit_url $bit_user $bit_key $(dropbox puburl $(ls -1t . | head -n 1)) \
| xclip -sel clipboard

exit 0

Tagged: , , , , , , , , , , , , ,

Say something

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>