Author Archive for Zz85

Timeless Belt Of Time: How I Integrated Twitter in Timeline

using JSON, PHP, CURL, JS, and of course HTML, and APIs of Simile Timeline and Twitter.

Timeline Twitter Mash up
Looks a little like a seismograph?

My first little personal project in this year was dealing with time, used by myself and view daily by my colleges in TAD. Later in the year, I brainstormed several other ideas that I hope to implement as a web 2.0 application. I stumbled on the Simile Timeline project earlier, thought it was a good idea, but never tried it until today. But it was also the fun using Twitter that sparked the interest to use Timeline and to integrate them.

However, given the large interest in twitter and mash ups, I would have expected someone else to have done before. Still there are handful of resources with the knowledge required to hack the pieces together. So let me outline what I did.

1. Prepare a php script to call Twitter’s API to retrieve Twitter posts using CURL. To do as little coding as possible, I decided to use JSON format (over XML), pass it self as an object into the (html) client’s javascript method.
2. The javascript method would iterate each of twitter’s data then generate the events in Timeline’s format.
3. Initialize and run Timeline’s API to render.

twitter.php The server side script.


<?php
/* 29 Dec 2007, http://zz85.is-a-geek.com/ */
// Edit your twitter's username and password
$username = 'USERNAME';
$password = 'PASSWORD'; 

// The twitter API address
//$url = 'http://twitter.com/account/archive.json'; // Each archive API call could return 80 statuses,
								// but there seem to be an error on Twitter's side so I'll use
								// the User timeline call which returns only the last 20 posts.

$url = 'http://twitter.com/statuses/user_timeline.json';

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

echo "timeline_data($buffer);"; // timeline_data is the javascript method to be execute
?>

time.html The Client side html

<html>
  <head>
  <title>Timeline API and Twitter API Integration</title>
   <link rel='stylesheet' href='http://simile.mit.edu/timeline/docs/styles.css' type='text/css' />
    <script src="http://simile.mit.edu/timeline/api/timeline-api.js" type="text/javascript"></script>
    <script>
    	var tl;
	var eventSource = new Timeline.DefaultEventSource();

	function onLoad() {
		var scriptTag = document.createElement('script');
		scriptTag.src = "http://CHANGEME_DOMAIN/twitter.php"; // Change to URL of your php script
		document.body.appendChild(scriptTag); // Runs the contents from the URL being called as javascript
	}

function createTimeline() {
  var bandInfos = [

   Timeline.createBandInfo({
        width:          "65%",
	timeZone: 8, // I have to adjust the time as I live in GMT +8
	eventSource:    eventSource,
        intervalUnit:   Timeline.DateTime.HOUR,
        intervalPixels: 80
    }),
    Timeline.createBandInfo({
	    showEventText:  false,
	eventSource:    eventSource,
	width:          "20%",
        intervalUnit:   Timeline.DateTime.DAY,
        intervalPixels: 70
    }),
    Timeline.createBandInfo({
        width:          "15%",
	showEventText:  false,
        intervalUnit:   Timeline.DateTime.MONTH,
        intervalPixels: 100
    }),

  ];

  // sync bands
  bandInfos[1].syncWith = 0;
  bandInfos[1].highlight = true;
  bandInfos[2].syncWith = 1;
  bandInfos[2].highlight = true;

  tl = Timeline.create(document.getElementById("my-timeline"), bandInfos);
   //tl.loadJSON("cubism.js", function(json, url) { eventSource.loadJSON(json, url); });
}

var resizeTimerID = null;
function onResize() {
    if (resizeTimerID == null) {
        resizeTimerID = window.setTimeout(function() {
            resizeTimerID = null;
            tl.layout();
        }, 500);
    }
}

function timeline_data(tjson) { // method called after response of twitter.php is exectured
	for (entries in tjson) {
		 var dateEvent = new Date(tjson[entries]["created_at"]);
		 var urlbase = 'http://twitter.com/' ;
		var evt = new Timeline.DefaultEventSource.Event(
		dateEvent, //start
		dateEvent, //end
		dateEvent, //latestStart
		dateEvent, //earliestEnd
		 true, //instant
		 tjson[entries]["text"], //text
		 '<a href="'+ urlbase + tjson[entries]["user"]["name"] + '/">' +
		 '<img src="' + tjson[entries]["user"]["profile_image_url"] + '" />'
		 + tjson[entries]["user"]["name"] +'</a>' + ' '
		  + tjson[entries]["text"] +
		 '<br /><a href="' + urlbase + 'statuses/' +tjson[entries]["id"]+'">Source:</a>'+ tjson[entries]["source"] + ''
		 //description
		);
		eventSource.add(evt);
	}
	createTimeline();
 }    </script>
  </head>
  <body onload="onLoad();" onresize="onResize();">
  	<h2>Zz85 Integration Test of Timeline API with Twitter API</h2>
	<div id="my-timeline" style="height:450px; border: 1px solid #aaa"></div>
	<br />version 1 by <a href="http://zz85.is-a-geek.com/">http://zz85.is-a-geek.com/</a>
  </body>
</html>

