Friday, March 6, 2009

A TextExpander snippet that works for me

At the end of A TextExpander snippet to paste quoted text I gave myself a to-do to create (or find) a TextExpander snippet to create a shortened URL. is currently my favorite URL shortening service. Not only is the domain short and easy to remember, but (if you sign up for an account) they provide stats for each “tr.immed” URL.

A Google search found this post on the SmileOnMyMac Blog (from the makers of TextExpander), where you can download a snippet that works. But it’s unnecessarily complicated. After reading the API documentation I simplified it to this shell script:
#! /bin/bash
curl -u yacitus:xxxxx`pbpaste`
Note that Blogger refuses to make this column any wider or use a scrollbar to show all of this code. But the text is there; just copy it and paste into your favorite text editor. (You'll need to use the same trick to see all of the code for the other bash one-liner and the Python code below.)

(You’ll of course want to replace “yacitus” with your username and replace “xxxxx” with your password.)

The problem with this is that it doesn’t work for me at work, where I use the Authoxy proxy server. I can make it work with:
#! /bin/bash
curl -u yacitus:xxxxx -x localhost:8080`pbpaste`
…but then it doesn’t work when I’m at home (and not using Authoxy). curl, unfortunately, doesn’t auto-detect proxy settings.

Once again, Python comes to the rescue. I read in “Fuzzyman’s” urllib2 - The Missing Manual that the Python urllib2 module will auto-detect proxy settings, so I wrote this script:
#!/usr/bin/env python
This script writes to stdout a version of the indicated URL.


import urllib2
import sys

USERNAME = 'yacitus'
PASSWORD = 'xxxxx'

def main():
"""The entry function."""
url_to_trim = sys.argv[1]
if not url_to_trim:
print "ERROR: one (and only one) argument accepted--the URL to GET"
return -1

response = urllib2.urlopen('%s/trim_simple?url=%s&username=%s&password=%s'

print response.code

return 0

if __name__ == '__main__':
Like in A TextExpander snippet to paste quoted text, I put this in a file called trim_url, did a chmod +x on it, created a symbolic link to it in /usr/local/bin/, and created my TextExpander snippet:

I could have saved myself a lot of time if I had stopped there. But I read on that basic HTTP authentication is preferred over the query string parameters I used above. So I figured it would be a learning opportunity to implement basic HTTP authentication in Python. The problem is, I’m not done learning yet! I have yet to get it to work. I posted my question on the BayPIGgies mailing list where I got some good advice on how to debug it (but no one saw the problem), and I also posted a question on where I got one answer that may be an improvement on the query string parameters, but again no one saw the problem. I guess I’ll have to take jj’s suggestion and look at what is being sent over the wire. When I figure it out I’ll post the answer on my PyPap blog (and of course on the BayPIGgies mailing list and

No comments: