Screenie version 2.0: Now with bit.ly URL shortening
I had a horrible wake-up this morning. I had the weirdest imaginable dreams, dreams which in hindsight probably signboard my sexual deviances like a rest stop billboard by a ten-lane freeway. Burning cake in the oven? Check.
After the obligatory ragged-breathing and recrimination periods, I turned back to last night and a more productive reflection upon my Dropbox hack. How can I improve this? I asked myself.
Big URL is $BIGURL
URL length. As it stands, a URL from the script tips the scale at a weighty 63 characters:
I want to shorten the link, and to to that just like all the other kids in the playground, I need to use a link shortening service; I picked bit.ly because it is the service which I already use for Twitter; in Twitterfeed distribution; on Facebook; and for Dropbox referral link spam. So. My goals for today are:
- Grab the output URL and, instead of piping it to your clipboard, instead turn it into a variable (I hate long, run-on lines in a script).
- Take this new, piping-hot variable…and pipe it into bit.ly.
- Copy the resultant short URL bit.ly returns to me and put that into your clipboard.
My first step in changing the script is to go back and redirect the output from screenie from your clipboard and into another pipe. Look at this line from the last script:
echo "$URL1/$DBUSER/$URL2/$LAST" | xclip -sel clip
The first part of the pipe (
echo blah blah blah) takes the four separate parts of the
final URL and strings them together into one big URL, a big URL which henceforth shall be creatively called the
$BIGURL variable. The second part of the pipe,
xclip -sel clip takes this wrought
$BIGURL and puts all sixty-three characters into your clipboard for instant pasting. Rather than piping into
xclip I change it around a little:
echo "$URL1/$DBUSER/$URL2/$JPG" > $TMP
A temp file? Oh, Mark. -_- I know, I know. My level of skill with the Bash shell is almost nonexistent. So what do those three lines mean to the lay user? I create a temporary file in your home folder and echo the final URL into that folder. I know that there are many other ways to achieve this end; my research on Google turned up different methods that are simpler or more complex, but what I felt worked best for me was a URL sitting in a file that I could manipulate at will. I drop
$JPG (changed from
$LAST in the version 1.0 script) into my temp file and use a reading of the contents of the temp file to use as $BIGURL.
To test if this works we change the last line of the script to an echo:
[mark][~] # ./screenie
(If you squint real good you can see all my failed attempts at parsing)
$BIGURL wants to be a $SMALLURL
Whee, it works. Pull the echo line. My next step is to pipe
$BIGURL into the bit.ly shortening service and get a nice, usable
$SMALLURL for pasting into instant message conversations, Facebook status updates and tweets. And other such sundry websites.
First stop is Google. I found some good references to bit.ly scripts for Ruby, PHP, Perl and, eventually, Bash (kinda). None of the samples I encountered really covered what I wanted to achieve, so I next turned to the bit.ly API documentation on Google Code. What I learned is:
- bit.ly uses JSON or XML for handling requests.
- I need three pieces of information: My bit.ly login, my bit.ly API key and the URL to be shortened.
sed me, up, sed me down
I already have all three. If, going forward, you want to use this version of the script with bit.ly shortening, you will need your own bit.ly account. Now, down to work: What I first notice is that bit.ly requires the URL to be percent encoded and then submitted in the form of an appropriately-formatted URL. So…
…needs to become, approximately…
…before I can submit it to bit.ly for shortening. Time for
sed absolutely terrifies me, as a program. When I was first learning Linux I once zeroed a few directories worth of files by a misplaced
sed. Childhood trauma, I has it.
With a full bladder, dry mouth and pounding heart I took the temp URL file from
screenie and inserted it into a small test script to parse
echo "In: $(cat ./longurl)"
cat longurl | sed 's,:,%3A,g;s,/,%2F,g' > shorturl
echo "Out: $(cat ./shorturl)"
[mark][~] # ./parse
Next step is to further parse the URL correctly for submission to bit.ly. To do that I need two more variables:
^So that gave me this from bit.ly:
200 OK http://bit.ly/dBM1Wc dBM1Wc abEE9u http://dl.dropbox.com/u/4144919/screenshots/20100703103754.jpg 0
What that line contains is the shortened bit.ly URL, http://bit.ly/dBM1Wc, although in this instance I let the script parse it and then I input it into a browser by hand.
I can now take a screenshot, send it to Dropbox, have it upload to the web, give me a working
$BIGURL and parse it twice: The first time to percent encode it and the second time to create a bit.ly-compatible API request. Yay. I don’t want to put up the separate bit.ly code in this post because I plan to modify it slightly and make it fully pipable. Something like this:
echo "http://www.google.com" | bitly >> shorturl.txt
Bits of bit.ly, OR: cURL up with me
My next step in this odyssey is to take the a-OK bit.ly request URL and actually send it to bit.ly. For that I need cURL, a commandline program intended for just this role. This brings me up to three external dependencies, which is fair going.
I take my big freaking URL and do this with it:
[mark][~] # curl -s -S http://big-url-for-bitly
Does the abrupt lack of inane detail betray my weariness? All of the above is really what I’ve done. I’ve made some general tidying up besides, so as of now I hit print screen and have a sharable-bit.ly URL in my hand (clipboard) about ten seconds later. Below is the download link for screenie version 2.0. You need to input (again) your Dropbox UID and now also your bit.ly login and API key. Instructions are enclosed in the script:
Categorised as: regular