Foros del Web » Programando para Internet » PHP »

Problema con robot.

Estas en el tema de Problema con robot. en el foro de PHP en Foros del Web. Buenas, espero se encuentren bien. Estuve navegando por la web y encontre algo que hace mucho estaba por crear y lo encontre. Es un spider ...
  #1 (permalink)  
Antiguo 23/02/2007, 21:08
 
Fecha de Ingreso: noviembre-2006
Ubicación: Salinas
Mensajes: 216
Antigüedad: 17 años, 5 meses
Puntos: 0
De acuerdo Problema con robot.

Buenas, espero se encuentren bien.

Estuve navegando por la web y encontre algo que hace mucho estaba por crear y lo encontre. Es un spider o robot, pero lo deseo utilizar con unas imagenes (fondos de escritorio).

Pero tiene errores y no encuentro como corregirlos, si me pueden dar una mano, o decirme donde esta el error, gracias.

spider.php
Código PHP:
<?php
    
// -----------------------------------------------------
    //
    //  Author:                                        WiZ2
    //  E-Mail:                          [email protected]
    //  Date (YYYY/MM/DD):                       2002-09-22
    //  Revision (YYYY/MM/DD):                   2002-09-22
    //  Version:                                  0.1 ALPHA
    //    
    // -----------------------------------------------------
    //  REQUIERE class.url.php
    //  EJEMPLO:
    //
    //         $savepath = "down/blizzard/";
    //         $indomain = true;
    //         $rewrite = false;
    //         $reclevel = 2;    
    //         $rpath = "ghost/";
    //         evaluate("www.blizzard.com/ghost/index.shtml");
    //
    //   php.exe spider.php
    //   subst i: c:\scr\down\blizzard
    //
    //   i:\index.html
    //
    // NOTA:
    //   No es para nada recomendable usar este spider con
    //   páginas dinámicas
    //
    // PARA HACER:
    // -----------
    //  - Que se puedan obtener ficheros html dinámicos con parámetros
    //    ej. index.php?sec=1
    //  - Arreglar algún que otro bug del programa
    //  - Ponerle funcionalidad
    //  - Hacer un gui con GTK y otros
    // -----------------------------------------------------

    
require_once("class.url.php");
    
    
// Extensiones válidas para los ficheros    
    
$valid_ext =
        array (
        
"htm*",        
        
"?htm*",
        
"php",
        
"php*",
        
"gif",
        
"jpg",
        
"js",
        
"css",
        
"png"
        
);

    
// Extensiones válidas para los ficheros con urls
    
$html_ext =
        array (
        
"htm*",
        
"?htm*",
        
"php",
        
"js",
        
"css",
        
"php*"
        
);

    
$evaluated = array();
    
    
$savepath "down/";
    
    function 
makedir($path) {
        
$path str_replace("\\""/"$path);
        for (
$n 0$n strlen($path); $n++) {
            if (
substr($path$n1) == "/") {
                if (!
is_dir(substr($path0$n))) {
                    
mkdir(substr($path0$n), 667);
                }
            }
        }
    }    

    function 
validPattern($pattern$txt) {
        
$stpos 0$next 0;
        for (
$n 0$n strlen($pattern); $n++) {
            switch (
substr($pattern$n1)) {
                case 
"?"$stpos++; break;
                case 
"*"$next 1$stpos++; break;
                default:
                    if (
$next == 1)     {
                        for (;
$stpos strlen($txt);$stpos++) {                            
                            if (
substr($pattern$n1) == substr($txt$stpos1)) { $stpos++; break; }
                        }
                        
$next 0;
                    } else {                                                
                        if (
substr($pattern$n1) != substr($txt$stpos1)) return false;
                        
$stpos++;
                    }
            }
            if (
$stpos strlen($txt)) return false;
        }
        return 
true;
    }
    
    function 
filefilter($txt) {
        global 
$valid_ext;
        if (
strpos($txt"<") == "" && strpos($txt">") == "")    {
            if (
strpos($txt"{") == "" && strpos($txt"}") == "")    {
                if (
strpos($txt".") != "") {
                    
$url1 = new url($txt);                                        
                    for (
$n 0$n sizeof($valid_ext); $n++) {                        
                        if (
validPattern("." $valid_ext[$n], $url1->getExtension())) {
                            return 
true;
                            break;
                        }
                    }
                    unset(
$url1);
                }
            }
        }
        return 
false;
    }
    
    function 
