martes, 19 de enero de 2010

Codigo fuente para buscar lyrics automatico en php

El codigo busca la letra de la cancion en un buscador, este caso google ( se puede cambiar ) , y este devuelve unos resultados que a su vez son CURL'eados, osea que se lee la web de la letra. Esta en PHP y usa la funcion LoadCURLPage para extraer el texto de una direccion.
En esta web http://yujuuu.com/ podeis ver como se busca la letra y la presenta como texto.
Si no queda claro cuelgo el codigo fuente completo. Le he agreado muchas variables para controlar errores.

  
// FUNCIONES
error_reporting (E_ALL ^ E_NOTICE);
function LoadCURLPage($url, 



$cookie = '', $referer = '', $post_fields = '', $return_transfer = 0,
$follow_location = 0, $ssl = '', $curlopt_header = 0){
        $ch = curl_init();
       
        curl_setopt($ch, CURLOPT_URL, $url);
       
        if($ssl)
        {
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
        }
       
        curl_setopt ($ch, CURLOPT_HEADER, $curlopt_header);
       
        if($agent)
        {
        curl_setopt($ch, CURLOPT_USERAGENT, $agent);
        }
       
        if($post_fields)
        {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
        }
       
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
       
        if($referer)
        {
        curl_setopt($ch, CURLOPT_REFERER, $referer);
        }
       
        if($cookie)
        {
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
        }
       
        $result = curl_exec ($ch);
       
        curl_close ($ch);
       
        return $result;
}

function entre2cad($data,$string_one,$string_two,$inclusive,$vuelta)
{
if (strlen($data) < strlen($string_one))
{
$sub=0;
}
else
{
$pos=strpos($data, $string_one);
if ($vuelta>0)
{
while ($vuelta!=0 and $pos !== 0 )
{
$pos=strpos($data, $string_one,$pos+1);
$vuelta=$vuelta-1;
}
}


if ($inclusive==1 and $data !== NULL)
{
$data=substr($data,$pos);
$pos2 = strpos($data, $string_two,2);

$sub=substr($data,0,$pos2+strlen($string_two));
}
else
{
$data=substr($data,$pos+strlen($string_one));
$pos2 = strpos($data, $string_two);
$sub=substr($data,0,$pos2);
}
if ($pos==null or $sub==null)
{
$sub=0;
}
}
return $sub;
}

function unhtmlentities($string)
{
    // replace numeric entities
    $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
    $string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string);
    // replace literal entities
    $trans_tbl = get_html_translation_table(HTML_ENTITIES);
    $trans_tbl = array_flip($trans_tbl);
    return strtr($string, $trans_tbl);
}





// Conectar a GOOGLE por el lyryc
$quero="nombre de la cancion"
$lyrycs=0;
$vueltas=0;
$canciones = array();
$index=0;
$url=0;
$data=0;
while ($vueltas<4 and $index != 3)
{
$pasada=0;

if ($vueltas===1)
{
$url = 'http://www.google.com/search?hl=en&q='.$quero.'+site:lyricsfreak.com';
}
if ($vueltas===0)
{
$url = 'http://www.google.com/search?hl=en&q='.$quero.'+site:musica.com';
}
if ($vueltas===2)
{
$url = 'http://www.google.com/search?hl=en&q='.$quero.'+site:lyricsdownload.com';
}
if ($vueltas===3)
{
$url = 'http://www.google.com/search?hl=en&q='.$quero.'+site:elyrics.net';
}
$data = LoadCURLPage($url);
$data=htmlentities($data);

while ($pasada <  3 and $index != 3)
{

$enlace= entre2cad($data,'<li class=g><h3 class=r><a href="/url?q=','">',0,$pasada);
if ($enlace === 0)
{
$enlace= entre2cad($data,'<li class=g><h3 class=r><a href="','" class=l',0,$pasada);
}

//Buscar la letra en la pagina
$data = LoadCURLPage($enlace);
//echo $enlace;
$data=htmlentities($data);
if ($vueltas===1)
{

$lyrycs= entre2cad($data, 'lang="en">','</div',0,0);
}

if ($vueltas===0)
{
$lyrycs= entre2cad($data, '<pre','/pre>',1,0);
if ($lyrycs===0)
{
$lyrycs= entre2cad($data, 'verdana>','</font',0,0);
}
}

if ($vueltas===2)
{
$lyrycs= entre2cad($data, 'id="div_customCSS">','</div',0,0);
}

if ($vueltas===3)
{
$lyrycs= entre2cad($data, '</strong><br><br>','<br><br></div>',0,0);
}
    if ($lyrycs!==0)
    {
  
    $canciones[$index]=$lyrycs;
    $index=$index+1;
    }

$pasada=$pasada+1;
}
$vueltas= $vueltas+1;
}

for ($i = 1; $i <= 3; $i++) {
    $letra = $canciones[$i-1];

$letra=nl2br(str_replace('<pre><font style=line-height:20px;font-size:14px;font-family:arial,tahoma,verdana>','',$letra));
$letra=str_replace('<pre style="line-height: 19px">','',$letra);
$letra=str_replace('</pre>','',$letra);
echo '

'.unhtmlentities($letra).'
';
          
            }

No hay comentarios: