transmission line noise

home

Twit­ter feeds in Snow Leop­ard & Dash­code with REST

07 Oct 2009

Tagged with Dashcode, REST

This is a bit long winded but it might help someone.

I needed to add a twit­ter feed to a Dash­code, Dash­board Wid­get. I had recently upgraded to Snow Leop­ard and i am sure that before i did so the twit­ter feed was workng but after­wards not. How­ever i could be mis­taken on that one.

In this Wid­get i make a cou­ple of curl called that need to authen­ti­cate and they work ok. I also could make unau­then­ti­cated Twit­ter calls but when i used the exact code for the Twit­ter api i wanted to it just hung the Wid­get out to dry. Cept it never dried.

OK so I need to run curl to get the exter­nal data i want, it is all writ­ten with REST, so i need to use curl. This is easy in PHP but in Dash­code i really only have Javascript and that won’t help as it i client side. So i need to make a call to the Mac sys­tem with widget.system. This is easy and looks like this

var rate_01 = widget.system("/usr/bin/curl --user username:password
http://api.worldfirst.com/rates/rate.php?pair=EUR/GBP", null).outputString;

This line by the way works, pulls the data in per­fectly no prob­lems. How­ever for the Twit­ter API i need to be a bit more com­plex. I need a spe­cific users time­line and i want only to get the lat­est feed from them to dis­play. So check out the Twit­ter API and use screen_name=xxx and count=1 and the rus­lut­ing code looks like .…

var rate_02 = widget.system("/usr/bin/curl --user ausername:password
http://twitter.com/statuses/user_timeline.xml?screen_name=Screen_Name&count=1",
null).outputString;

Now if i strip the wid­get items out and just use the curl ele­ment a the CLI on the Mac it works fine, except for bring­ing back afull xml rather then the first item.

Just to be clear

/usr/bin/curl --user username:password
http://twitter.com/statuses/user_timeline.xml?screen_name=Screen_Name&count=1;

almost works out­side of the wid­get, but run­ning the whole thing inside the wid­get doesn’t even though the other authen­ti­cated REST call does.

Another point that also came to mind was that by using the authen­ti­ca­tion in the wid­get i was effec­tively giv­ing this away to any­one who cared to down­load the wid­get and open up the files. Not a good ides.

So what i did was first set up a php page for my web site that got the data from the twit­ter api and then use sim­plexml to parse and extract the data i wanted

<?php

// twitter feed for company
// initilaise curl
    $curl_conn = curl_init();
    // set URL and other appropriate options
    curl_setopt($curl_conn, CURLOPT_URL,
"http://twitter.com/statuses/user_timeline.xml?screen_name=Screen_Name&count=1");
    // Use basic authentication
    curl_setopt($curl_conn,CURLOPT_USERPWD,
"username:password" ); // aranaio:arana10
    // grab URL and pass it to the browser
    curl_setopt($curl_conn, CURLOPT_RETURNTRANSFER, 1);
    // this loads to the srting as opposed to the screen so you can further process it
    $result = curl_exec($curl_conn);
    //curl_exec($curl_conn);
    curl_close ($curl_conn);
    // query data
    $ss = simplexml_load_string($result);
    $xml = $ss->xpath("status/text");
    print $xml[0];
?>

then use a sim­ple unau­then­ti­cated call from the wid­get to that site to get the data

function getTwitterFeed() {
    var twitter_feed = widget.system("/usr/bin/curl
'http://www.aranaio.com/wftwit.php'", null).outputString;
    document.getElementById('twitter-text').innerHTML = twitter_feed;
}

Thank you. You can always get in touch with me for more information

comments powered by Disqus