The Big Bang Theory and Twitter

If you never heard of The Big Bang Theory, I suggest you watch it immediately. You can watch a sample scene and read @pradt’s review about the Big Bang Theory season 1 here. If you have watched it and you are a twitterer, you might interested to check out these accounts.

@sheldoncooper – I feel he is not nerd as in the show.
@leonardhofstadt
@pennyin4b
@wolowizard
@TheRaj and @kunalnayyar

IMO, account name give you enough clue to guess the owner :D. Follow them ASAP.

PS. The complete season 1 DVD is now on sell.

Creating Twitter timeline badge

Recently, I’m getting addicted to Twitter. Since I’ve moved to my new host, this is a good opportunity to try something new.

Twitter, in fact, does provide a pack of pre-built badges. But they are not my type, 2 flash badges and a html/javascript one. Fortunately, Twitter also provides a set of API over JSON/REST to query almost everything out. After take a look at API, I ended up with an idea to create a new one. I want to display either my friends’ or mine in the timeline so I picked ‘friend_timeline’ method over the ‘user_timeline’ method because the latter will return only your own tweets. After that, I want the result in JSON therefore I have to ping to http://twitter.com/statuses/friends_timeline/wiennat.json.

Next step is to figure how do I use the json result from twitter. Because of security reason, AJAX call cannot go across the domain. This reason has an impact on me, I can’t call to timeline url directly. Then, proxy call came into my head but I hesitated to do it so because I remembered that I found many websites with twitter badges and I was sure they didn’t use this technique. After doing some research, I knew the solution, Twitter provides a way to set up a callback by using ‘callback’ parameter. This reflects a change to target url, http://twitter.com/statuses/friends_timeline/wiennat.json?callback=cbfn (where cbfn is the name of callback function). Instead of only JSON data, using callback parameter causes Twitter to return the JSON data as a parameter of specified function call.

To make it possible in HTML, I wrapped the url in script element and put it at the end of element. I delayed this process by using $(document).ready to wait until DOM is ready before loading the timeline. This prevents the timeline to be loaded while the page still is not loaded completely.

After append twitter script to the body element, timeline will be loaded and will call the function specified with callback parameter, inthis case Twitter.twcb. Twitter.twcb is a function that processes the returned data and put it in desired element.

twcb : function(ob) {
var twitterpane = $(‘#’ + Twitter._twitter_parent);

ot = ‘<ul id="’+twitterpane.id+’_inner" class="twpane">’;
for (var j=0;j<Twitter._tweet_count ;j++ ) {
at = Twitter.relative_time(ob[j].created_at);
us = ob[j].user.screen_name;
id = ob[j].id;
tx = ob[j].text;
ot += (‘<li><span class="twus"><a href="http://twitter.com/’ + us + ‘">’+us+'</a></span>: ‘ + tx + ‘ <span class="twdtxt"><a href="http://twitter.com/’+us + ‘/statuses/’+ id+’">#</a> <small>~’+at+'</small></span></li>’);
}
ot += ‘</ul>’;
ot += ‘<div style="font-size:xx-small;text-align:right">powered by <a href="http://twitter.com">twitter</a></div>’;
twitterpane.css(‘overflow’, ‘hidden’);
twitterpane.css(‘height’,0);
twitterpane.html(ot);
twitterpane.animate({
height: twitterpane[0].scrollHeight
}, 750 );

As for relative time, I copied it out from the twitter badges. Actually, jquery doesn’t provide blind effect likes script.aculo.us. To do it so, set the overflow property of desired element to hidden and then animate its dimension to achieve the effect. In this step, I got struck for 4 hrs. because I don’t know how to specified target height dynamically. Thank you iPats for his advice on scrollHeight. To run this script, I put the script into external script and initialize the script with this code

<script type="text/javascript" src="/misc/twitterj.js"></script>
<script type="text/javascript">
$(document).ready(function() {
Twitter.init(‘wiennat’, ‘itwitterpane’, 7); });
</script>
<div id="itwitterpane"> <img src="/images/spinner.gif" />loading… </div>

That’s not too hard for me to create such a simple badge like this.

time

In PHP, both mktime and gmtime yield the same unix_timestamp with no argument passed.

<?php

echo mktime() == gmmktime() ? ‘eq’ : ‘ not eq’;

?>

yields ‘eq’

However, mktime argument is “local time” but the gmmktime’s is “GMT time”.

<?php

echo Date(“D M Y H:i:s e”, mktime(0,0,7,10,12,2007) );
echo ‘<br/>’;
echo Date(“D M Y H:i:s e”, gmmktime(0,0,7,10,12,2007) );
?>

yields

Fri Oct 2007 00:00:07 Asia/Krasnoyarsk
Fri Oct 2007 08:00:07 Asia/Krasnoyarsk

Let’s compare this to .NET.

In .NET, class DateTime has responsibility to these kind of task. In fact, DateTime class has one static property to get current, local time (or day), DateTime.Now, and another property, DateTime.UtcNow, provides current universal time.  To calculate time, Timespan struct comes into play but I usually use various method on DateTime object to do so. Moreover, universal time is advised to use in time calculation.

DateTime d = DateTime.Now // get current local time
DateTime u = d.ToUniversalTime() // get universal time of corresponding time
DateTime l = u.AddHours(7); // plus 7 to hour, get time for Bangkok timezone.

Personally, .NET seems to be easier than PHP. Formatting date and time syntax is much better and more understandable than PHP.

Technorati tags: , , ,

Windows Live Messenger ภาษาไทย

วันก่อนมีคนมาถามว่า ถ้าลง Windows Live Messenger ลงไปแล้วเป็นภาษาไทยแล้วอ่านไม่รู้เรื่อง จะทำยังไงดี

ตอนแรกก็คิดว่ามันน่าจะให้เลือกเปลี่ยนภาษาได้ง่ายๆ แต่เนื่องจากไม่ได้ใช้ภาษาไทยก็เลยไม่ได้เอะใจ จนเมื่อวานไปเล่นที่เครื่องแม่แล้วลงเป็นภาษาไทยไว้ ก็เลยไปนั่งงมหาตั้งนาน จนสุดท้ายได้วิธีมาแล้วแต่มันเป็นวิธีที่รับไม่ค่อยได้ (แต่ก้ต้องทำถ้าจะใช้ภาษาอังกฤษ)

  1. Uninstall Windows Live Messenger ออก
  2. เปลี่ยนภาษาของบราวเซอร์ให้เป็นภาษาอังกฤษ
    language_ff
    สำหรับ FireFox
    language_ie
    สำหรับ IE
  3. เข้าไปที่ messenger.live.com
  4. เลือก Get It Free
  5. ลงตามปกติ

แค่นี้ก็ได้ Windows Live Messenger ภาษาอังกฤษแล้ว
ใครลองแล้วบอกด้วย อันนี้อ่านมาจาก Help ของ Windows Live อะ

Update: ถ้าจะให้เปลี่ยนจากภาษาอังกฤษให้เป็นภาษาไทยให้ดูที่ เปลี่ยน Windows Live Messenger เป็นภาษาไทย