parseHtml($urlD$level 0) {
        global 
$savepath$indomain$rewrite;        
        echo 
str_repeat(" "$level)  . "[Loading... $urlD]";
        
$level++;
        
$url = new url($urlD);
        if (
$html $url->download()) {                                                                
            echo 
" {\n";
            
$tags explode("\""$html);
            for (
$n2 0$n2 sizeof($tags); $n2++) {
                
$tagk explode("'"str_replace(chr(9), ""trim($tags[$n2])));                
                for (
$n 0$n sizeof($tagk); $n++) {                        
                    
$tag $tagk[$n];    
                    if (
filefilter($tag)) {
                        
$ret $url->getAbsolute(new url($tag));
                        if (
$indomain) {
                            if (
$url->inDomain($ret)) evaluate($ret->getUrl(), $level);
                        } else {
                            
evaluate($ret->getUrl(), $level);
                        }
                        unset(
$ret);
                    }
                }                                
            }            
            echo 
str_repeat(" "$level)  . "Saving... " $savepath $url->getUri();
            
makedir($savepath $url->getPath());                    
            if (
$rewrite) {
                
fwrite($fd fopen($savepath $url->getUri(), "w+"), $html); fclose($fd);            
                echo 
" - Ok\n}\n";
            } else {
                if (!
file_exists($savepath $url->getUri())) {
                    
fwrite($fd fopen($savepath $url->getUri(), "w+"), $html); fclose($fd);            
                    echo 
" - Ok\n}\n";
                } else {
                    echo 
" - Ok(DR FE)\n";
                }
            }            
        } else {
            echo 
" - Error\n";
        }
    }
    
    function 
evaluate($url$level 0) {
        global 
$evaluated$html_ext$savepath$reclevel$rpath$rewrite;
        
        
// Condición para aceptar la petición
        
$pu = new url($url);
        
        if (!
in_array(strtolower($url), $evaluated)) {
            
array_push($evaluatedstrtolower($url));                                
                        
            
$ok 0;
            for (
$n 0$n sizeof($html_ext); $n++) {                
                if (
validPattern("." $html_ext[$n], $pu->getExtension()) && $level $reclevel) {                                    
                    if (
validPattern("$rpath*"$pu->getUri())) {
                        
parseHtml($url$level);
                        
$ok 1; break;
                    }
                }
            }
            if (
$ok == 0) {
                echo 
str_repeat(" "$level)  . "Saving... " $savepath $pu->getUri();
                if (
$pu->exists($pu)) {                    
                    
makedir($savepath $pu->getPath());
                    if (
$rewrite) {
                        
$pu->save($savepath $pu->getUri());
                        echo 
" - Ok\n";
                    } else {
                        if (!
file_exists($savepath $pu->getUri())) {
                            
$pu->save($savepath $pu->getUri());
                            echo 
" - Ok\n";
                        } else {
                            echo 
" - Ok(DR FE)\n";
                        }
                    }                    
                } else {
                    echo 
" - Error(FNE)\n";
                }
            }
            
            
//write_file("sample.out", "$url\n", "a");            
        
}
    }

    
$savepath "down/ppt/";
    
$indomain true;
    
$rewrite false;
    
$reclevel 2;    
    
$rpath "";
    
evaluate("www.unawebqquiera.com");

?>
  #2 (permalink)  
Antiguo 23/02/2007, 21:08
 
Fecha de Ingreso: noviembre-2006
Ubicación: Salinas
Mensajes: 216
Antigüedad: 17 años, 5 meses
Puntos: 0
Re: Problema con robot.


class.url.php

Código PHP:
<?php
    
