Although I use it all the time it turns I haven't written here about Adjix. There is one mention in some issues but nothing that spells out just how awesome it is. The short version is Adjix is hands down the single best URL shortener out there. While it deserves it's own post suffice it to say you can use its power and still retain a copy of your links on your own S3 drive. Future proofing at its best.
But this post isn't about that. It's about using your own custom domain and setting up a URL shortener that works from Alfred. Alfred is itself another awesome tool that deserves yet another post. Inspired by a post on Dirt Don I set out to create a url shortening shortcut. (Recursion is encouraged).
So based on this post I setup a quick shell script shortcut in Alfred. The properties end up looking something like this:
Title: Shorten URL
Description: Shorten a URL and copy to clipboard
Keyword: sl
curl "http://api.adjix.com/shrinkLink?url={query}&partnerID=xxxxxMyPartnerIDxxxxxx&tokenOnly=yes" | awk 'BEGIN { FS = "!" }; { print "http://shorturl.me/" $1 }' | tr -d '\n' | pbcopy
This works as expected and after typing "sl http://example.com" into the Alfred window a short link is copied to the clipboard. And all is well with the world. Almost.
It's almost well with the world because it could be better. It turns out I already have a database of short links. Sure there's the filesystem on my S3 storage but I have a more immediately accessible database. It is the same database that powers this website. And that database has a collection of URLs that I've shortened over time and serves as another sort of record. Wouldn't it be nice to connect the two?
The Shorten module is the powerhouse of Drupal and URL shortening. If you use Drupal and you need to make short URLs for any reason it should be your first choice. So I thought well I can create a callback for Shorten that provides a simple web service ala the other redirection tools. And certainly I could do that. But that's when it hit me that I already have the tools I needed and the script would be a short one. A little Drush is all that's needed.
My desktop is already setup with drush and a site alias for my site. With this knowledge the above code became:
drush @joshuabrauer.prod ev "print (shorten_url('{query}'));" | pbcopy
There are several advantages including that it gets stored in my database. In addition I can customize the rules for how the shortener works on my site should I need to and there's no need to hard-code the shortener domain in the script.
Of course nothing about these scripts is particularly unique to Alfred. They would work just as well as shell scripts on pretty much any operating system. Soon I'll write more about how I am using Drupal more and more through tools other than web browsers.