Permanent Primitive URL

From Second Life Wiki
Jump to: navigation, search

Created by Kira Komarov.

Web-Related Tree

+-------------+ +-------------+ +---------+ +-----------------------------+ +--------------------------+ | Prowler | | Gremlin | | N2K | | Permanent Primitive URL | | Collaborative Coding | +------+------+ +-------------+ +---------+ +------------+----------------+ +--------------------------+ | | +--------+--------+ +-------------------+-------------------+ | SIM Status | | Population Genetics and Selection | +-----------------+ +-------------------+-------------------+ | +------------+-------------+ | Interactive Bacteria | +--------------------------+

ChangeLog

  • 22 December 2011

Added llResetScript in the on_rez event, to make sure that the script starts.

Introduction

When you need to pass data into SL from outside, the first problem you will encounter is that llRequestURL() generates a new URL every time the script is reset, de-rezzed or the region the primitive is restarted. It becomes very difficult to keep track of the URLs and it would be easier if there were some permanent pointer that could be updated every time the primitive's URL changes.

The script below will allow you to create a permanent primitive URL by using the services at tiny.cc. Every time the script is reset, the primitive de-rezzed or the region restarts, the script below will send a request to tiny.cc and update the short URL to point to the newly acquired URL.

Setup

  • Go to http://tiny.cc and create an account.
  • Log-in at http://tiny.cc and visit http://tiny.cc/api-docs to get your API key.
  • Create a primitive and a new script in that primitive.
  • Copy the code from the code section below and paste it into the new script in that primitive.
  • Configure the script to set-up your custom url, user name and API key.

Configuration

There are several options you can configure in the CONFIGURATION section:

string CUSTOM_URL = "ksim";
string USER_NAME = "kve";
string API_KEY = "fbf9ra57-5d54-9fbg-89fd-711bc7b163c3";

Where USER_NAME is the name you used to sign-up on http://tiny.cc, API_KEY is the API key generated by visiting http://tiny.cc/api-docs and CUSTOM_URL is the short descriptive name of the simulator URL.

For example, the configuration above will create and update the primitive's URL, setting it to: http://tiny.cc/evesim

Code

//////////////////////////////////////////////////////////
// [K] Kira Komarov - 2011, License: GPLv3              //
// Please see: http://www.gnu.org/licenses/gpl.html     //
// for legal details, rights of fair usage and          //
// the disclaimer and warranty conditions.              //
//////////////////////////////////////////////////////////
 
//////////////////////////////////////////////////////////
//                   CONFIGURATION                      //
//////////////////////////////////////////////////////////
// This is the url extension, ie http://tiny.cc/biose.
string CUSTOM_URL = "evesim";
// Set this to the username you signed up with on 
// http://tiny.cc.
string USER_NAME = "eve";
// Set this to the API key generatred by tiny.cc. To
// generate one, visit: http://tiny.cc/api-docs and you
// will see Your "API Key:" followed by a key.
string API_KEY = "fbf9ba57-4d54-6fbe-89cd-761bc7a163c3";
//////////////////////////////////////////////////////////
//                     INTERNALS                        //
//////////////////////////////////////////////////////////
 
key rlReq = NULL_KEY;
key glReq = NULL_KEY;
string smURL = "";
 
default
{
    state_entry()
    {
        llSetTimerEvent(5);
        llRequestURL();
    }
    on_rez(integer param) {
        llResetScript();
    }
    timer() {
        if(smURL != "") {
            rlReq = llHTTPRequest("http://tiny.cc/?c=rest_api&format=json&version=2.0.3&m=shorten&login=" + USER_NAME + "&apiKey=" + API_KEY + "&shortUrl=" + CUSTOM_URL + "&longUrl=" + llEscapeURL(smURL), [HTTP_METHOD, "GET"], "");
            llSetTimerEvent(0);
            return;
        }
    }
    http_request(key id, string method, string body)
    {
        if (method == URL_REQUEST_GRANTED) {
            smURL = body;
            return;
        }
 
        if(method == URL_REQUEST_DENIED) {
            llResetScript();
            return;
        }
 
        if(method == "GET") {
            llHTTPResponse(id, 200, "UP");
            return;
        }
    }
    http_response(key request_id, 
        integer status, 
        list metadata, 
        string body)
    {
        if (glReq == request_id) {
            llHTTPResponse(glReq, 200, "");
            return;
        }
        if(rlReq == request_id) {
            if(~llSubStringIndex(body, "1215")) {
                glReq = llHTTPRequest("http://tiny.cc/?c=rest_api&format=json&version=2.0.3&m=edit&hash=" + CUSTOM_URL + "&login=" + USER_NAME + "&apiKey=" + API_KEY + "&shortUrl=" + CUSTOM_URL + "&longUrl=" + llEscapeURL(smURL), [HTTP_METHOD, "GET"], "");
            }
            llHTTPResponse(rlReq, 200, "");
            return;
        }
    }
}