// -----------------------------------------------------
    //
    //  Author:                                        WiZ2
    //  E-Mail:                          [email protected]
    //  Date (YYYY/MM/DD):                       2002-08-26
    //  Revision (YYYY/MM/DD):                   2002-09-22
    //  Version:                                        1.5
    //    
    // -----------------------------------------------------
    //
    // -----------------------------------------------------
    //  Class:
    //
    //        void        = url          (string $url)
    //        void        = parseUrl     (string $url)
    //
    //        string      = getProtocol  ();
    //        string      = getServer    ();
    //        string      = getFileA     ();
    //        string      = getFile      ();
    //        string      = getPath      ();
    //        string      = getArgs      ();
    //        string      = getUriA      ();
    //        string      = getUrl       ();
    //        string      = getExtension ();
    //
    //        object url  = getAbsolute  (object url);
    //        bool        = inDomain     (object url);
    //        string      = getParent    (string path);
    //
    //        void        = setProtocol  (string protocol);
    //        void        = setServer    (string server);
    //        void        = setPath      (string path);
    //        void        = setFile      (string file);
    //        void        = setArgs      (string args);
    //
    //        bool        = download     ();
    //        bool        = save         (string path);
    //        bool        = exists       ();
    //
    // -----------------------------------------------------
    //  Other:
    //
    //  Functions to write & read files:
    //
    //        bool        = write_file   (string path, string txt, [mode = w|w+|a|a+]);
    //        string      = read_file    (string path, [mode = r]);
    //
    // -----------------------------------------------------
    //
    //
    // ----------------
    // 1.5 - 2002-09-22
    //
    //   - Add function inDomain to class
    //   - Add function exists to class
    // ----------------
    // 1.3 - 2002-09-22
    //
    //   - Add function download to class
    //   - Add function save to class
    //   - Add function getExtension to class
    //   - Add write_file to script
    //   - Add read_file to script
    //
    // ----------------
    // 1.0 - 2002-08-26
    //
    //   - First release
    //

    
