Thursday, 28 February 2008

Fetching a URL and Parsing the Result and more scripts

If you would like to fetch a URL and go to web site from SL with a response and parsing the result,,,, the problem with this code is it doesn't open any sourceURL except "http://blog.secondlife.com/feed/".however, I'd refered to the book and found the wrong, so after if statment to check if the http is ok I sould use llLoadURL immedatilly((the correct code at the end
// Get the first link in the first item in an RSS feed and open it in
// the user's web browser when the object is touched
//
// This script is very naive and not very robust. It expects only
// one user to be clicking on it at a time.

// URL for the RSS feed
string sourceURL=
"http://blog.secondlife.com/feed/";

// The standard HTTP success response code
integer HTTP_OK = 200;

// Key of the resident that clicked on the object. Saved in the
// touch_start event so that it can be used in the http_response event.
key requestingResident = NULL_KEY;


// Parse an HTTP response that should be an RSS feed and return the
// first link from the first item, or "" if the link could not be found
//
// body is the response from an HTTP request. Since HTTP requests are
// currently limited to 2048 characters, and since this is unknown data
// from the outside world, the body may or may not actually contain the
// information we want.
//

string getFirstItemLink(string body)
{
// Find the start of the "items" in the RSS feed
string startItems = "";
if (llSubStringIndex(body, startItems) == -1)
{
return "";
}

// get the string from the beginning of the first item to the end
// of the body
string items =
llGetSubString(body, llSubStringIndex(body, startItems), -1);

// find the first link in the items
string startLink = "";
string endLink = "";
integer startIndex = llSubStringIndex(items, startLink);
integer endIndex = llSubStringIndex(items, endLink);

// make sure startLink and endLink were found, and startLink is
// before end link
if (startIndex != -1 && endIndex != -1 && endIndex > startIndex)
{
string link =
llGetSubString(
items,
startIndex +llStringLength(startLink),
endIndex - 1);
// Got it!
return link;
}
else
{
return "";
}
}

default
{
touch_start(integer total_number)
{
// Save the requestor
requestingResident = llDetectedKey(0);

llSay(0, "Fetching: " + sourceURL);
// Get the RSS feed
llHTTPRequest(sourceURL, [], "");
}

http_response(key id, integer status, list metadata, string body)
{
// check if the page was successfully fetched
if (status == HTTP_OK)
{
string link = getFirstItemLink(body);
if (link != "")
{
// Got the link, load it in the user's browser
llLoadURL(requestingResident, "", link);
}
else
{
// Got a response, but didn't get the link
llSay(0, "Could not find link");
}
}
else
{
llSay(0, "Couldn't fetch page, http status: " +
(string)status);
}

// reset this variable
requestingResident = NULL_KEY;
}
}
here is the correct code to fetch URL:
integer HTTP_OK = 200;
key requestingResident = NULL_KEY;
string sourceURL= "http://baderalkhamees.blogspot.com/"; //to my blog here
default { touch_start(integer total_number)
{ requestingResident = llDetectedKey(0);
llHTTPRequest(sourceURL, [], ""); }
http_response(key id, integer status, list metadata, string body)
{ // check if the page was successfully fetched
if (status == HTTP_OK) { // Got the link, load it in the user's browser
llLoadURL(requestingResident, "", sourceURL); }
else {
llSay(0, "Couldn't fetch page, http status: " + (string)status);
}
// reset this variable
requestingResident = NULL_KEY; }
}
===================================================
the following script is to move the object when it is collidded...

default
{
state_entry()
{
llSetStatus(STATUS_PHYSICS, TRUE);// first you should change the statuse to
//physics by writing TRUE after STATUS_PHYSICS because thats enable the prim to be
//moved
}

collision_start(integer num_detected)// here the function must be collision to
//move
{

vector shove = llGetPos() - llDetectedPos(0);
shove = llVecNorm(shove);
shove = shove * 100.0;// 100.0 is the destince the prim will move
llApplyImpulse(shove, FALSE);
}
}

No comments: