2 # $Id: robot.tcl,v 1.17 2001/06/07 08:10:10 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
110 # puts "npath=$npath"
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 length $href] > 256} {
270 if {[string first {?} $url] >= 0 && [string first {?} $href] >= 0} {
273 # get method (if any)
274 if {![regexp {^([^/:]+):(.*)} $href x method hpath]} {
278 if {[string compare $method http]} {
283 if {[regexp {^//([^/]+)([^\#]*)} $hpath x host surl]} {
284 if {![string length $surl]} {
287 if {[info exist domains]} {
289 foreach domain $domains {
290 if {[string match $domain $host]} {
300 regexp {^([^\#]*)} $hpath x surl
301 set host $URL($url,hostport)
303 if {![string length $surl]} {
306 if {[string first / $surl]} {
308 regexp {^([^\#?]*)} $URL($url,path) x dpart
309 set l [string last / $dpart]
310 if {[expr $l >= 0]} {
311 set surl [string range $dpart 0 $l]$surl
313 set surl $dpart/$surl
316 set surllist [split $surl /]
319 foreach c $surllist {
324 set path [lrange $path 0 $pathl]
337 set path [join $path /]
341 regsub -all {~} $path {%7E} path
342 set href "$method://$host$path"
343 puts "Ref href = $href"
347 proc RobotError {url code} {
350 puts "Bad URL $url, $code"
353 if {[RobotFileExist unvisited $URL($url,hostport) $URL($url,path)]} {
354 set inf [RobotFileOpen unvisited $URL($url,hostport) $URL($url,path) r]
355 RobotReadRecord $inf fromurl distance
358 RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)
359 if {![RobotFileExist bad $URL($url,hostport) $URL($url,path)]} {
360 set outf [RobotFileOpen bad $URL($url,hostport) $URL($url,path)]
361 RobotWriteRecord $outf $fromurl $distance
366 proc RobotRedirect {url tourl code} {
369 puts "Redirecting from $url to $tourl"
373 if {[RobotFileExist unvisited $URL($url,hostport) $URL($url,path)]} {
374 set inf [RobotFileOpen unvisited $URL($url,hostport) $URL($url,path) r]
375 RobotReadRecord $inf fromurl distance
378 if {![RobotFileExist bad $URL($url,hostport) $URL($url,path)]} {
379 set outf [RobotFileOpen bad $URL($url,hostport) $URL($url,path)]
380 RobotWriteRecord $outf $fromurl $distance
383 if {[RobotHref $url tourl host path]} {
384 if {![RobotFileExist visited $host $path]} {
385 if {![RobotFileExist unvisited $host $path]} {
386 set outf [RobotFileOpen unvisited $host $path]
387 RobotWriteRecord $outf $fromurl $distance
392 set inf [RobotFileOpen visited $host $path r]
393 RobotReadRecord $inf oldurl olddistance
395 if {[string length $olddistance] == 0} {
398 if {[string length $distance] == 0} {
401 puts "distance=$distance olddistance=$olddistance"
402 if {[expr $distance < $olddistance]} {
403 set outf [RobotFileOpen unvisited $host $path]
404 RobotWriteRecord $outf $tourl $distance
409 if {[catch {RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)}]} {
415 proc RobotTextHtml {url out} {
416 global URL maxDistance
419 if {$maxDistance < 1000 && [info exists URL($url,dist)]} {
420 set distance [expr $URL($url,dist) + 1]
422 htmlSwitch $URL($url,buf) \
424 puts $out "<title>$body</title>"
426 puts -nonewline $out "<meta"
427 foreach a [array names parm] {
428 puts -nonewline $out " $a"
429 puts -nonewline $out {="}
430 puts -nonewline $out $parm($a)
431 puts -nonewline $out {"}
435 regsub -all -nocase {<script([^<]|(<!.*>))*</script>} $body {} abody
436 regsub -all {<[^\>]+>} $abody {} nbody
437 puts $out "<documentcontent>"
439 puts $out "</documentcontent>"
441 if {![info exists parm(href)]} {
445 if {[expr $distance <= $maxDistance]} {
446 set href [string trim $parm(href)]
447 if {![RobotHref $url href host path]} continue
450 puts $out "<identifier>$href</identifier>"
451 puts $out "<description>$body</description>"
454 if {![RobotFileExist visited $host $path]} {
456 if {![RobotFileExist bad $host $path]} {
457 if {[RobotFileExist unvisited $host $path]} {
458 set inf [RobotFileOpen unvisited $host $path r]
459 RobotReadRecord $inf oldurl olddistance
465 if {[string length $olddistance] == 0} {
468 if {[expr $distance < $olddistance]} {
469 set outf [RobotFileOpen unvisited $host $path]
470 RobotWriteRecord $outf $url $distance
473 } elseif {[string compare $href $url]} {
474 set inf [RobotFileOpen visited $host $path r]
475 RobotReadRecord $inf xurl olddistance
477 if {[string length $olddistance] == 0} {
480 if {[expr $distance < $olddistance]} {
481 puts "OK remarking url=$url href=$href"
482 puts "olddistance = $olddistance"
483 puts "newdistance = $distance"
484 set outf [RobotFileOpen unvisited $host $path]
485 RobotWriteRecord $outf $url $distance
491 if {![info exists parm(href)]} {
495 if {[expr $distance <= $maxDistance]} {
496 set href [string trim $parm(href)]
497 if {![RobotHref $url href host path]} continue
500 puts $out "<identifier>$href</identifier>"
501 puts $out "<description></description>"
504 if {![RobotFileExist visited $host $path]} {
506 if {![RobotFileExist bad $host $path]} {
507 if {[RobotFileExist unvisited $host $path]} {
508 set inf [RobotFileOpen unvisited $host $path r]
509 RobotReadRecord $inf oldurl olddistance
515 if {[string length $olddistance] == 0} {
518 if {[expr $distance < $olddistance]} {
519 set outf [RobotFileOpen unvisited $host $path]
520 RobotWriteRecord $outf $url $distance
523 } elseif {[string compare $href $url]} {
524 set inf [RobotFileOpen visited $host $path r]
525 RobotReadRecord $inf xurl olddistance
527 if {[string length $olddistance] == 0} {
530 if {[expr $distance < $olddistance]} {
531 puts "OK remarking url=$url href=$href"
532 puts "olddistance = $olddistance"
533 puts "newdistance = $distance"
534 set outf [RobotFileOpen unvisited $host $path]
535 RobotWriteRecord $outf $url $distance
543 proc RobotsTxt {url} {
546 RobotsTxt0 URL(URL($url,hostport),robots) $URL($url,buf)
549 proc RobotsTxt0 {v buf} {
552 foreach l [split $buf \n] {
553 if {[regexp {([-A-Za-z]+):[ \t]*([^\#\t ]+)} $l match cmd arg]} {
554 puts "cmd=$cmd arg=$arg"
555 switch -- [string tolower $cmd] {
558 set pat [string tolower $arg]*
559 set section [string match $pat $agent]
563 puts "rule [list 0 $arg]"
564 lappend $v [list 0 $arg]
569 puts "rule [list 1 $arg]"
570 lappend $v [list 1 $arg]
578 proc RobotTextPlain {url out} {
581 puts $out "<documentcontent>"
582 puts $out $URL($url,buf)
583 puts $out "</documentcontent>"
585 if {![string compare $URL($url,path) /robots.txt]} {
590 proc Robot200 {url} {
593 set out [RobotFileOpen raw $URL($url,hostport) $URL($url,path)]
594 puts -nonewline $out $URL($url,buf)
597 set out [RobotFileOpen visited $URL($url,hostport) $URL($url,path)]
601 if {[RobotFileExist unvisited $URL($url,hostport) $URL($url,path)]} {
602 set inf [RobotFileOpen unvisited $URL($url,hostport) $URL($url,path) r]
603 RobotReadRecord $inf fromurl distance
606 set URL($url,dist) $distance
607 puts $out "<distance>"
608 puts $out " $distance"
609 puts $out "</distance>"
611 puts "Parsing $url distance=$distance"
612 switch $URL($url,head,content-type) {
614 if {[string length $distance]} {
615 RobotTextHtml $url $out
619 RobotTextPlain $url $out $outr
622 set pdff [open test.pdf w]
623 puts -nonewline $pdff $URL($url,buf)
629 # puts "Parsing done"
630 RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)
633 proc RobotReadContent {url sock binary} {
636 puts "RobotReadContent $url"
637 set buffer [read $sock 16384]
638 set readCount [string length $buffer]
640 if {$readCount <= 0} {
642 RobotRestart $url $sock
643 } elseif {!$binary && [string first \0 $buffer] >= 0} {
645 RobotRestart $url $sock
647 # puts "Got $readCount bytes"
648 set URL($url,buf) $URL($url,buf)$buffer
652 proc RobotReadHeader {url sock} {
655 puts "RobotReadHeader $url"
656 if {[catch {set buffer [read $sock 2148]}]} {
658 RobotRestart $url $sock
660 set readCount [string length $buffer]
662 if {$readCount <= 0} {
664 RobotRestart $url $sock
666 # puts "Got $readCount bytes"
667 set URL($url,buf) $URL($url,buf)$buffer
669 set n [string first \r\n\r\n $URL($url,buf)]
673 set headbuf [string range $URL($url,buf) 0 $n]
675 set URL($url,buf) [string range $URL($url,buf) $n end]
677 regexp {^HTTP/([0-9.]+)[ ]+([0-9]+)} $headbuf x version code
678 set lines [split $headbuf \n]
679 foreach line $lines {
680 if {[regexp {^([^:]+):[ ]+([^;]*)} $line x name value]} {
681 set URL($url,head,[string tolower $name]) [string trim $value]
685 set URL($url,state) skip
688 RobotRedirect $url $URL($url,head,location) 301
689 RobotRestart $url $sock
692 RobotRedirect $url $URL($url,head,location) 302
693 RobotRestart $url $sock
696 if {![info exists URL($url,head,content-type)]} {
697 set URL($url,head,content-type) {}
700 switch $URL($url,head,content-type) {
705 fileevent $sock readable [list RobotReadContent $url $sock $binary]
708 RobotError $url $code
709 RobotRestart $url $sock
716 proc RobotSockCancel {url sock} {
718 puts "RobotSockCancel sock=$sock url=$url"
720 RobotRestart $url $sock
723 proc RobotConnect {url sock} {
726 fconfigure $sock -translation {lf crlf} -blocking 0
727 fileevent $sock readable [list RobotReadHeader $url $sock]
728 puts $sock "GET $URL($url,path) HTTP/1.0"
729 puts $sock "Host: $URL($url,host)"
730 puts $sock "User-Agent: $agent"
733 set URL($sock,cancel) [after 30000 [list RobotSockCancel $url $sock]]
740 proc RobotGetUrl {url phost} {
741 global URL robotsRunning
743 puts "RobotGetUrl --------- robotsRunning=$robotsRunning url=$url"
744 if {![regexp {([^:]+)://([^/]+)(.*)} $url x method hostport path]} {
747 if {![regexp {([^:]+):([0-9]+)} $hostport x host port]} {
751 set URL($url,method) $method
752 set URL($url,host) $host
753 set URL($url,hostport) $hostport
754 set URL($url,path) $path
755 set URL($url,state) head
758 if {[string compare $path /robots.txt]} {
760 if {![info exists URL($hostport,robots)]} {
761 puts "READING robots.txt for host $hostport"
762 if {[RobotFileExist visited $hostport /robots.txt]} {
763 set inf [RobotFileOpen visited $hostport /robots.txt r]
764 set buf [read $inf 32768]
767 set buf "User-agent: *\nAllow: /\n"
769 RobotsTxt0 URL($hostport,robots) $buf
771 if {[info exists URL($hostport,robots)]} {
772 foreach l $URL($hostport,robots) {
773 if {[string first [lindex $l 1] $path] == 0} {
780 puts "skipped due to robots.txt"
784 if [catch {set sock [socket -async $host $port]}] {
787 RobotConnect $url $sock
792 if {![llength [info commands htmlSwitch]]} {
793 set e [info sharedlibextension]
794 if {[catch {load ./tclrobot$e}]} {
799 set agent "zmbot/0.0"
800 if {![catch {set os [exec uname -s -r]}]} {
801 set agent "$agent ($os)"
818 set l [llength $argv]
822 set url "http://www.sportsfiskeren.dk/sportsfiskeren/corner/index.htm"
823 set href "../../data/../../data2/newsovs.asp?Mode=5"
825 set URL($url,path) /sportsfiskeren/corner/index.htm
826 set URL($url,hostport) www.sportsfiskeren.dk
827 RobotHref $url href host path
832 puts {tclrobot: usage [-j jobs] [-i idle] [-c count] [-d domain] [url ..]}
833 puts " Example: -c 3 -d '*.dk' http://www.indexdata.dk/"
838 set arg [lindex $argv $i]
839 switch -glob -- $arg {
841 set robotsMax [string range $arg 2 end]
842 if {![string length $robotsMax]} {
843 set robotsMax [lindex $argv [incr i]]
847 set maxDistance [string range $arg 2 end]
848 if {![string length $maxDistance]} {
849 set maxDistance [lindex $argv [incr i]]
853 set dom [string range $arg 2 end]
854 if {![string length $dom]} {
855 set dom [lindex $argv [incr i]]
860 set idleTime [string range $arg 2 end]
861 if {![string length $idleTime]} {
862 set idleTime [lindex $argv [incr i]]
867 if {[RobotHref http://www.indexdata.dk/ href host path]} {
868 if {![RobotFileExist visited $host $path]} {
869 set outf [RobotFileOpen unvisited $host $path]
870 RobotWriteRecord $outf href 0
879 if {![info exist domains]} {
882 if {![info exist maxDistance]} {
885 if {![info exist robotsMax]} {
889 puts "domains=$domains"
890 puts "max distance=$maxDistance"
891 puts "max jobs=$robotsMax"
895 while {$robotsRunning} {