<?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>Brian Lyttle &#187; twitter</title>
	<atom:link href="http://brianlyttle.com/tag/twitter/feed/" rel="self" type="application/rss+xml" />
	<link>http://brianlyttle.com</link>
	<description>What happens when the code just stops</description>
	<lastBuildDate>Sun, 09 Oct 2011 04:30:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Twitter to blog script</title>
		<link>http://brianlyttle.com/2009/05/twitter-to-blog-script/</link>
		<comments>http://brianlyttle.com/2009/05/twitter-to-blog-script/#comments</comments>
		<pubDate>Sun, 24 May 2009 17:37:58 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://brianlyttle.com/?p=147</guid>
		<description><![CDATA[Based on an example provided with the Twitter library for Python I cobbled together the following script to add my latest tweets to this site. It&#8217;s called from a cron job that I run on an occasional basis. My script linkifies hashtags and @username tokens in tweets so that you can see search results or [...]]]></description>
			<content:encoded><![CDATA[<p></p><p><img src="/wp-content/uploads/2009/05/twitter_logo_header.png" alt="Twitter logo" class="imgright" />Based on <a href="http://code.google.com/p/python-twitter/source/browse/trunk/examples/twitter-to-xhtml.py">an example</a> provided with the <a href="http://code.google.com/p/python-twitter/">Twitter library for Python</a> I cobbled together the following script to add my latest tweets to this site. It&#8217;s called from a cron job that I run on an occasional basis. My script linkifies hashtags and @username tokens in tweets so that you can see search results or user information.</p>
<p>Why did I not use one of the WordPress widgets? Well writing scripts like this is fun, and some widgets don&#8217;t seem to play too well with my Thesis theme. One thing to note is that getting the shell script setup under some cron configurations can take a while if you aren&#8217;t using it on a regular basis. It&#8217;s operation is also different between Ubuntu Server and Joyent&#8217;s Accelerator platform.</p>
<p>Up next: a similar script to process my latest bookmarks on <a href="http://delicious.com/brianly">Delicious.com</a>.</p>
<p><strong>Main script (tweets.py)</strong></p>
<pre>
#!/usr/bin/python

import codecs, re, getopt, sys, twitter

TEMPLATE = &quot;&quot;&quot;
&lt;li&gt;
  &lt;span class=&quot;twitter-text&quot;&gt;%s&lt;/span&gt;
  &lt;span class=&quot;twitter-relative-created-at&quot;&gt;&lt;a href=&quot;http://twitter.com/%s/statuses/%s&quot;&gt;Posted %s&lt;/a&gt;&lt;/span&gt;
&lt;/li&gt;
&quot;&quot;&quot;

def Usage():
  print 'Usage: %s [options] twitterid' % __file__
  print
  print '  This script fetches a users latest twitter update and stores'
  print '  the result in a file as an XHTML fragment'
  print
  print '  Options:'
  print '    --help -h : print this help'
  print '    --output : the output file [default: stdout]'

def FetchTwitter(user, output):
  assert user
  statuses = twitter.Api().GetUserTimeline(user=user, count=7)

  xhtml = []
  for status in statuses:
  	  status.text = Linkify(status.text)
  	  xhtml.append(TEMPLATE % (status.text, status.user.screen_name, status.id, status.relative_created_at))

  if output:
    Save(''.join(xhtml), output)
  else:
    print ''.join(xhtml)

def Linkify(tweet):
    tweet = re.sub(r'(\A|\s)@(\w+)', r'\1@<a href="http://www.twitter.com/\2">\2</a>', tweet)
    return re.sub(r'(\A|\s)#(\w+)', r'\1#<a href="http://search.twitter.com/search?q=%23\2">\2</a>', tweet)

def Save(xhtml, output):
  out = codecs.open(output, mode='w', encoding='utf-8',
                    errors='xmlcharrefreplace')
  out.write(xhtml)
  out.close()

def main():
  try:
    opts, args = getopt.gnu_getopt(sys.argv[1:], 'ho', ['help', 'output='])
  except getopt.GetoptError:
    Usage()
    sys.exit(2)
  try:
    user = args[0]
  except:
    Usage()
    sys.exit(2)
  output = None
  for o, a in opts:
    if o in ("-h", "--help"):
      Usage()
      sys.exit(2)
    if o in ("-o", "--output"):
      output = a
  FetchTwitter(user, output)

if __name__ == "__main__":
  main()
</pre>
<p><strong>Shell script executed as cron job (tweets.sh)</strong></p>
<pre>
/usr/bin/python /path/to/tweets.py brianly --output /path/to/output/twittertimeline.htm
</pre>
]]></content:encoded>
			<wfw:commentRss>http://brianlyttle.com/2009/05/twitter-to-blog-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

