MP3 Jukebox

I was selling this in SL, but with the forthcoming wide integration of Shared Media, this is going to become obsolete.

The PHP for this script uses the Snoopy class.

Main Script

key user;
list titles;
list links;
list times;
string access = "public";
string optionsa = "owner";
integer inuse;
key owner;
integer listener;
string mode;
string ptrack0;
string ptrack1;
string ptrack2;
string ptrack3;
string ctitle;
integer floats = TRUE;

string UpdateTracks(string title)
{
    if (ptrack0!=title)
    {
        ptrack3=ptrack2;
        ptrack2=ptrack1;
        ptrack1=ptrack2;
        ptrack1=ptrack0;
        ptrack0=title;
    }
    return "";
}

string PlayingText()
{
    string text;
    if (ctitle=="")
    {
        if (llGetListLength(links)!=0)
        {
            text+="\n*Links Loaded!*\n";
        }
        text+="\nClick search to locate a song. You can search by title or artist.";
    }
    else {text += "Playing:  "+ctitle;}
    if (ptrack1!=""){text += "\n\n*Previously Played*\n"+ptrack1;}
    if (ptrack2!=""){text += "\n"+ptrack2;}
    if (ptrack3!=""){text += "\n"+ptrack3;}
    return text;
}

default
{
    on_rez(integer i)
    {
        state TOS;
    }

    state_entry()
    {
        owner = llGetOwner();
        llMessageLinked(LINK_ALL_OTHERS,0,"","");
        llInstantMessage(llGetOwner(),"Started, ready for use!");
    }

    timer()
    {
        llSetTimerEvent(0);
        llListenRemove(listener);
        mode="";
        llInstantMessage(user,"Timed out! Please make your choice within 15 seconds!");
        inuse=FALSE;
    }

    touch_start(integer total_number)
    {
        integer usable;
        if (inuse==FALSE){usable=TRUE;}
        else if (llDetectedKey(0)==user){usable=TRUE;}

        if (usable==TRUE)
        {
            integer uaccess;
            if (floats==FALSE){llSetText("",<1,1,1>,1);}
            if (access=="public"){uaccess=TRUE;}
            else if (access=="group" && llDetectedGroup(0)==TRUE){uaccess=TRUE;}
            else if (access=="group" && llDetectedKey(0)==owner){uaccess=TRUE;}
            else if (access=="owner" && llDetectedKey(0)==owner){uaccess=TRUE;}
            if (uaccess==TRUE)
            {
                llWhisper(0,llDetectedName(0)+" has started using the device...");
                inuse=TRUE;
                llSetTimerEvent(0);
                llListenRemove(listener);
                mode="";
                user = llDetectedKey(0);
                llDialog(user,PlayingText(),[" ","Cancel","Options","Play","Stop","Restart","Search","Download","List"],-2593);
                listener = llListen(-2593,"","","");
                llSetTimerEvent(15);
            }
            else
            {
                llInstantMessage(llDetectedKey(0),"You do not have permission to use this object!");
            }
        }
        else
        {
            llSay(0,"This device is currently being used by "+llKey2Name(user)+", please wait until they have finished.");
        }
    }

    listen(integer chan, string name, key id, string mess)
    {
        llSetTimerEvent(0);
        llListenRemove(listener);
        if (chan==-2593)
        {
            if (mess==" ")
            {
                inuse=FALSE;
                mode="";
            }
            else if (mode=="options" && mess=="Float Text")
            {
                if (floats==TRUE)
                {
                    floats=FALSE;
                    llInstantMessage(user,"Floating text disabled...");
                }
                else
                {
                    floats=TRUE;
                    llInstantMessage(user,"Floating text enabled...");
                }
                inuse=FALSE;
            }
            else if (mode=="options" && mess=="OptnAccess")
            {
                if (user==owner)
                {
                    listener = llListen(-2593,"","","");
                    mode="optnaccess";
                    llDialog(user,"\nOwner - Only the person who rezzed the object may set the options.\nGroup - Anyone wearing a group tag of the same group this object is deeded to may change the options.",["Owner","Group"],-2593);
                    llSetTimerEvent(15);
                }
                else
                {
                    llInstantMessage(user,"You do not have permission to access these options!");
                    inuse=FALSE;
                }        
            }
            else if (mode=="optnaccess")
            {
                if (mess=="Owner")
                {
                    optionsa="owner";
                    llInstantMessage(user,"Options access now restricted to "+llKey2Name(owner));
                }
                else if (mess=="Group")
                {
                    optionsa="group";
                    llInstantMessage(user,"Options access set to group only!");
                }
                inuse=FALSE;
            }            
            else if (mode=="options" && mess=="AccessMode")
            {
                listener = llListen(-2593,"","","");
                mode="access";
                llDialog(user,"\nPublic - Allows anyone to search, and play songs.\nOwner - Only the person who rezzed the object may use it.\nGroup - Anyone wearing a group tag of the same group this object is deeded to may use it.",["Public","Owner","Group"],-2593);
                llSetTimerEvent(15);
            }
            else if (mode=="access")
            {
                if (mess=="Public")
                {
                    access="public";
                    llSay(0,"Public access enabled!");
                }
                else if (mess=="Owner")
                {
                    access="owner";
                    llInstantMessage(user,"Access now restricted to "+llKey2Name(owner));
                }
                else if (mess=="Group")
                {
                    access="group";
                    llInstantMessage(user,"Access set to group only!");
                }
                inuse=FALSE;
            }
            else if (mess=="Search")
            {
                listener = llListen(9,"",user,"");
                llInstantMessage(user,"Please say your search query now...\n*Speak on channel 9!");
                mode = "search";
                llSetTimerEvent(15);
            }
            else if (mess=="Cancel")
            {
                mode="";
                inuse=FALSE;
            }
            else if (mess=="Options")
            {
                integer uaccess;
                if (optionsa=="group" && llSameGroup(user)==TRUE){uaccess=TRUE;}
                else if (optionsa=="group" && user==owner){uaccess=TRUE;}
                else if (optionsa=="owner" && user==owner){uaccess=TRUE;}
                if (uaccess==TRUE)
                {
                    mode="options";
                    listener = llListen(-2593,"","","");
                    string dtext;
                    dtext+="\nFloating Text: ";
                    if (floats==FALSE){dtext+="Disabled";}
                    else {dtext+="Enabled";}
                    dtext += "\nAccess Mode: ";
                    if (access=="public"){dtext+="Public";}
                    else if (access=="group"){dtext+="Group Only";}
                    else if (access=="owner"){dtext+="Owner Only";}
                    dtext+="\nOptions Access: ";
                    if (optionsa=="owner"){dtext+="Owner Only";}
                    else if (optionsa=="group"){dtext+="Group Only";}
                    llDialog(user,dtext+"\n\nPlease choose an option",[" ","Cancel"," ","Float Text","AccessMode", "OptnAccess"],-2593);
                    llSetTimerEvent(15);
                }
                else {llInstantMessage(user,"You do not have permission to access the options!");}
            }
            else if (mess=="Play")
            {
                if (llOverMyLand(user)==TRUE)
                {
                    listener = llListen(9,"",user,"");
                    llInstantMessage(user,"Please say the number of the track you wish to play...\n*Speak on channel 9!");
                    mode = "play";
                    llSetTimerEvent(15);
                }
                else
                {
                    llInstantMessage(user,"This object is not on your land, or is not deeded to the land group!");
                    inuse=FALSE;
                }
            }
            else if (mess=="Stop")
            {
                if (llOverMyLand(user)==TRUE)
                {
                    llSay(0,"Stopping Parcel Media...");
                    llParcelMediaCommandList([PARCEL_MEDIA_COMMAND_STOP]);
                }
                else
                {
                    llInstantMessage(user,"This object is not on your land, or is not deeded to the land group!");
                }
                inuse=FALSE;                
            }
            else if (mess=="Restart")
            {
                if (llOverMyLand(user)==TRUE)
                {
                    llSay(0, "Restarting '"+ctitle+"'...");
                    llParcelMediaCommandList([PARCEL_MEDIA_COMMAND_STOP]);
                    llParcelMediaCommandList([PARCEL_MEDIA_COMMAND_PLAY]);           
                }
                else
                {
                    llInstantMessage(user,"This object is not on your land, or is not deeded to the land group!");
                }
                inuse=FALSE;
            }
            else if (mess=="Download")
            {
                listener = llListen(9,"","","");
                llInstantMessage(user,"Please say the number of the track you with to download...\n*Speak on channel 9!");
                mode="download";
                llSetTimerEvent(15);
            }
            else if (mess=="List")
            {
                integer i;
                string titlest;
                while (i<llGetListLength(links)+1)
                {
                    if (llList2String(links,i)!="")
                    {
                        string link = llUnescapeURL(llList2String(links,i));
                        list temp = llParseString2List(link,["/",".mp3"],[""]);
                        string title = llList2String(temp,llGetListLength(temp)-1);
                        titles = titles + [title];
                        integer num = i+1;
                        titlest += "\n("+(string)num+") "+title; 
                    }
                    ++i;
                }    
                llInstantMessage(user,titlest);
                inuse=FALSE;
            }        
        }
        else
        {
            if (mode=="search")
            {
                 llHTTPRequest("http://zetaphor.net/mp3/new.php?query="+llEscapeURL(mess),[HTTP_METHOD,"GET"],"");
                llInstantMessage(user,"Searching...");
            }
            else if (mode=="play")
            {
                llInstantMessage(user,"Preparing Track...");
                integer track = (integer)mess;
                string link = llList2String(links,track-1);
                ctitle = llList2String(titles,track-1);
                string duration = llList2String(times,track-1);
                UpdateTracks(ctitle);
                llSay(0,"Preparing new track...");
                llParcelMediaCommandList([PARCEL_MEDIA_COMMAND_STOP,PARCEL_MEDIA_COMMAND_URL,""]);
                llParcelMediaCommandList([PARCEL_MEDIA_COMMAND_TYPE,"audio/*"]);
                llParcelMediaCommandList([PARCEL_MEDIA_COMMAND_URL,link]);
                llSay(0, llKey2Name(user)+" started '"+ctitle+"'  ("+duration+")");
                if (floats==TRUE){llMessageLinked(LINK_ALL_OTHERS,0," \n \n \n"+PlayingText(),"");}
                llParcelMediaCommandList([PARCEL_MEDIA_COMMAND_PLAY]);
                inuse=FALSE;
            }
            else if (mode=="download")
            {
                integer track = (integer)mess;
                string link = llList2String(links,track-1);
                string title = llList2String(titles, track-1);
                llInstantMessage(id,"Sending direct MP3 link...");
                llLoadURL(id,"*Direct MP3 Link*\n("+(string)track+") "+title,link);      
                inuse=FALSE;                
            }
        }
    }

    http_response(key id, integer status, list meta, string body)
    {
        if (body!="")
        {
            llInstantMessage(user,"Reply recieved, preparing results...");
            links = [];
            titles = [];
            times = [];
            list tlinks = llParseString2List(body,["{"],[""]);
            integer i;
            string titlest;
            while (i<llGetListLength(tlinks)+1)
            {
                if (llList2String(tlinks,i)!="")
                {
                    list ltemp = llParseString2List(llList2String(tlinks,i),["}"],[""]);
                    string duration = llUnescapeURL(llList2String(ltemp,1));
                    times = times + [duration];
                    string link = llUnescapeURL(llList2String(ltemp,0));
                    list temp = llParseString2List(link,["/",".mp3"],[""]);
                    string title = llList2String(temp,llGetListLength(temp)-1);
                    if (llSubStringIndex(title,"_")!=-1)
                    {
                        while (llSubStringIndex(title,"_")!=-1)
                        {
                            integer index=llSubStringIndex(title,"_");
                            title=llDeleteSubString(title,index,index);
                            title=llInsertString(title,index," ");
                        }
                    }
                    titles = titles + [title];
                    links = links + [link];
                    integer num = i+1;
                    titlest += "\n("+(string)num+") "+title+ "  ("+llList2String(times,i)+")"; 
                }
                ++i;
            }    
            llInstantMessage(user,titlest);
        }
        else
        {
            llInstantMessage(user,"No results found!");
        }
        inuse=FALSE;
    }
}
state TOS
{
    state_entry()
    {
        llGiveInventory(llGetOwner(),"TOS");
        llListen(-2596,"","","");
        llDialog(llGetOwner(),"*Terms Of Service*\nYou MUST agree to these terms to use this object. By clicking I Agree, you agree to all terms listed in the provided notecard!",["I Agree","I Disagree"],-2596);
    }

    listen(integer chan, string name, key id, string mess)
    {
        if (mess=="I Agree")
        {
            llOwnerSay("Starting...please wait");
            llResetScript();
        }
        else
        {
            llOwnerSay("You must agree to the Terms Of Service! Deleting object!");
            llDie();
        }
    }
}