Well, I hope this would work for you, or at least help you to do something better. At least if you managed to do what I did, you would be able to visualise what you were doing on the dynamic scrollable timeline. Of course, the timeline javascript library issnt perfect, and I wish there were features like: adding events by clicking on the timeline, dragging the duration, zooming in&out and dragging the events around. Anyway I have some ideas, that I’m likely not to do it, but its implementable.

  • Integrate Photos eg. Flickr/Fackbook + Blogs + MAps + …
  • Integrate Calendars (eg. Google Calendars)…
  • Automatic Widening/Narrowing
  • Import and usage of own database with metadata
  • Layered Rows for Different events.
  • Integrate with other time applications like countdown counter
  • Integrate ToDo List

Have timeless fun! Drop me a note if you manage to do something satisfying after reading this article, or if you have some other cool ideas too =)

Further Readings

  1. Tutorial on using Timeline
  2. About JSON
  3. Posting to Twitter using PHP and CURL
  4. twitter api
  5. Google’s method of dynamic json data import
  6. a json inspector
  7. Creating timeline from (blog) feeds

Activity & Status Streams

Perhaps some keep a journal so they can remember what they have done in the past. Perhaps many nowadays use a blog for that purpose too. Twitter, which appeared in my previous post, a social networking site based mainly on activity streams could now be the synonym for “what am/was/are I/you doing”.

Twitter Ninja

Twitter’s API allows many implementation of getting twitter service, eg. this twitter balloon.

A similar feature exist in Facebook, and is perhaps the more popular among many is to announce their status or what they are doing. Recently I read that the first thing one would do after a relationship break off is to change their status on Facebook. Perhaps you are not that extreme, but might still be guilty of complaining, or showing off via your online messaging nick, status or personal message.

Talking about streams, no one can deny the power of RSS feeds, whether its a blog, twitter, or facebook, they all have them. And my preferred method for reading blogs, and my favorites sites.

And lastly, even Google must have realise the potential of these lifestreaming activities and acquired Twitter’s competitor Jaiku. Even though there are already a couple of Google Services eg. Maps/Earth + Twitter Mashups, it would be interesting to see what Google has in store for all of us (perhaps integrate gcalendars too).

Online Trends

Some of my random thoughts of how things changes and turn out

  • Library > Google
  • Encyclopedia > Wikipedia
  • Dictionary > Spellcheck
  • Books > e-Books
  • Telephone > Skype
  • BBS > Newgroups, Mailing Lists > Forums
  • Notice Boards > Portals > Blogs > Feeds
  • Yearbook > e-Circles > Friendster > Facebook
  • Maps / Street Directory > Google Earth / Maps
  • Bookmarks > del.icio.us > Digg
  • Icq > Msn > Web messesaging
  • Irc > ShoutBoxes > Twitter

become twitter
cartoon from gapingvoid.com

Lights Off, Laptop!

Based on our experiences use gadgets like handphones, digital cameras, DS etc, we probably guessed bulked of the batteries usage went into the LCD screen.

Personally I find little electricity used in turning the colours of the pixels, but much in powering the backlight. A digital watch I had exceeded its estimated battery life without its light used often. The DS Lite new brightness levels consumes the battery much faster than if you use the less bright options. When I use the DS for music listening, closing the lid usually turns off the LCD and allow much longer battery stamina. Digital cameras usually have an option for switching on and off the LCD screens, but what about laptops?

Let me go through the different options.

High-end models would have the LCD poweroff button: Hit “Fn+Battery Key”. If you don’t, you could dim it manually hitting the brightness buttons.

Why not close the laptop lid? Remember the reason why we just want just the lights of is for the computer to go doing what it needs to (play some music, transferring files using wifi, do some virus scan etc). Closing the lid but default usually means suspend, sleep or hibernate, so you need to change the power saving schemes if you like the lid shutting action.

Windows have this Blank screensaver which turns the screen black. Or you could download the freeware Power Dimmer which mimics iBooks screen-fading-energy-saving feature. Take note that these 2 doesn’t turn your screen off, only changing the screen black so the lights in them are still on. Anyway a tip for those who like to turn on their screensavers by demand, create a shortcut (on the desktop) to the screensaver file, double click it or assign a hot shortcut key to run them.

Next, in the windows energy saving section, you could specify screens to turn off after a period of time. Good, and I usually set it @ 2 minutes and power dimmer @ 1 minute. However, your media player may forbid the system entering power saving while you are listening to music.

Here’s more cool tools to the field. NirCmd is a command line based swiss knife for windows. Type nircmd.exe monitor off or create a shortcut for this command to turn off your screen at demand.

Need a easier software? There’s a software called MonOff but I’ve not tired it. However, my favorite software for controlling windows shutdowns called PowerOff also does the job for screens. Its a very small, no-installations-needed file, and after running it you task it to poweroff your screen/windows immediately or a specific time, or decide later at the icon-tray.

Further readings: 1
2

Lights off, sleep tight, keep dreaming, so don’t drain my battery, screen, eyesight and brain cycles too much.

Gastric

After feeling nausea for weeks, I thought if men could get morning sickness too. At least I thought it was motion sickness.

So I went to the doctor, and my surprise was I had gastric. The doctors said many patients didn’t know too, claim they just had “wind” and the next thing was they vomited blood.

I then heard the longest list of “do not eat” I have heard in my life. No spicy, no chilli, no curry (only porridge), no fruits- no orange, no kiwi, no … (only apples), no tea, no coffee, .. (only milo)

After medicine, I went home and concussed in bed.