LSL: Newspaper Stand (Pull Data From an API and Display it in Second Life)

Second Life news stand

Second Life news stand

One of the more popular objects that I created in Second Life is the News Stand. This charming device takes any query term and displays the two latest headlines from Yahoo! News. Clicking the news stand will load the top news story for the topic in a web browser.

I’ve gotten more requests to customize the script than I can possibly keep up with, so I’ve decided to release the code under a creative commons license. I hope you find it useful! Please let me know if you make improvements, so that I can link to them from here.

Unfortunately, parsing XML at all and RSS feeds in particular is extremely messy in LSL. This script doesn’t contain a general RSS feed parser — the parsing code was written specifically for the Yahoo! news feed.

// News Stand script
// by Ann Enigma

string url="http://news.search.yahoo.com/news/rss?p=";
key requestid;
string current_url = "http://news.yahoo.com";
integer gLine = 0;
key data_card; // for settings notecard
string query;

integer mins = 10; // number of minutes between refreshes

get_news() {
    llWhisper(0, "Refreshing the news...");
    requestid = llHTTPRequest(url,[HTTP_METHOD,"GET"],"");       
}

default
{
    state_entry()
    {
        data_card = llGetNotecardLine("Settings",gLine); // load settings from notecard
    }

    // read data from the notecard
    dataserver(key query_id, string data) {
        if (query_id == data_card) {
            if (data != EOF) {    // not at the end of the notecard
                if (gLine == 0) {
                    query = data;
                } 
            }
        }
        state setup;
    }
    changed(integer change) {
        llResetScript();
    }
}

state setup {
    state_entry()
    {
        url += llEscapeURL(query); // set the url
        get_news(); // get the news!
        llSetTimerEvent(60*mins); // every mins # of minutes
    }

    touch_start(integer total_number)
    {
       llLoadURL(llDetectedKey(0), "Current Top Story", current_url);
    }
    http_response(key req_id, integer status, list metadata, string body) {
        
        integer count = 0;
        string display;
        
        if (req_id == requestid) { // parse the results (unfortunately messy)
            list result = llParseString2List(body,["<",">","n","[","]"],[]);
            integer length = llGetListLength(result);
            integer i = 0;
            for (i = 0; i < length; i++) {
                if (llList2String(result,i) == "title" && llList2String(result,i+4) == "/title") {
                    display += llList2String(result,i+3) + "n";
                    if (count == 1) { 
                        current_url = llList2String(result,i+6);
                    }
                    count++;
                }
            }
            llSetText(display, <1,1,1>, 1.0);
        }
    }
    timer() {
        get_news();
    }
    changed(integer change) {
        llResetScript();
    }

}

This script is released under Creative Commons License. Have fun!

Create a notecard called “Settings”, with a single line containing your search term. For example:

Educational Technology

The original news stand comes with these instructions, which may come in handy:

To set up the News Stand, simply open the "Settings" notecard, delete the default search term, and type in your own!

This gadget will refresh every 10 minutes, and will display the top two headlines on Yahoo! News for any search term. If you click on the news stand, it will load the top story in a web browser.

If you would like to build your own news stand, you can simply drag the script and "Settings" notecard into any object.

If you want the no-assembly-required version, the News Stand is available for L$350 at Xstreet SL Marketplace or from the ICT Library on Info Island in-world.

The original news stand object was created by (now former) student Kyle Pouliot.


  • http://sarastreeter.com Sara Streeter

    Love it! This is what makes SL fun – you code things into existence. Thanks for sharing your script!

  • http://squareprofit.com Hellen CLARK

    I like your blog and wanted to say keep up the good work. Do you plan on posting more soon?

  • http://john-norris.net John Norris

    Many thanks for releasing the code. I’m using it to display H1N1 info at Healthinfo Island and it works like a charm.

    I’m looking at modifying it to be used with other RSS feeds…but am still just learning LSL. But what you have shared has gotten me quite far! Thanks

  • Jason Hagger

    Very cool! No more alt-tabbing to check cnn.com!

  • Anthony Curtis

    Hilary, thanks for this release of your Second Life news stand. I’m sure your generosity of spirit and the expression of kindness here will provide opportunities for new learning experiences by untold numbers of Second Life residents in the future. I’m going to introduce my students to this script as an opportunity for experiences in both scripting and building.

  • Arielyn Docherty

    Tried purchasing the constructed version on XStreet, as well as building my own with the script above–both to no avail. Any suggestions?