Floattext Script

integer spinning = FALSE;
default
{
    state_entry()
    {
        llTargetOmega(<0.0, 0.0, 1.0>, 0.9, 0.1);
    }

    link_message(integer s, integer num, string str, key id)
    {
        llSetText(str,<1,1,1>,1);
    }
}

MP3 Finder PHP

<?php
 include('snoopy.php');
 function getBetween($str, $start, $end)
 {
    $startlen = strlen($start);
    if (($startpos = strpos($str, $start)) !== false
    && ($endpos = strpos($str, $end)) !== false
    && ($skip = $startpos + $startlen) <= $endpos)
    {
        return substr($str, $skip, $endpos - $skip);
    }
    else
     {
         return false;
     }
 }
 
function is_available($url, $timeout = 10) {
    $ch = curl_init(); // get cURL handle
    // set cURL options
    $opts = array(CURLOPT_RETURNTRANSFER => true, // do not output to browser
                  CURLOPT_URL => $url,            // set URL
                  CURLOPT_NOBODY => true,           // do a HEAD request only
                  CURLOPT_TIMEOUT => $timeout);   // set timeout
    curl_setopt_array($ch, $opts); 
    curl_exec($ch); // do it!
    $retval = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200; // check if HTTP OK
    curl_close($ch); // close handle
    return $retval;
}
 
 function getEnd($string)
 {
    $length = strlen($string);
    $characters = 3;
    $start = $length - $characters;
    $string = substr($string , $start ,$characters);
    return $string; 
 }
 
 $snoopy = new Snoopy;
 $snoopy->agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
 $query = $_GET['query'];
 $query = str_replace(" ","+",$query);
 
 $snoopy->fetchlinks("http://skreemr.com/results.jsp?q=".$query);
 $links = ($snoopy->results);
 $contents = file_get_contents("http://skreemr.com/results.jsp?q=" . $query,NULL);
 preg_match_all("/mp3 - (.*) sec -/",$contents,$matches);
 $i=0;
 $times = $matches[0];
 $ch = curl_init(); // get cURL handle
 $timeout = 5;
 foreach ($links as $link)
 {
     if (getEnd($link)=="mp3")
     {
        // set cURL options
        $opts = array(CURLOPT_RETURNTRANSFER => true, // do not output to browser
                      CURLOPT_URL => $link,            // set URL
                      CURLOPT_NOBODY => true,           // do a HEAD request only
                      CURLOPT_TIMEOUT => $timeout);   // set timeout
        curl_setopt_array($ch, $opts);
        curl_exec($ch); // do it!
        $retval = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200; // check if HTTP OK
        if ($retval==1)
        {            
             $tduration = getBetween($times[$i],"mp3 - "," sec -");
             $tduration = explode(":",$tduration);
             if ($tduration[0]=="00")
             {
                 $duration = $tduration[1] . ":" . $tduration[2];
             }
             else
             {
                 $duration = $tduration;
             }
            echo $link . "}" . $duration .'{';
        }
        $i++;        
     }
 }
 curl_close($ch); // close handle
?>
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License