<?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>HauntedShell &#187; Unproductivity</title>
	<atom:link href="http://hauntedshell.com/category/unproductivity/feed/" rel="self" type="application/rss+xml" />
	<link>http://hauntedshell.com</link>
	<description>Just another Hauntedshell.com weblog</description>
	<lastBuildDate>Sat, 01 May 2010 20:05:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>What PHP, JSON, jQuery and James Cameron have in common</title>
		<link>http://hauntedshell.com/2010/01/09/what-php-json-jquery-and-james-cameron-have-in-common/</link>
		<comments>http://hauntedshell.com/2010/01/09/what-php-json-jquery-and-james-cameron-have-in-common/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 15:02:26 +0000</pubDate>
		<dc:creator>Kwame</dc:creator>
				<category><![CDATA[Unproductivity]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Avatar]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[movies]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Silverbird]]></category>

		<guid isPermaLink="false">http://www.hauntedshell.com/?p=164</guid>
		<description><![CDATA[Over the holidays, I spend a few days in Accra. While I was there, one of the things at the top of my todo list was to go and watch James Cameron&#8217;s Avatar (which I hear is a fantastic movie, by the way). Only place to watch it of course, was the Silverbird Cinema at [...]]]></description>
			<content:encoded><![CDATA[<p>Over the holidays, I spend a few days in Accra. While I was there, one of the things at the top of my todo list was to go and watch James Cameron&#8217;s Avatar (which I hear is a fantastic movie, by the way). Only place to watch it of course, was the Silverbird Cinema at the Accra Mall. So, being the consummate geek that I am, I checked their Twitter feed for scheduling information. Turns out Avatar would be showing that evening at 9:30pm.  Alright then. Since I would be leaving for Kumasi the next day, that left me just about enough time for me to finish packing, take a nap and have supper first.</p>
<p>Only one tiny hitch; I wasn&#8217;t paying attention and misread the time. It was actually showing at 7:30pm. I&#8217;m sure you will be able to figure out how the rest of the evening played out. Anyways, fast foward two days later. I&#8217;m still grumpy and decided that I would have to do something, even  if it as completely useless. So I threw together a little php script that displays the Silverbird movie lineup in a somewhat more reasonable manner.</p>
</p>
<p style="text-align: center">
<div id="attachment_165" class="wp-caption aligncenter" style="width: 460px"><a title="Much better" rel="lightbox" href="http://www.hauntedshell.com/wp-content/uploads/2010/01/main.jpg"><img title="Much better" src="http://www.hauntedshell.com/wp-content/uploads/2010/01/main-300x160.jpg" alt="Much better" width="450" height="240" /></a><p class="wp-caption-text">Much better</p></div>
<p><span id="more-164"></span></p>
<p style="text-align: left">Basically, this is how it works:<br />
It gets the Silverbird&#8217;s last 8 or so twitter status updates in JSON format, applies a regex and writes the results into a list.<br />
<code><br />
$curl = curl_init();<br />
curl_setopt ($curl, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/silverbirdghana.json?callback?");<br />
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);<br />
$result = curl_exec ($curl);<br />
curl_close ($curl);<br />
</code>
</p>
<p>I used CURL to retrieve the status data in JSON. It could have easily been in RSS, ATOM or XML but JSON was much much easier to work with. The &#8216;?callback?&#8217; at the end of the url is required for cross-domain requests(I think).</p>
<p><code><br />
$output = json_decode($result);<br />
foreach($output as $tweet):<br />
preg_match('/^"(.*)".*\[(.*)\]\s([0-9:]+\s[APM]+).*SCREEN\s([0-9])/', $tweet-&gt;text, $info);<br />
$item = '&lt;li&gt;';<br />
$item .= '&lt;span class="title"&gt;' .$info[1] . '&lt;/span&gt;';<br />
$item .= '&lt;span class="time"&gt;' .$info[3] . ' on &lt;/span&gt;';<br />
$item .= '&lt;span class="day"&gt;' .$info[2] . '&lt;/span&gt;';<br />
$item .= '&lt;span class="screen"&gt;' .$info[4] . '&lt;/span&gt;';<br />
$item .= '&lt;/li&gt;';<br />
echo $item;<br />
endforeach;<br />
</code></p>
<p>So using php&#8217;s <a href="http://php.net/manual/en/function.json-decode.php">json_decode</a> function to convert the JSON data directly into an associative array, all that was left to do was to loop over the array, apply a regex to the text of the tweets and print the results. Of course, I wasn&#8217;t satisfied and ended adding some jQuery flavour to refresh the content dynamically(More on that later. You can have a look at the source code below if you&#8217;re curious though). Then I cooked up some CSS and called it a day(meaning I lost interest). Of course, it&#8217;s far from perfect: there&#8217;s the issue of timeouts and exceeding the Twitter API limits (those are the first two that come to mind).</p>
<div class="wp-caption aligncenter" style="width: 458px"><a rel="lightbox" href="http://www.hauntedshell.com/wp-content/uploads/2010/01/Whats-On-_1263048946821.jpg"><img src="http://www.hauntedshell.com/wp-content/uploads/2010/01/Whats-On-_1263048946821.jpg" alt="" width="448" height="252" /></a><p class="wp-caption-text">Told you it wan&#39;t perfect</p></div>
<p>What was the point of all this? Well I did learn that I am not above doing something completely useless just so that I can write a post telling a story about it.  If I have some free time this weekend. I&#8217;ll see if I can cook up a desktop widget or something.
</p>
<p>Source code: <a href='http://www.hauntedshell.com/wp-content/uploads/2010/01/whatson.zip'>whatson.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://hauntedshell.com/2010/01/09/what-php-json-jquery-and-james-cameron-have-in-common/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
