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

From Second Life Wiki
Jump to navigation Jump to search
m (Provide some theory of operation as well as the code for the google application server.)
m
Line 73: Line 73:
from google.appengine.ext import webapp
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp.util import run_wsgi_app
import string
import urllib, urlparse
import urllib, urlparse


Line 82: Line 81:
class MainPage( webapp.RequestHandler ):
class MainPage( webapp.RequestHandler ):
     def get(self):
     def get(self):
         inName = string.upper(self.request.get("name"))
         inName = self.request.get("name").upper()
         name = string.join(string.split(inName, " "), "%20")
         name = inName.replace(" ", "%20")
         data = urllib.urlopen(kURL + name).read()
         data = urllib.urlopen(kURL + name).read()
         start = string.index( data, kProfile )
         start = data.index( kProfile )
         foundName = data[start+18:start+18+len(inName)]
         foundName = data[start+18:start+18+len(inName)]
key = '00000000-0000-0000-0000-000000000000'
key = '00000000-0000-0000-0000-000000000000'
         if string.upper(foundName) == inName:
         if foundName.upper() == inName:
             start = string.index( data, kResult )
             start = data.index( kResult )
             key = data[start+len(kResult):start+len(kResult)+36]
             key = data[start+len(kResult):start+len(kResult)+36]
         else:
         else:

Revision as of 12:07, 5 March 2010

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

To use, you make an llHttpRequest to the application running on the Google Application Server (a free service for moderate users). The URL looks like this: http://name2key.appspot.com/?name=firstname%20lastname i.e. the first name of the person you want to look up is separated from their last name by "%20" (this is how the space character is coded in URLs). The application doesn't care if you get the case correct. The application will format the body of the return as follows: firstname lastname:key, where firstname and lastname will be capitalized correctly and the key is either the key for the person or the null key (00000000-0000-0000-0000-000000000000). It really isn't much code if you prefer to code it into your main script rather than rely on an external script for the lookup.


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 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 = self.request.get("name").upper()
       name = inName.replace(" ", "%20")
       data = urllib.urlopen(kURL + name).read()
       start = data.index( kProfile )
       foundName = data[start+18:start+18+len(inName)]

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

       if foundName.upper() == inName:
           start = data.index( 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