<?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>Terry Jones &#187; twitter</title>
	<atom:link href="http://blogs.fluidinfo.com/terry/tag/twitter/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.fluidinfo.com/terry</link>
	<description>Random thoughts on tech, books, programming, etc.</description>
	<lastBuildDate>Sat, 21 Jan 2012 06:18:14 +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>Python code for retrieving all your tweets</title>
		<link>http://blogs.fluidinfo.com/terry/2009/06/24/python-code-for-retrieving-all-your-tweets/</link>
		<comments>http://blogs.fluidinfo.com/terry/2009/06/24/python-code-for-retrieving-all-your-tweets/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 20:44:06 +0000</pubDate>
		<dc:creator>terry</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://blogs.fluidinfo.com/terry/?p=608</guid>
		<description><![CDATA[Here&#8217;s a little Python code to pull back all a user&#8217;s Twitter tweets. Make sure you read the notes at bottom in case you want to use it. import sys, twitter, operator from dateutil.parser import parse twitterURL = &#8216;http://twitter.com&#8217; def fetch&#40;user&#41;: &#160; &#160; data = &#123;&#125; &#160; &#160; api = twitter.Api&#40;&#41; &#160; &#160; max_id = [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a little Python code to pull back all a user&#8217;s Twitter tweets. Make sure you read the notes at bottom in case you want to use it.</p>
<div class="dean_ch" style="white-space: nowrap;">
<span class="kw1">import</span> <span class="kw3">sys</span>, twitter, <span class="kw3">operator</span><br />
<span class="kw1">from</span> dateutil.<span class="kw3">parser</span> <span class="kw1">import</span> parse</p>
<p>twitterURL = <span class="st0">&#8216;http://twitter.com&#8217;</span></p>
<p><span class="kw1">def</span> fetch<span class="br0">&#40;</span><span class="kw3">user</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; data = <span class="br0">&#123;</span><span class="br0">&#125;</span><br />
&nbsp; &nbsp; api = twitter.<span class="me1">Api</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; max_id = <span class="kw2">None</span><br />
&nbsp; &nbsp; total = <span class="nu0">0</span><br />
&nbsp; &nbsp; <span class="kw1">while</span> <span class="kw2">True</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; statuses = api.<span class="me1">GetUserTimeline</span><span class="br0">&#40;</span><span class="kw3">user</span>, count=<span class="nu0">200</span>, max_id=max_id<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; newCount = ignCount = <span class="nu0">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> s <span class="kw1">in</span> statuses:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> s.<span class="kw2">id</span> <span class="kw1">in</span> data:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ignCount += <span class="nu0">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data<span class="br0">&#91;</span>s.<span class="kw2">id</span><span class="br0">&#93;</span> = s<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newCount += <span class="nu0">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; total += newCount<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> &gt;&gt;sys.<span class="me1">stderr</span>, <span class="st0">&quot;Fetched %d/%d/%d new/old/total.&quot;</span> % <span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newCount, ignCount, total<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> newCount == <span class="nu0">0</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; max_id = <span class="kw2">min</span><span class="br0">&#40;</span><span class="br0">&#91;</span>s.<span class="kw2">id</span> <span class="kw1">for</span> s <span class="kw1">in</span> statuses<span class="br0">&#93;</span><span class="br0">&#41;</span> &#8211; <span class="nu0">1</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> data.<span class="me1">values</span><span class="br0">&#40;</span><span class="br0">&#41;</span></p>
<p><span class="kw1">def</span> htmlPrint<span class="br0">&#40;</span><span class="kw3">user</span>, tweets<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; <span class="kw1">for</span> t <span class="kw1">in</span> tweets:<br />
&nbsp; &nbsp; &nbsp; &nbsp; t.<span class="me1">pdate</span> = parse<span class="br0">&#40;</span>t.<span class="me1">created_at</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; key = <span class="kw3">operator</span>.<span class="me1">attrgetter</span><span class="br0">&#40;</span><span class="st0">&#8216;pdate&#8217;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; tweets = <span class="kw2">sorted</span><span class="br0">&#40;</span>tweets, key=key<span class="br0">&#41;</span><br />
&nbsp; &nbsp; f = <span class="kw2">open</span><span class="br0">&#40;</span><span class="st0">&#8216;%s.html&#8217;</span> % <span class="kw3">user</span>, <span class="st0">&#8216;wb&#8217;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">print</span> &gt;&gt;f, <span class="st0">&quot;&quot;</span><span class="st0">&quot;&lt;html&gt;&lt;title&gt;Tweets for %s&lt;/title&gt;<br />
&nbsp; &nbsp; &lt;meta http-equiv=&quot;</span>Content-Type<span class="st0">&quot; content=&quot;</span>text/html;charset=utf<span class="nu0">-8</span><span class="st0">&quot;&gt;<br />
&nbsp; &nbsp; &lt;body&gt;&lt;small&gt;&quot;</span><span class="st0">&quot;&quot;</span> % <span class="kw3">user</span><br />
&nbsp; &nbsp; <span class="kw1">for</span> i, t <span class="kw1">in</span> <span class="kw2">enumerate</span><span class="br0">&#40;</span>tweets<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> &gt;&gt;f, <span class="st0">&#8216;%d. %s &lt;a href=&quot;%s/%s/status/%d&quot;&gt;%s&lt;/a&gt;&lt;br/&gt;&#8217;</span> % <span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i, t.<span class="me1">pdate</span>.<span class="me1">strftime</span><span class="br0">&#40;</span><span class="st0">&#8216;%Y-%m-%d %H:%M&#8217;</span><span class="br0">&#41;</span>, twitterURL,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">user</span>, t.<span class="kw2">id</span>, t.<span class="me1">text</span>.<span class="me1">encode</span><span class="br0">&#40;</span><span class="st0">&#8216;utf8&#8242;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">print</span> &gt;&gt;f, <span class="st0">&#8216;&lt;/small&gt;&lt;/body&gt;&lt;/html&gt;&#8217;</span><br />
&nbsp; &nbsp; f.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <br />
<span class="kw1">if</span> __name__ == <span class="st0">&#8216;__main__&#8217;</span>:<br />
&nbsp; &nbsp; <span class="kw3">user</span> = <span class="st0">&#8216;terrycojones&#8217;</span> <span class="kw1">if</span> <span class="kw2">len</span><span class="br0">&#40;</span><span class="kw3">sys</span>.<span class="me1">argv</span><span class="br0">&#41;</span> &lt; <span class="nu0">2</span> <span class="kw1">else</span> <span class="kw3">sys</span>.<span class="me1">argv</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; data = fetch<span class="br0">&#40;</span><span class="kw3">user</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; htmlPrint<span class="br0">&#40;</span><span class="kw3">user</span>, data<span class="br0">&#41;</span><br />
&nbsp;</div>
<p><strong>Notes:</strong></p>
<p>Fetch all of a user&#8217;s tweets and write them to a file <tt>username.html</tt> (where username is given on the command line).</p>
<p>Output is to a file instead of to stdout as tweet texts are unicode and <tt>sys.stdout.encoding</tt> is <tt>ascii</tt> on my machine, which prevents printing non-ASCII chars.</p>
<p>This code uses the Python-Twitter library. You need to get (via SVN) <a href="http://python-twitter.googlecode.com/svn/trunk">the very latest version</a>, and then you need to fix a tiny bug, <a href="http://groups.google.com/group/python-twitter/msg/6ba85d52d36be02e">described here</a>.  Or wait a while and the SVN trunk will be patched.</p>
<p>This worked flawlessly for <a href="http://jon.es/twitter/terrycojones.html">my 2,300 tweets</a>, but only retrieved about half the tweets of someone who had over 7,000. I&#8217;m not sure what happened there.</p>
<p>There are tons of things that could be done to make the output more attractive and useful. And yes, for nitpickers, the code has a couple of slight inefficiencies :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.fluidinfo.com/terry/2009/06/24/python-code-for-retrieving-all-your-tweets/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

