<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Andrew Ferrier &#187; python</title>
	<atom:link href="http://www.andrewferrier.com/blog/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.andrewferrier.com/blog</link>
	<description>Economics; Travel; Film; and Technology.</description>
	<lastBuildDate>Tue, 06 Sep 2011 09:40:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Highrise Backup</title>
		<link>http://www.andrewferrier.com/blog/2009/12/04/highrise-backup/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=highrise-backup</link>
		<comments>http://www.andrewferrier.com/blog/2009/12/04/highrise-backup/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 12:40:22 +0000</pubDate>
		<dc:creator>andrewferrier</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[highrise]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.andrewferrier.com/blog/?p=614</guid>
		<description><![CDATA[I no longer use Highrise, but when I did, I used this Python script to back up my contacts. Hope it&#8217;s helpful to someone. Updated 2009-12-14: Added license #!/usr/bin/python # # Copyright (c) 2009, Andrew Ferrier All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are [...]]]></description>
			<content:encoded><![CDATA[<p>I no longer use Highrise, but when I did, I used this Python script to back up my contacts. Hope it&#8217;s helpful to someone.</p>
<p><strong>Updated 2009-12-14</strong>: Added license</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># Copyright (c) 2009, Andrew Ferrier All rights reserved.</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># Redistribution and use in source and binary forms, with or without</span>
<span style="color: #808080; font-style: italic;"># modification, are permitted provided that the following conditions are met:</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># * Redistributions of source code must retain the above copyright notice,</span>
<span style="color: #808080; font-style: italic;"># this list of conditions and the following disclaimer.</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># * Redistributions in binary form must reproduce the above copyright notice,</span>
<span style="color: #808080; font-style: italic;"># this list of conditions and the following disclaimer in the documentation</span>
<span style="color: #808080; font-style: italic;"># and/or other materials provided with the distribution.</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># * The name of Andrew Ferrier may not be used to endorse or promote products</span>
<span style="color: #808080; font-style: italic;"># derived from this software without specific prior written permission.</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot;</span>
<span style="color: #808080; font-style: italic;"># AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span>
<span style="color: #808080; font-style: italic;"># IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span>
<span style="color: #808080; font-style: italic;"># ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE</span>
<span style="color: #808080; font-style: italic;"># LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span>
<span style="color: #808080; font-style: italic;"># CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span>
<span style="color: #808080; font-style: italic;"># SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span>
<span style="color: #808080; font-style: italic;"># INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN</span>
<span style="color: #808080; font-style: italic;"># CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span>
<span style="color: #808080; font-style: italic;"># ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span>
<span style="color: #808080; font-style: italic;"># POSSIBILITY OF SUCH DAMAGE.</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> httplib2
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">getopt</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
    opts, args = <span style="color: #dc143c;">getopt</span>.<span style="color: #dc143c;">getopt</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span>, <span style="color: #483d8b;">&quot;a:d:&quot;</span>, <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;user-auth-token=&quot;</span>, <span style="color: #483d8b;">&quot;domain=&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #dc143c;">getopt</span>.<span style="color: black;">GetoptError</span>, err:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>err<span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
userauthtoken = <span style="color: #008000;">None</span>
domain = <span style="color: #008000;">None</span>
<span style="color: #ff7700;font-weight:bold;">for</span> o, a <span style="color: #ff7700;font-weight:bold;">in</span> opts:
    <span style="color: #ff7700;font-weight:bold;">if</span> o <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-a&quot;</span>, <span style="color: #483d8b;">&quot;--user-auth-token&quot;</span><span style="color: black;">&#41;</span>:
        userauthtoken = a
    <span style="color: #ff7700;font-weight:bold;">elif</span> o <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-d&quot;</span>, <span style="color: #483d8b;">&quot;--domain&quot;</span><span style="color: black;">&#41;</span>:
        domain = a
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">assert</span> <span style="color: #008000;">False</span>, <span style="color: #483d8b;">&quot;unhandled option&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> userauthtoken == <span style="color: #008000;">None</span> <span style="color: #ff7700;font-weight:bold;">or</span> domain == <span style="color: #008000;">None</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;You must specify both the user-auth-token and the domain&quot;</span>
    <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
&nbsp;
url = <span style="color: #483d8b;">&quot;http://&quot;</span> + domain + <span style="color: #483d8b;">&quot;.highrisehq.com/people.xml&quot;</span>
&nbsp;
http = httplib2.<span style="color: black;">Http</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
http.<span style="color: black;">add_credentials</span><span style="color: black;">&#40;</span>userauthtoken, <span style="color: #483d8b;">&quot;x&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
offset = <span style="color: #ff4500;">0</span>
content = <span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
    response, newContent = http.<span style="color: black;">request</span><span style="color: black;">&#40;</span>url + <span style="color: #483d8b;">&quot;?n=&quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>offset<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> response.<span style="color: black;">status</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">200</span>:
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span>response.<span style="color: black;">status</span><span style="color: black;">&#41;</span>
&nbsp;
    numContacts = newContent.<span style="color: black;">count</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;person&gt;&quot;</span><span style="color: black;">&#41;</span>
    offset += numContacts
    content += newContent
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> numContacts <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">500</span>:
        <span style="color: #ff7700;font-weight:bold;">break</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> content</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.andrewferrier.com/blog/2009/12/04/highrise-backup/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Connecting Google Reader and podget</title>
		<link>http://www.andrewferrier.com/blog/2008/11/23/connecting-google-reader-and-podget/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=connecting-google-reader-and-podget</link>
		<comments>http://www.andrewferrier.com/blog/2008/11/23/connecting-google-reader-and-podget/#comments</comments>
		<pubDate>Sun, 23 Nov 2008 21:08:09 +0000</pubDate>
		<dc:creator>andrewferrier</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[google-reader]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[podcast]]></category>
		<category><![CDATA[podget]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[technical]]></category>

		<guid isPermaLink="false">http://www.andrewferrier.com/blog/?p=542</guid>
		<description><![CDATA[For some time, I&#8217;ve had a Perl script that runs regularly, backing up my Google Reader subscriptions using the standard OPML format: #!/usr/bin/perl # # Usage: # backup-google-reader-opml file-to-write-to.opml google.user.name@domain google-password &#160; use strict; use warnings; &#160; use WWW::Mechanize; &#160; my $mech = WWW::Mechanize-&#62;new&#40;&#41;; &#160; $mech-&#62;get&#40;&#34;http://reader.google.com&#34;&#41; or die &#34;Cannot reach Google Reader Homepage&#34;; &#160; $mech-&#62;submit_form&#40; [...]]]></description>
			<content:encoded><![CDATA[<p>For some time, I&#8217;ve had a Perl script that runs regularly, backing up my Google Reader subscriptions using the standard OPML format:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Usage:</span>
<span style="color: #666666; font-style: italic;">#  backup-google-reader-opml file-to-write-to.opml google.user.name@domain google-password</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> WWW<span style="color: #339933;">::</span><span style="color: #006600;">Mechanize</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$mech</span> <span style="color: #339933;">=</span> WWW<span style="color: #339933;">::</span><span style="color: #006600;">Mechanize</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$mech</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">get</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;http://reader.google.com&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Cannot reach Google Reader Homepage&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$mech</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">submit_form</span><span style="color: #009900;">&#40;</span>
    form_number <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
    fields <span style="color: #339933;">=&gt;</span>
    <span style="color: #009900;">&#123;</span>
        Email <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        Passwd <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Cannot submit form&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$mech</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">get</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;http://www.google.com/reader/subscriptions/export&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$mech</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">save_content</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>However, I recently wrote another script (this time Python) that then takes this OPML, parses out all the URLs that are tagged with &#8216;podcast&#8217;, and outputs a serverlist file for <a href="http://podget.sourceforge.net/">podget</a> (an automated console-based podcast downloader). This enables me to subscribe to a podcast in Google Reader, and have the podcast automatically added to the download list. The script looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># Pass in the OPML file as the first command-line parameter. Will output the</span>
<span style="color: #808080; font-style: italic;"># podget serverlist on stdout.</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">dom</span>.<span style="color: black;">minidom</span>
&nbsp;
doc = <span style="color: #dc143c;">xml</span>.<span style="color: black;">dom</span>.<span style="color: black;">minidom</span>.<span style="color: black;">parse</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
body = doc.<span style="color: black;">getElementsByTagName</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;body&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
&nbsp;
p = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'^<span style="color: #000099; font-weight: bold;">\W</span>+'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> outline <span style="color: #ff7700;font-weight:bold;">in</span> doc.<span style="color: black;">getElementsByTagName</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;outline&quot;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> outline.<span style="color: black;">getAttribute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;text&quot;</span><span style="color: black;">&#41;</span> == <span style="color: #483d8b;">&quot;podcast&quot;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> subOutlines <span style="color: #ff7700;font-weight:bold;">in</span> outline.<span style="color: black;">getElementsByTagName</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;outline&quot;</span><span style="color: black;">&#41;</span>:
            title = subOutlines.<span style="color: black;">getAttribute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;title&quot;</span><span style="color: black;">&#41;</span>
            title = p.<span style="color: black;">sub</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;</span>, title<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">print</span> subOutlines.<span style="color: black;">getAttribute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;xmlUrl&quot;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot; NoCategory &quot;</span> + title</pre></div></div>

<p>Feel free to use and adapt to your needs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andrewferrier.com/blog/2008/11/23/connecting-google-reader-and-podget/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

