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 &#39;Usage: trim.py &lt;url to shorten&gt;&#39;<br>    sys.exit(0)<br><br>user = raw_input(&#39;username: &#39;)<br>passwd = getpass(&#39;password: &#39;)<br>
<br># See <a href="http://tr.im/api">http://tr.im/api</a><br>url = &#39;<a href="http://api.tr.im/api/trim_url.xml?url=%s&amp;newtrim=yes">http://api.tr.im/api/trim_url.xml?url=%s&amp;newtrim=yes</a>&#39; % sys.argv[1]<br>
<br># Warning: --silent don&#39;t show error messages<br>cmd  = &#39;curl --silent --basic -u %s:%s &#39; % (user, passwd)<br>cmd += url<br><br>fd = os.popen(cmd, &#39;r&#39;)<br>response = fd.read()<br>print response<br>
fd.close()<br><br>Doc = xml.dom.minidom.parseString(response)<br>N = Doc.getElementsByTagName(&quot;url&quot;)<br>shortened_url = str((N[0].childNodes)[0].data)<br><br>print &#39;shortened url:&#39;, shortened_url<br><br>
<br>[bsergean@localhost bin]$ python trim.py<br>Usage: trim.py &lt;url to shorten&gt;<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>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;trim&gt;<br>  &lt;status result=&quot;OK&quot; code=&quot;200&quot; message=&quot;<a href="http://tr.im">tr.im</a> URL Added.&quot;/&gt;<br>
  &lt;url&gt;<a href="http://tr.im/hglC">http://tr.im/hglC</a>&lt;/url&gt;<br>  &lt;reference&gt;ZfCAq7qcyouYXwfR2WsuBR6v3V4q7L&lt;/reference&gt;<br>  &lt;trimpath&gt;hglC&lt;/trimpath&gt;<br>&lt;/trim&gt;<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">&lt;<a href="mailto:jjinux@gmail.com">jjinux@gmail.com</a>&gt;</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 &lt;<a href="mailto:daryl.spitzer@gmail.com">daryl.spitzer@gmail.com</a>&gt; wrote:<br>
&gt; I&#39;m playing around, trying to write some code to use the <a href="http://tr.im" target="_blank">http://tr.im</a><br>
&gt; APIs (<a href="http://tr.im/api/" target="_blank">http://tr.im/api/</a>) to shorten a URL.<br>
&gt;<br>
&gt; After reading <a href="http://docs.python.org/library/urllib2.html" target="_blank">http://docs.python.org/library/urllib2.html</a>, I tried:<br>
&gt;<br>
&gt;    TRIM_API_URL = &#39;<a href="http://api.tr.im/api" target="_blank">http://api.tr.im/api</a>&#39;<br>
&gt;    auth_handler = urllib2.HTTPBasicAuthHandler()<br>
&gt;    auth_handler.add_password(realm=&#39;<a href="http://tr.im" target="_blank">tr.im</a>&#39;,<br>
&gt;                              uri=TRIM_API_URL,<br>
&gt;                              user=USERNAME,<br>
&gt;                              passwd=PASSWORD)<br>
&gt;    opener = urllib2.build_opener(auth_handler)<br>
&gt;    urllib2.install_opener(opener)<br>
&gt;    response = urllib2.urlopen(&#39;%s/trim_simple?url=%s&#39;<br>
&gt;                               % (TRIM_API_URL, url_to_trim))<br>
&gt;    url = response.read().strip()<br>
&gt;<br>
&gt; response.code is 200 (I think it should be 202).  url is valid, but<br>
&gt; the basic HTTP authentication doesn&#39;t seem to have worked, because the<br>
&gt; shortened URL isn&#39;t in my list of URLs (at <a href="http://tr.im/?page=1" target="_blank">http://tr.im/?page=1</a>).<br>
&gt;<br>
&gt; 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>
&gt; I also tried:<br>
&gt;<br>
&gt;    TRIM_API_URL = &#39;<a href="http://api.tr.im/api" target="_blank">api.tr.im/api</a>&#39;<br>
&gt;    password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()<br>
&gt;    password_mgr.add_password(None, TRIM_API_URL, USERNAME, PASSWORD)<br>
&gt;    auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)<br>
&gt;    opener = urllib2.build_opener(auth_handler)<br>
&gt;    urllib2.install_opener(opener)<br>
&gt;    response = urllib2.urlopen(&#39;http://%s/trim_simple?url=%s&#39;<br>
&gt;                               % (TRIM_API_URL, url_to_trim))<br>
&gt;    url = response.read().strip()<br>
&gt;<br>
&gt; But I get the same results.  (response.code is 200 and url is valid,<br>
&gt; but not recorded in my account at <a href="http://tr.im/" target="_blank">http://tr.im/</a>.)<br>
&gt;<br>
&gt; If I use query string parameters instead of basic HTTP authentication,<br>
&gt; like this:<br>
&gt;<br>
&gt;    TRIM_API_URL = &#39;<a href="http://api.tr.im/api" target="_blank">http://api.tr.im/api</a>&#39;<br>
&gt;    response = urllib2.urlopen(&#39;%s/trim_simple?url=%s&amp;username=%s&amp;password=%s&#39;<br>
&gt;                               % (TRIM_API_URL,<br>
&gt;                                  url_to_trim,<br>
&gt;                                  USERNAME,<br>
&gt;                                  PASSWORD))<br>
&gt;    url = response.read().strip()<br>
&gt;<br>
&gt; ...then not only is url valid but it&#39;s recorded in my <a href="http://tr.im" target="_blank">tr.im</a> account.<br>
&gt; (Though response.code is still 200.)<br>
&gt;<br>
&gt; Either there must be something wrong in my authentication code, or in<br>
&gt; <a href="http://tr.im" target="_blank">tr.im</a>&#39;s.  (I&#39;ve written <a href="mailto:api@tr.im">api@tr.im</a> to see if it&#39;s a known problem and<br>
&gt; haven&#39;t received a response.)<br>
&gt;<br>
&gt; Can anyone spot any problems?<br>
&gt;<br>
&gt; --<br>
&gt; Daryl<br>
&gt; _______________________________________________<br>
&gt; Baypiggies mailing list<br>
&gt; <a href="mailto:Baypiggies@python.org">Baypiggies@python.org</a><br>
&gt; To change your subscription options or unsubscribe:<br>
&gt; <a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
&gt;<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>