2 # $Id: robot.tcl,v 1.12 2001/01/23 14:28:41 adam Exp $
4 proc RobotFileNext1 {area lead} {
5 puts "RobotFileNext1 area=$area lead=$lead"
6 if {[catch {set ns [glob ${area}/*]}]} {
10 if {[file isfile $n]} {
11 set off [string last / $n]
13 return $lead/[string range $n $off end]
17 if {[file isdirectory $n]} {
18 set off [string last / $n]
20 set sb [RobotFileNext1 $n $lead/[string range $n $off end]]
21 if {[string length $sb]} {
29 proc RobotWriteRecord {outf fromurl distance} {
31 puts $outf "<distance>"
33 puts $outf "</distance>"
34 puts $outf "<fromurl>"
36 puts $outf "</fromurl>"
40 proc RobotReadRecord {inf fromurlx distancex} {
41 upvar $fromurlx fromurl
42 upvar $distancex distance
45 set distance [string trim [gets $inf]]
46 puts "got distance = $distance"
49 set fromurl [string trim [gets $inf]]
52 proc RobotFileNext {area} {
53 global robotSeq global idleTime ns
55 puts "RobotFileNext robotSeq=$robotSeq"
60 if {[catch {set ns [glob ${area}/*]}]} {
64 set off [string length $area]
66 set n [lindex $ns $robotSeq]
67 if {![string length $n]} {
70 puts "------------ N E X T R O U N D --------"
74 if {[file isfile $n/frobots.txt]} {
75 puts "ok returning http://[string range $n $off end]/robots.txt"
76 return http://[string range $n $off end]/robots.txt
77 } elseif {[file isdirectory $n]} {
78 set sb [RobotFileNext1 $n http://[string range $n $off end]]
79 if {[string length $sb]} {
83 puts "no more work at end of RobotFileNext n=$n"
89 proc RobotFileExist {area host path} {
90 puts "RobotFileExist begin area=$area host=$host path=$path"
91 set lpath [split $path /]
92 set l [llength $lpath]
94 set t [lindex $lpath $l]
96 set npath $area/$host[join [lrange $lpath 0 $l] /d]/f$t
97 puts "RobotFileExist end npath=$npath"
98 return [file exists $npath]
101 proc RobotFileUnlink {area host path} {
102 puts "RobotFileUnlink begin"
103 puts "area=$area host=$host path=$path"
104 set lpath [split $path /]
105 set l [llength $lpath]
107 set t [lindex $lpath $l]
109 set npath $area/$host[join [lrange $lpath 0 $l] /d]/f$t
111 set comp [split $npath /]
112 set l [llength $comp]
114 if {[catch {exec rm [join $comp /]}]} return
116 for {set i $l} {$i > 0} {incr i -1} {
117 set path [join [lrange $comp 0 $i] /]
118 if {![catch {glob $path/*}]} return
121 puts "RobotFileUnlink end"
124 proc RobotFileClose {out} {
125 if [string compare $out stdout] {
130 proc RobotFileOpen {area host path {mode w}} {
134 if {![info exists workdir]} {
137 puts "RobotFileOpen orgPwd=$orgPwd area=$area host=$host path=$path mode=$mode"
138 if {[string compare $orgPwd $workdir]} {
139 puts "ooops. RobotFileOpen failed"
140 puts "workdir = $workdir"
144 set comp [split $area/$host$path /]
145 set len [llength $comp]
147 for {set i 0} {$i < $len} {incr i} {
149 set d "d[lindex $comp $i]"
151 set d [lindex $comp $i]
153 if {[catch {cd ./$d}]} {
156 if {![string compare $area unvisited] && $i == 1 && $mode == "w"} {
157 set out [open frobots.txt w]
158 puts "creating robots.txt in $d"
163 set d [lindex $comp $len]
164 if {[string length $d]} {
165 if {[file isdirectory $d]} {
166 set out [open $d/f $mode]
168 set out [open f$d $mode]
171 set out [open f $mode]
178 global robotSeq robotsRunning
180 incr robotsRunning -1
181 while {$robotsRunning} {
188 proc RobotRestart {url sock} {
189 global URL robotsRunning
192 after cancel $URL($sock,cancel)
194 foreach v [array names URL $url,*] {
198 incr robotsRunning -1
204 global robotsRunning robotsMax idleTime
208 set url [RobotFileNext unvisited]
209 if {![string length $url]} {
213 if {[string compare $url wait] == 0} {
214 after $idleTime RobotRR
217 set r [RobotGetUrl $url {}]
219 if {$robotsRunning >= $robotsMax} return
221 incr robotsRunning -1
222 if {![RobotFileExist bad $URL($url,hostport) $URL($url,path)]} {
223 set outf [RobotFileOpen bad $URL($url,hostport) $URL($url,path)]
226 RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)
231 proc headSave {url out} {
234 if {[info exists URL($url,head,last-modified)]} {
235 puts $out "<lastmodified>$URL($url,head,last-modified)</lastmodified>"
238 if {[info exists URL($url,head,date)]} {
239 puts $out " <date>$URL($url,head,date)</date>"
241 if {[info exists URL($url,head,content-length)]} {
242 puts $out " <by>$URL($url,head,content-length)</by>"
244 if {[info exists URL($url,head,server)]} {
245 puts $out " <format>$URL($url,head,server)</format>"
248 puts $out {<publisher>}
249 puts $out " <identifier>$url</identifier>"
250 if {[info exists URL($url,head,content-type)]} {
251 puts $out " <type>$URL($url,head,content-type)</type>"
253 puts $out {</publisher>}
256 proc RobotHref {url hrefx hostx pathx} {
262 puts "Ref url = $url href=$href"
264 if {[string first { } $href] >= 0} {
267 if {[string first {?} $url] >= 0 && [string first {?} $href] >= 0} {
270 # get method (if any)
271 if {![regexp {^([^/:]+):(.*)} $href x method hpath]} {
275 if {[string compare $method http]} {
280 if {[regexp {^//([^/]+)([^\#]*)} $hpath x host surl]} {
281 if {![string length $surl]} {
284 if {[info exist domains]} {
286 foreach domain $domains {
287 if {[string match $domain $host]} {
297 regexp {^([^\#]*)} $hpath x surl
298 set host $URL($url,hostport)
300 if {![string length $surl]} {
303 if {[string first / $surl]} {
305 regexp {^([^\#?]*)} $URL($url,path) x dpart
306 set l [string last / $dpart]
307 if {[expr $l >= 0]} {
308 set surl [string range $dpart 0 $l]$surl
310 set surl $dpart/$surl
313 set c [split $surl /]
316 set path [lindex $c $i]
319 switch -- [lindex $c $i] {
330 set path [lindex $c $i]/$path
335 regsub -all {~} $path {%7E} path
336 set href "$method://$host$path"
337 puts "Ref href = $href"
341 proc RobotError {url code} {
344 puts "Bad URL $url, $code"
347 if {[RobotFileExist unvisited $URL($url,hostport) $URL($url,path)]} {
348 set inf [RobotFileOpen unvisited $URL($url,hostport) $URL($url,path) r]
349 RobotReadRecord $inf fromurl distance
352 RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)
353 if {![RobotFileExist bad $URL($url,hostport) $URL($url,path)]} {
354 set outf [RobotFileOpen bad $URL($url,hostport) $URL($url,path)]
355 RobotWriteRecord $outf $fromurl $distance
360 proc RobotRedirect {url tourl code} {
363 puts "Redirecting from $url to $tourl"
367 if {[RobotFileExist unvisited $URL($url,hostport) $URL($url,path)]} {
368 set inf [RobotFileOpen unvisited $URL($url,hostport) $URL($url,path) r]
369 RobotReadRecord $inf fromurl distance
372 if {![RobotFileExist bad $URL($url,hostport) $URL($url,path)]} {
373 set outf [RobotFileOpen bad $URL($url,hostport) $URL($url,path)]
374 RobotWriteRecord $outf $fromurl $distance
377 if {[RobotHref $url tourl host path]} {
378 if {![RobotFileExist visited $host $path]} {
379 if {![RobotFileExist unvisited $host $path]} {
380 set outf [RobotFileOpen unvisited $host $path]
381 RobotWriteRecord $outf $fromurl $distance
386 set inf [RobotFileOpen visited $host $path r]
387 RobotReadRecord $inf oldurl olddistance
389 if {[string length $olddistance] == 0} {
392 if {[string length $distance] == 0} {
395 puts "distance=$distance olddistance=$olddistance"
396 if {[expr $distance < $olddistance]} {
397 set outf [RobotFileOpen unvisited $host $path]
398 RobotWriteRecord $outf $tourl $distance
403 if {[catch {RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)}]} {
409 proc RobotTextHtml {url out} {
410 global URL maxDistance
413 if {$maxDistance < 1000 && [info exists URL($url,dist)]} {
414 set distance [expr $URL($url,dist) + 1]
416 htmlSwitch $URL($url,buf) \
418 puts $out "<title>$body</title>"
420 puts -nonewline $out "<meta"
421 foreach a [array names parm] {
422 puts -nonewline $out " $a"
423 puts -nonewline $out {="}
424 puts -nonewline $out $parm($a)
425 puts -nonewline $out {"}
429 regsub -all -nocase {<script.*</script>} $body {} abody
430 regsub -all {<[^\>]+>} $abody {} nbody
431 puts $out "<documentcontent>"
433 puts $out "</documentcontent>"
435 if {![info exists parm(href)]} {
439 if {[expr $distance <= $maxDistance]} {
440 set href [string trim $parm(href)]
441 if {![RobotHref $url href host path]} continue
444 puts $out "<identifier>$href</identifier>"
445 puts $out "<description>$body</description>"
448 if {![RobotFileExist visited $host $path]} {
450 if {![RobotFileExist bad $host $path]} {
451 if {[RobotFileExist unvisited $host $path]} {
452 set inf [RobotFileOpen unvisited $host $path r]
453 RobotReadRecord $inf oldurl olddistance
459 if {[string length $olddistance] == 0} {
462 if {[expr $distance < $olddistance]} {
463 set outf [RobotFileOpen unvisited $host $path]
464 RobotWriteRecord $outf $url $distance
467 } elseif {[string compare $href $url]} {
468 set inf [RobotFileOpen visited $host $path r]
469 RobotReadRecord $inf xurl olddistance
471 if {[string length $olddistance] == 0} {
474 if {[expr $distance < $olddistance]} {
475 puts "OK remarking url=$url href=$href"
476 puts "olddistance = $olddistance"
477 puts "newdistance = $distance"
478 set outf [RobotFileOpen unvisited $host $path]
479 RobotWriteRecord $outf $url $distance
487 proc RobotsTxt {url} {
490 RobotsTxt0 URL(URL($url,hostport),robots) $URL($url,buf)
493 proc RobotsTxt0 {v buf} {
496 foreach l [split $buf \n] {
497 if {[regexp {([-A-Za-z]+):[ \t]*([^\#]+)} $l match cmd arg]} {
498 puts "cmd=$cmd arg=$arg"
502 set pat [string tolower $arg]*
503 set section [string match $pat $agent]
507 puts "rule [list 0 $arg]"
508 lappend $v [list 0 $arg]
513 puts "rule [list 1 $arg]"
514 lappend $v [list 1 $arg]
522 proc RobotTextPlain {url out} {
525 puts $out "<documentcontent>"
526 puts $out $URL($url,buf)
527 puts $out "</documentcontent>"
529 if {![string compare $URL($url,path) /robots.txt]} {
534 proc Robot200 {url} {
537 set out [RobotFileOpen visited $URL($url,hostport) $URL($url,path)]
541 if {[RobotFileExist unvisited $URL($url,hostport) $URL($url,path)]} {
542 set inf [RobotFileOpen unvisited $URL($url,hostport) $URL($url,path) r]
543 RobotReadRecord $inf fromurl distance
546 set URL($url,dist) $distance
547 puts $out "<distance>"
548 puts $out " $distance"
549 puts $out "</distance>"
551 puts "Parsing $url distance=$distance"
552 switch $URL($url,head,content-type) {
554 if {[string length $distance]} {
555 RobotTextHtml $url $out
559 RobotTextPlain $url $out
562 set pdff [open test.pdf w]
563 puts -nonewline $pdff $URL($url,buf)
569 # puts "Parsing done"
570 RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)
573 proc RobotReadContent {url sock binary} {
576 puts "RobotReadContent $url"
577 set buffer [read $sock 16384]
578 set readCount [string length $buffer]
580 if {$readCount <= 0} {
582 RobotRestart $url $sock
583 } elseif {!$binary && [string first \0 $buffer] >= 0} {
585 RobotRestart $url $sock
587 # puts "Got $readCount bytes"
588 set URL($url,buf) $URL($url,buf)$buffer
592 proc RobotReadHeader {url sock} {
595 puts "RobotReadHeader $url"
596 if {[catch {set buffer [read $sock 2148]}]} {
598 RobotRestart $url $sock
600 set readCount [string length $buffer]
602 if {$readCount <= 0} {
604 RobotRestart $url $sock
606 # puts "Got $readCount bytes"
607 set URL($url,buf) $URL($url,buf)$buffer
609 set n [string first \r\n\r\n $URL($url,buf)]
613 set headbuf [string range $URL($url,buf) 0 $n]
615 set URL($url,buf) [string range $URL($url,buf) $n end]
617 regexp {^HTTP/([0-9.]+)[ ]+([0-9]+)} $headbuf x version code
618 set lines [split $headbuf \n]
619 foreach line $lines {
620 if {[regexp {^([^:]+):[ ]+(.*)} $line x name value]} {
621 set URL($url,head,[string tolower $name]) [string trim $value]
625 set URL($url,state) skip
628 RobotRedirect $url $URL($url,head,location) 301
629 RobotRestart $url $sock
632 RobotRedirect $url $URL($url,head,location) 302
633 RobotRestart $url $sock
636 if {![info exists URL($url,head,content-type)]} {
637 set URL($url,head,content-type) {}
640 switch $URL($url,head,content-type) {
645 fileevent $sock readable [list RobotReadContent $url $sock $binary]
648 RobotError $url $code
649 RobotRestart $url $sock
656 proc RobotSockCancel {url sock} {
658 puts "RobotSockCancel sock=$sock url=$url"
660 RobotRestart $url $sock
663 proc RobotConnect {url sock} {
666 fconfigure $sock -translation {lf crlf} -blocking 0
667 fileevent $sock readable [list RobotReadHeader $url $sock]
668 puts $sock "GET $URL($url,path) HTTP/1.0"
669 puts $sock "Host: $URL($url,host)"
670 puts $sock "User-Agent: $agent"
673 set URL($sock,cancel) [after 30000 [list RobotSockCancel $url $sock]]
680 proc RobotGetUrl {url phost} {
681 global URL robotsRunning
683 puts "RobotGetUrl --------- robotsRunning=$robotsRunning url=$url"
684 if {![regexp {([^:]+)://([^/]+)(.*)} $url x method hostport path]} {
687 if {![regexp {([^:]+):([0-9]+)} $hostport x host port]} {
691 set URL($url,method) $method
692 set URL($url,host) $host
693 set URL($url,hostport) $hostport
694 set URL($url,path) $path
695 set URL($url,state) head
698 if {[string compare $path /robots.txt]} {
700 if {![info exists URL($hostport,robots)]} {
701 puts "READING robots.txt for host $hostport"
702 if {[RobotFileExist visited $hostport /robots.txt]} {
703 set inf [RobotFileOpen visited $hostport /robots.txt r]
704 set buf [read $inf 32768]
707 set buf "User-Agent: *\nAllow: /\n"
709 RobotsTxt0 URL($hostport,robots) $buf
711 if {[info exists URL($hostport,robots)]} {
712 foreach l $URL($hostport,robots) {
713 if {[string first [lindex $l 1] $path] == 0} {
723 if [catch {set sock [socket -async $host $port]}] {
726 RobotConnect $url $sock
731 if {![llength [info commands htmlSwitch]]} {
732 set e [info sharedlibextension]
733 if {[catch {load ./tclrobot$e}]} {
738 set agent "zmbot/0.0"
739 if {![catch {set os [exec uname -s -r]}]} {
740 set agent "$agent ($os)"
757 set l [llength $argv]
760 puts {tclrobot: usage [-j jobs] [-c count] [-d domain] [url ..]}
761 puts " Example: -c 3 -d '*.dk' http://www.indexdata.dk/"
766 set arg [lindex $argv $i]
767 switch -glob -- $arg {
769 set robotsMax [string range $arg 2 end]
770 if {![string length $robotsMax]} {
771 set robotsMax [lindex $argv [incr i]]
775 set maxDistance [string range $arg 2 end]
776 if {![string length $maxDistance]} {
777 set maxDistance [lindex $argv [incr i]]
781 set dom [string range $arg 2 end]
782 if {![string length $dom]} {
783 set dom [lindex $argv [incr i]]
789 if {[RobotHref http://www.indexdata.dk/ href host path]} {
790 if {![RobotFileExist visited $host $path]} {
791 set outf [RobotFileOpen unvisited $host $path]
792 RobotWriteRecord $outf href 0
801 if {![info exist domains]} {
804 if {![info exist maxDistance]} {
807 if {![info exist robotsMax]} {
811 puts "domains=$domains"
812 puts "max distance=$maxDistance"
813 puts "max jobs=$robotsMax"
817 while {$robotsRunning} {