class url {

        
// Internal variables
        
var $protocol;
        var 
$server;
        var 
$path;
        var 
$file;
        var 
$fileargs;

        
// Get functions
        
function getProtocol () { return $this->protocol; }
        function 
getServer () { return $this->server; }
        function 
getFileA () { return $this->file; }
        function 
getFile () { return $this->file $this->fileargs; }
        function 
getPath () { return $this->path; }
        function 
getArgs () { return $this->fileargs; }
        function 
getUriA () { return $this->path $this->file; }
        function 
getUri () { return $this->path $this->file $this->fileargs; }
        function 
getUrl () { return $this->protocol $this->server $this->path $this->file $this->fileargs; }
        function 
getExtension() { return substr($this->filestrpos($this->file".")); }
        function 
getAbsolute ($objUri) {
            if (
strlen ($objUri->getServer()) == 0) {
                
$act $objUri->getPath ();
                
$frm $this->getPath ();
                if (
substr ($act01) == "/"$frm "";
                
$act explode ("/"$act);
                for (
$n 0$n sizeof ($act); $n++) {
                    if (
strlen ($act[$n]) > 0) {
                        if (
$act[$n] == "..") {
                            
$frm $this->getParent ($frm);
                        } elseif (
$act[$n] != ".") {
                            if (
strpos ($act[$n], ".") != "" && $n == sizeof ($act) - 1) {
                                
$frm .= $act[$n];
                            } else {
                                
$frm .= $act[$n] . "/";
                            }
                        }
                    }
                }
                return (new 
url ($this->protocol $this->server $frm $objUri->getFile()));
            } else {
                return (
$objUri);
            }
        }
        function 
inDomain ($objUri) { return ($objUri->server == $this->server); }

        
// Set functions
        
function setProtocol ($protocol) { $this->protocol $protocol; }
        function 
setServer ($server) { $this->server $server; }
        function 
setPath ($path) { $this->path $path; }
        function 
setFile ($file) { $this->file $file; }
        function 
setArgs ($args) { $this->fileargs $args; }

        
// Parse functions
        
function parseURL ($url) {
            
$this->protocol "http://"$this->server "";
            
$this->path ""$this->file ""$this->fileargs "";

            
$act 0;
            for (
$n 0$n strlen ($url); $n++) {
                
$c substr ($url$n1);
                if (
substr ($url$n3) == "://") {
                    if (
$act == 0) {
                        
$this->protocol $buffer "://";
                        
$buffer ""$act 1$n+=2;
                    } else {
                        
$buffer .= $c;
                    }
                } elseif (
$c == "/") {
                    
$buffer .= $c;
                    if (
strpos ($buffer".") != ""$act 1;
                    if (
$act == 0) {
                        
$this->path .= $buffer$buffer "";
                    } elseif (
$act == 1) {
                        
$this->server $buffer;
                        
$act 0$buffer "";
                    }
                } elseif (
$c == "?") {
                    if (
$act == 0) {
                        
$this->file $buffer;
                        
$act 2$buffer "";
                    } else {
                        
$buffer .= $c;
                    }
                } else {
                    
$buffer .= $c;
                }
            }
            if (
strlen ($buffer) > 0) {
                if (
$act == 2) {
                    
$this->fileargs "?" $buffer;
                } elseif (
$act == 0) {
                    if (
strpos ($buffer".") != "") {
                        
$this->file $buffer;
                    } else {
                        
$this->path .= $buffer "/";
                    }
                }
            }
        }

        
// Internal functions
        
function getParent ($url) {
            if (
substr ($urlstrlen ($url) - 11) == "/"$url substr ($url0strlen ($url) - 1);
            return 
substr ($url0strrpos ($url"/") + 1);
        }
        
        
// Hacks
        
        
function download() {
            
$url $this->getUrl();
            if (
trim(strtolower($this->getProtocol())) == "file://"$url substr($url7);            
            
$code "";
            if (
$fd = @fopen($url"r")) {                
                while (!
feof($fd)) $code .= fread($fd40960);                
                
fclose($fd);
            }
            return 
$code;
        }
        
        function 
save($file) {            
            
$url $this->getUrl();        
            if (
trim(strtolower($this->getProtocol())) == "file://"$url substr($url7);            
            if (
$fd = @fopen($url"r")) {
                if (
$fd2 = @fopen($file"w+")) {
                    
$code "";            
                    while (!
feof($fd)) fwrite($fd2fread($fd40960));                                    
                    
fclose($fd2);
                    return 
true;
                }
                
fclose($fd);
            }            
            return 
false;
        }

        function 
exists() {
            
//ob_start();
            
$url $this->getUrl();        
            if (
trim(strtolower($this->getProtocol())) == "file://"$url substr($url7);
            if (
$fd = @fopen($url"r")) {
                
fclose($fd);
                return 
true;
            } else {
                return 
false;
            }
            
//ob_end_clean();
        
}
               

        
// Constructor
        
function url ($url) { parseURL ($url); }
    }
    
    function 
write_file($path$txt$mode "w+") {
        if (
$fd = @fopen($path$mode)) {
            
fwrite($fd$txt);
            
fclose($fd);
            return 
true;
        } else {
            return 
false;
        }
    }
    
    function 
read_file($path$mode "r") {
        
$code "";
        if (
$fd = @fopen($path$mode)) {            
            while(!
feof($fd)) $code .= fread($fd409600);
            
fclose($fd);            
        }
        return 
$code;        
    }
?>
  #3 (permalink)  
Antiguo 24/02/2007, 19:00
 
Fecha de Ingreso: noviembre-2006
Ubicación: Salinas
Mensajes: 216
Antigüedad: 17 años, 5 meses
Puntos: 0
Re: Problema con robot.

Realmente necesito ayuda si me pueden ayudar.
  #4 (permalink)  
Antiguo 28/02/2007, 19:53
Avatar de DeeR  
Fecha de Ingreso: diciembre-2003
Ubicación: Santiago
Mensajes: 520
Antigüedad: 20 años, 4 meses
Puntos: 17
Re: Problema con robot.

No he mirado la Clase que has posteado xD

Segun lo que veo , queres un bot spiderweb que navege en paginas web, capture las URL de las Imagenes, y luego guarde las imagenes en algun directorio local (como dices wallpaper, con la condicino que la imagen sea mayor a 640x480).

Aqui tengo un SpiderWeb Bot en PHP, pero orientado a Navegar en Sitios Web, en los cuales captura las palabras y las URL, las palabras les calculo su md5 y la guardo en una bd, las URL tambien las guardo, para navegar en ellas despues.

Lo que puedes hacer, es ver mi script, luego analizar dentro de la clase el $this->html (el cual contiene el codigo html de la pagina) , lo que tienes que realizar es parsear las URL de las imagenes utilizando Expresiones Regulares (hay tengo unos Tips para capturar Datos). Ya que tienes el URL de la Imagen (tiene que ser la URL real, es decir http://servidor/images/foto.jpg ) , tendras que abrir esa URL y guardarla (copy() permite copiar archivos remotos desde php4.3.0) tambien tendras que ver que tenga una dimension minima la foto (googlea para eso) antes de copiarla.

Saludos ojala te sirve la idea.
Mas rato analizare la Clase que posteaste.

Saludos
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:50.