package ajax;

import java.net.*;
import java.io.*;



public class getMyspace {


public String slurp (InputStream in) throws IOException {
    StringBuffer out = new StringBuffer();
    byte[] b = new byte[4096];
    for (int n; (n = in.read(b)) != -1;) {
        out.append(new String(b, 0, n));
    }
    return out.toString();
}


public String findPlayer(String thePage){

String info="";
int eleje,vege;
int mp3eleje;

eleje=(thePage.indexOf("<OBJECT id=\"mp3player\""));
vege=(thePage.indexOf("</OBJECT>",eleje));
vege+=9;

info+=thePage.substring(eleje,vege);

return info;

}

public String findMyspace(String thePage){

String info="";
int eleje, vege;
eleje=thePage.indexOf(".myspace.com/");
eleje-=15; vege=eleje+130;
String linkString = thePage.substring(eleje,vege);

eleje = linkString.indexOf("\"http");
vege = linkString.indexOf("\"",eleje+1);

info+=linkString.substring(eleje+1,vege);


return info;
}



public String getMyspacePlayer(String band){



HttpURLConnection conn = null;
String thePage="";
String thePlayer="";
try{
band = band.replace(" ","+");

URL url = new URL ("http://www.google.com/search?q="+band+"+myspace");
conn = (HttpURLConnection)url.openConnection();	
conn.setRequestProperty("User-agent","Mozilla/4.0");

String myspacesite = findMyspace(slurp(conn.getInputStream()));


url = new URL (myspacesite);

conn = (HttpURLConnection)url.openConnection();	

thePage=slurp(conn.getInputStream());
thePlayer = findPlayer(thePage).replace("&a=0","&a=1");
}
catch(Exception e)
{
System.err.println(e);
return "";
}
return thePlayer;

}

}