Warning: quick and dirty solution with no error checking using curl and a unix pipe.<br><br>- Benjamin<br><br>[bsergean@localhost bin]$ cat trim.py<br>#!/usr/bin/env python<br><br>import os, sys<br>from getpass import getpass<br>
import xml.dom.minidom<br><br>if len(sys.argv) != 2:<br> print 'Usage: trim.py <url to shorten>'<br> sys.exit(0)<br><br>user = raw_input('username: ')<br>passwd = getpass('password: ')<br>
<br># See <a href="http://tr.im/api">http://tr.im/api</a><br>url = '<a href="http://api.tr.im/api/trim_url.xml?url=%s&newtrim=yes">http://api.tr.im/api/trim_url.xml?url=%s&newtrim=yes</a>' % sys.argv[1]<br>
<br># Warning: --silent don't show error messages<br>cmd = 'curl --silent --basic -u %s:%s ' % (user, passwd)<br>cmd += url<br><br>fd = os.popen(cmd, 'r')<br>response = fd.read()<br>print response<br>
fd.close()<br><br>Doc = xml.dom.minidom.parseString(response)<br>N = Doc.getElementsByTagName("url")<br>shortened_url = str((N[0].childNodes)[0].data)<br><br>print 'shortened url:', shortened_url<br><br>
<br>[bsergean@localhost bin]$ python trim.py<br>Usage: trim.py <url to shorten><br>[bsergean@localhost bin]$ python trim.py <a href="http://www.reddit.com/r/programming/">http://www.reddit.com/r/programming/</a><br>
username: bsergean<br>password:<br><?xml version="1.0" encoding="UTF-8"?><br><trim><br> <status result="OK" code="200" message="<a href="http://tr.im">tr.im</a> URL Added."/><br>
<url><a href="http://tr.im/hglC">http://tr.im/hglC</a></url><br> <reference>ZfCAq7qcyouYXwfR2WsuBR6v3V4q7L</reference><br> <trimpath>hglC</trimpath><br></trim><br><br>shortened url: <a href="http://tr.im/hglC">http://tr.im/hglC</a><br>
<br><br><br><br><br><br><div class="gmail_quote">On Tue, Mar 10, 2009 at 11:51 PM, Shannon -jj Behrens <span dir="ltr"><<a href="mailto:jjinux@gmail.com">jjinux@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
It may help to look at what is being sent over the wire.<br>
<br>
-jj<br>
<div><div></div><div class="h5"><br>
On Tue, Mar 10, 2009 at 8:26 AM, Daryl Spitzer <<a href="mailto:daryl.spitzer@gmail.com">daryl.spitzer@gmail.com</a>> wrote:<br>
> I'm playing around, trying to write some code to use the <a href="http://tr.im" target="_blank">http://tr.im</a><br>
> APIs (<a href="http://tr.im/api/" target="_blank">http://tr.im/api/</a>) to shorten a URL.<br>
><br>
> After reading <a href="http://docs.python.org/library/urllib2.html" target="_blank">http://docs.python.org/library/urllib2.html</a>, I tried:<br>
><br>
> TRIM_API_URL = '<a href="http://api.tr.im/api" target="_blank">http://api.tr.im/api</a>'<br>
> auth_handler = urllib2.HTTPBasicAuthHandler()<br>
> auth_handler.add_password(realm='<a href="http://tr.im" target="_blank">tr.im</a>',<br>
> uri=TRIM_API_URL,<br>
> user=USERNAME,<br>
> passwd=PASSWORD)<br>
> opener = urllib2.build_opener(auth_handler)<br>
> urllib2.install_opener(opener)<br>
> response = urllib2.urlopen('%s/trim_simple?url=%s'<br>
> % (TRIM_API_URL, url_to_trim))<br>
> url = response.read().strip()<br>
><br>
> response.code is 200 (I think it should be 202). url is valid, but<br>
> the basic HTTP authentication doesn't seem to have worked, because the<br>
> shortened URL isn't in my list of URLs (at <a href="http://tr.im/?page=1" target="_blank">http://tr.im/?page=1</a>).<br>
><br>
> After reading <a href="http://www.voidspace.org.uk/python/articles/authentication.shtml#doing-it-properly" target="_blank">http://www.voidspace.org.uk/python/articles/authentication.shtml#doing-it-properly</a><br>
> I also tried:<br>
><br>
> TRIM_API_URL = '<a href="http://api.tr.im/api" target="_blank">api.tr.im/api</a>'<br>
> password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()<br>
> password_mgr.add_password(None, TRIM_API_URL, USERNAME, PASSWORD)<br>
> auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)<br>
> opener = urllib2.build_opener(auth_handler)<br>
> urllib2.install_opener(opener)<br>
> response = urllib2.urlopen('http://%s/trim_simple?url=%s'<br>
> % (TRIM_API_URL, url_to_trim))<br>
> url = response.read().strip()<br>
><br>
> But I get the same results. (response.code is 200 and url is valid,<br>
> but not recorded in my account at <a href="http://tr.im/" target="_blank">http://tr.im/</a>.)<br>
><br>
> If I use query string parameters instead of basic HTTP authentication,<br>
> like this:<br>
><br>
> TRIM_API_URL = '<a href="http://api.tr.im/api" target="_blank">http://api.tr.im/api</a>'<br>
> response = urllib2.urlopen('%s/trim_simple?url=%s&username=%s&password=%s'<br>
> % (TRIM_API_URL,<br>
> url_to_trim,<br>
> USERNAME,<br>
> PASSWORD))<br>
> url = response.read().strip()<br>
><br>
> ...then not only is url valid but it's recorded in my <a href="http://tr.im" target="_blank">tr.im</a> account.<br>
> (Though response.code is still 200.)<br>
><br>
> Either there must be something wrong in my authentication code, or in<br>
> <a href="http://tr.im" target="_blank">tr.im</a>'s. (I've written <a href="mailto:api@tr.im">api@tr.im</a> to see if it's a known problem and<br>
> haven't received a response.)<br>
><br>
> Can anyone spot any problems?<br>
><br>
> --<br>
> Daryl<br>
> _______________________________________________<br>
> Baypiggies mailing list<br>
> <a href="mailto:Baypiggies@python.org">Baypiggies@python.org</a><br>
> To change your subscription options or unsubscribe:<br>
> <a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
><br>
<br>
<br>
<br>
</div></div><font color="#888888">--<br>
In this life we cannot do great things. We can only do small things<br>
with great love. -- Mother Teresa<br>
<a href="http://jjinux.blogspot.com/" target="_blank">http://jjinux.blogspot.com/</a><br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Baypiggies mailing list<br>
<a href="mailto:Baypiggies@python.org">Baypiggies@python.org</a><br>
To change your subscription options or unsubscribe:<br>
<a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
</div></div></blockquote></div><br>