#! /bin/bash
curl -u yacitus:xxxxx http://api.tr.im/api/trim_simple?url=`pbpaste`
(You’ll of course want to replace “yacitus” with your tr.im 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 http://api.tr.im/api/trim_simple?url=`pbpaste`
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 tr.im version of the indicated URL.
"""
import urllib2
import sys
TRIM_API_URL = 'http://api.tr.im/api'
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'
% (TRIM_API_URL,
url_to_trim,
USERNAME,
PASSWORD))
print response.read().strip()
print response.code
return 0
if __name__ == '__main__':
sys.exit(main())
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 http://tr.im/api 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 stackoverflow.com 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 stackoverflow.com).
No comments:
Post a Comment