package ajax;

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



public class getLastfm {

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"));
vege=(thePage.indexOf("</object>",eleje));

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

return info;
*/

String info="";
int eleje,vege;
int mp3eleje;
if (!thePage.contains("id=\"lfmPlayer\"")) return "";

eleje=(thePage.indexOf("<div id=\"flashContainer\">"));
eleje+=37;
vege=(thePage.indexOf("</noscript>",eleje));


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

return info;
}



public String getLastfmPlayer(String band) {
String thePage="";
String thePlayer="";
try {

HttpURLConnection conn = null;

band = band.replace(" ","+");

URL url = new URL ("http://www.last.fm/music/"+band);

conn = (HttpURLConnection)url.openConnection();	

thePage=slurp(conn.getInputStream());
thePlayer= findPlayer(thePage);
}
catch(Exception e){
return "";
}

  return thePlayer;
  }
}