<?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>Our Geek Space &#187; twitter</title>
	<atom:link href="http://blog.moove-it.com/category/twitter/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.moove-it.com</link>
	<description>be free to express yourself...</description>
	<lastBuildDate>Fri, 23 Jul 2010 15:35:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Interfacing with Twitter</title>
		<link>http://blog.moove-it.com/interfacing-with-twitter/</link>
		<comments>http://blog.moove-it.com/interfacing-with-twitter/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 15:30:42 +0000</pubDate>
		<dc:creator>Gian Zas</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[web 2.0]]></category>
		<category><![CDATA[interfacing twitter]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[rest api]]></category>
		<category><![CDATA[restful]]></category>
		<category><![CDATA[twitter python rest]]></category>

		<guid isPermaLink="false">http://blog.moove-it.com/?p=116</guid>
		<description><![CDATA[Tweet tweet tweet! All the farm is chating about Twitter, all the farm is using twitter and all the farm is building applications on top of it.
Luckily, Twitter seems to be a programmer&#8217;s best friend, why? you may be asking&#8230; because of its wonderful API. Well designed and documented, what else could you wish?
The Twitter [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-117" title="twitter_bird" src="http://blog.moove-it.com/wp-content/uploads/2009/03/twitter_post.jpg" mce_src="http://blog.moove-it.com/wp-content/uploads/2009/03/twitter_post.jpg" alt="twitter_bird" height="95" width="161">Tweet tweet tweet! All the farm is chating about <b>Twitter</b>, all the farm is using twitter and all the farm is building applications on top of it.</p>
<p>Luckily, Twitter seems to be a programmer&#8217;s best friend, why? you may be asking&#8230; because of its wonderful <b>API</b>. Well designed and documented, what else could you wish?</p>
<p>The Twitter <b>API is RESTful</b>, but it doesn&#8217;t only mean that you can access to it over <b>HTTP</b>, sending parameters in <b>GET </b>or <b>POST </b>requests, it also means that it is designed complying with the <a href="http://blog.moove-it.com/?p=74" mce_href="http://blog.moove-it.com/?p=74">principles of REST</a>. A plus to this API is that it supports <b>JSON</b>, <b>XML</b>, <b>RSS</b> and <b>Atom </b>data formats and user authentication is reached by<b> Basic Auth</b>.</p>
<p>Due to the popularity of the service, a bunch of libraries have been developed and are heavily used to interact with Twitter, but if you like to explore the other end of the telephone line you are reading the right post <img src='http://blog.moove-it.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> . As we&#8217;ve said the API is accessible through HTTP, so let&#8217;s go and fire up your browser and point to:</p>
<p><b><span style="color: rgb(51, 102, 255);" mce_style="color: #3366ff;">http://twitter.com/statuses/user_timeline.xml?screen_name=twitterapi</span></b></p>
<p>Ok, what do you see? the last tweets of the user whose screen name is <b>twitterapi </b>formatted in xml. Another popular operation is:</p>
<p><b><span style="color: rgb(51, 102, 255);" mce_style="color: #3366ff;">http://twitter.com/statuses/friends_timeline.json?</span></b></p>
<p>That url returns (after successful authentication) the latest tweets posted by the authenticated user and it&#8217;s friends.</p>
<p>Because requesting a <b>URL </b>and satisfying basic authentication mechanisms are easy tasks in the day-to-day programming languages, invoking the API is a piece of cake. As <b>Python </b>has been one of our favorite languages, we&#8217;ll show you how to retrieve user statuses and post a new one. Here we go!</p>
<pre><b><span style="color: rgb(51, 153, 102);" mce_style="color: #339966;">import simplejson as jsonimport urllibimport urllib2</span></b></pre>
<pre><span style="color: rgb(51, 153, 102);" mce_style="color: #339966;"><b>GET_STATUS_URL = 'http://twitter.com/statuses/user_timeline.json?'UPDATE_STATUS_URL = 'http://twitter.com/statuses/update.json?'</b></span></pre>
<p>First we must declare the necessary imports (simplejson must be downloaded and installed before) and declare the URLs to be accessed.</p>
<pre><b><span style="color: rgb(51, 153, 102);" mce_style="color: #339966;">def get_statuses(screen_name):&nbsp;&nbsp;&nbsp; data = {'screen_name' : screen_name}</span><span style="color: rgb(51, 153, 102);" mce_style="color: #339966;">&nbsp; &nbsp; url = GET_STATUS_URL + urllib.urlencode(data)</span><span style="color: rgb(51, 153, 102);" mce_style="color: #339966;">&nbsp;&nbsp;&nbsp; f = urllib2.urlopen(url)</span><span style="color: rgb(51, 153, 102);" mce_style="color: #339966;">&nbsp;&nbsp;&nbsp; response = ''.join(f.readlines())&nbsp; &nbsp; return json.loads(response)</span></b></pre>
<p><b>get_statuses</b> retrieves the last tweets of <b>#{screen_name}</b>, it simply adds the screen_name parameter to the url, then requests it and finally parse the response using the json library.  To print the tweets retrieved in a simple manner:</p>
<pre><b><span style="color: rgb(51, 153, 102);" mce_style="color: #339966;">&nbsp;&nbsp;&nbsp; statuses = get_statuses(username)&nbsp;&nbsp;&nbsp; for status in statuses:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "# " + status['user']['screen_name'] + ": " + status['text']</span></b></pre>
<pre><b></b></pre>
<p>To post a tweet we simply do a POST request to the <b>UPDATE_STATUS_URL</b> passing the status (the tweet&#8217;s text) parameter.</p>
<pre><b><span style="color: rgb(51, 153, 102);" mce_style="color: #339966;">def update_status(username, password, status):&nbsp;&nbsp;&nbsp; __authenticate(username, password)&nbsp;&nbsp;&nbsp; data = {'status' : status}&nbsp;&nbsp;&nbsp; f = urllib2.urlopen(UPDATE_STATUS_URL, urllib.urlencode(data))&nbsp;&nbsp;&nbsp; response = ''.join(f.readlines())</span></b>    <span style="color: rgb(51, 153, 102);" mce_style="color: #339966;"><b>return json.loads(response)</b></span></pre>
<p>Username and password corresponds to the twitter user (you for example) whose status will be updated, obviously the user (ex: you again) must have a twitter account.</p>
<p>Because the API uses basic authentication we call to the <b>__authenticate</b> function:</p>
<pre><b><span style="color: rgb(51, 153, 102);" mce_style="color: #339966;">def __authenticate(username, password):&nbsp; &nbsp; passman = urllib2.HTTPPasswordMgrWithDefaultRealm()&nbsp;&nbsp;&nbsp; passman.add_password(None, TWITTER_HOST, username, password)&nbsp; &nbsp; auth_handler = urllib2.HTTPBasicAuthHandler(passman)&nbsp;&nbsp;&nbsp; opener = urllib2.build_opener(auth_handler)&nbsp; &nbsp; urllib2.install_opener(opener)</span></b></pre>
<p>For a good explanation on how this chunk of code works, please refer to&nbsp; <a href="http://www.voidspace.org.uk/python/articles/authentication.shtml" mce_href="http://www.voidspace.org.uk/python/articles/authentication.shtml">this great tutorial</a><br mce_bogus="1"></p>
<p>As you&#8217;ve seen, in a small amount of code we are interacting with Twitter, where to use it, how to display the results and what ideas bring<br />
to life is up to you!</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Interfacing+with+Twitter+http://32soc.th8.us" title="Post to Twitter"><img class="nothumb" src="http://blog.moove-it.com/wp-content/plugins/tweet-this/icons/tt-twitter.png" alt="[Post to Twitter]" border="0" /></a> <a class="tt" href="http://twitter.com/home/?status=Interfacing+with+Twitter+http://32soc.th8.us" title="Post to Twitter">Tweet This Post</a>&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://blog.moove-it.com/interfacing-with-twitter/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
