Difference between revisions of "User:Takat Su/Name2Key"

From Second Life Wiki
Jump to navigation Jump to search
m (Created page with '{{LSL Header}}__NOTOC__ == Name2Key == '''This is an update to Maeva Anatine's excellent suggestion for an updated name2key function. Rather than overwrite all her work, I crea...')
 
m
Line 64: Line 64:
     }
     }
}</lsl></div>
}</lsl></div>
<div id="box">
== Google App Python Code ==
<lsl>
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import string
import urllib, urlparse
kURL = 'http://vwrsearch.secondlife.com/client_search.php?session=00000000-0000-0000-0000-000000000000&q='
kProfile = "Resident profile"
kResult = "secondlife:///app/agent/"
class MainPage( webapp.RequestHandler ):
    def get(self):
        inName = string.upper(self.request.get("name"))
        name = string.join(string.split(inName, " "), "%20")
        data = urllib.urlopen(kURL + name).read()
        start = string.index( data, kProfile )
        foundName = data[start+18:start+18+len(inName)]
key = '00000000-0000-0000-0000-000000000000'
        if string.upper(foundName) == inName:
            start = string.index( data, kResult )
            key = data[start+len(kResult):start+len(kResult)+36]
        else:
            foundName = inName
        self.response.out.write("%s:%s" % (foundName, key))
application = webapp.WSGIApplication(
    [('/', MainPage)],
    debug = True)
def main():
    run_wsgi_app(application)
if __name__ == "__main__":
    main()
</lsl></div>
<div id="box">
== app.yaml file ==
<lsl>
application: name2key
version: 1
runtime: python
api_version: 1
handlers:
- url: .*
  script: name2key.py
</lsl></div>


<div id="box">  
<div id="box">  

Revision as of 22:39, 20 September 2009

Name2Key

This is an update to Maeva Anatine's excellent suggestion for an updated name2key function. Rather than overwrite all her work, I created this page. The idea is DEFINITELY hers - I just am creating an alternate implementation.

Still missing Name2Key functions in your code? Still relying on external databases that are not always up to date with latests SL subscribers?

Well now you can solve this by yourself, within your LSL script! All you have to do is to rely on LL Search engine!

I put a kind of "library" and a sample of usage for your convenience


Library

<lsl> integer cmdName2Key = 19790; integer cmdName2KeyResponse = 19791;

list gRequests;

key requestName2Key( string inName ) {

   list lNameParts = llParseString2List( inName, [" "], [] );
   string lFirstName = llList2String( lNameParts, 0 );
   string lLastName = llList2String( lNameParts, 1 );
   return llHTTPRequest( "http://name2key.appspot.com/?name=" + lFirstName + "%20" + lLastName, [], "" );

}

default {

   link_message( integer inFromPrim, integer inCommand, string inName, key inKey ) {
       if( inCommand == cmdName2Key )
           gRequests += [requestName2Key( inName ), inKey ];
   }
   
   http_response(key inKey, integer inStatus, list inMetaData, string inBody ) {
       integer lPosition = llListFindList( gRequests, [inKey]);
       if( lPosition != -1 ) {
           llMessageLinked( LINK_SET, cmdName2KeyResponse, inBody, llList2Key( gRequests, lPosition+1 ) );
           gRequests = llDeleteSubList( gRequests, lPosition, lPosition + 1 );
       }
   }

} </lsl>

Usage sample

<lsl> integer cmdName2Key = 19790; integer cmdName2KeyResponse = 19791;

default {

   state_entry() {
       llMessageLinked( LINK_SET, cmdName2Key, "Test Name", NULL_KEY );
   }
   
   link_message( integer inFromPrim, integer inCommand, string inKeyData, key inReturnedKey ) {
       if( inCommand == cmdName2KeyResponse ) {
           list lParts = llParseString2List( inKeyData, [":"], [] );
           string lName = llList2String( lParts, 0 );
           key lKey = (key)llList2String(lParts, 1 );
       }
   }
}</lsl>

Google App Python Code

<lsl> from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app import string import urllib, urlparse

kURL = 'http://vwrsearch.secondlife.com/client_search.php?session=00000000-0000-0000-0000-000000000000&q=' kProfile = "Resident profile" kResult = "secondlife:///app/agent/"

class MainPage( webapp.RequestHandler ):

   def get(self):
       inName = string.upper(self.request.get("name"))
       name = string.join(string.split(inName, " "), "%20")
       data = urllib.urlopen(kURL + name).read()
       start = string.index( data, kProfile )
       foundName = data[start+18:start+18+len(inName)]

key = '00000000-0000-0000-0000-000000000000'

       if string.upper(foundName) == inName:
           start = string.index( data, kResult )
           key = data[start+len(kResult):start+len(kResult)+36]
       else:
           foundName =	inName
       self.response.out.write("%s:%s" % (foundName, key))


application = webapp.WSGIApplication(

   [('/', MainPage)],
   debug = True)

def main():

   run_wsgi_app(application)

if __name__ == "__main__":

   main()
</lsl>

app.yaml file

<lsl> application: name2key version: 1 runtime: python api_version: 1

handlers: - url: .*

 script: name2key.py
</lsl>

Comments

The main difference is that I wrote a an app for the google app engine that act as a relay. Since I was going that far, it also parses the return, strips out all the extraneous stuff and just returns you the name and key separated by a colon.

Takat Su