2 # $Id: robot.tcl,v 1.36 2003/06/10 11:55:18 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 {task area} {
57 # puts "RobotFileNext robotSeq=$robotSeq($task)"
58 if {$robotSeq($task) < 0} {
61 if {$robotSeq($task) == 0} {
62 if {[catch {set ns($task) [glob $task/$area/*]}]} {
66 # puts "ns=$ns($task)"
67 set off [string length $task/$area]
69 set n [lindex $ns($task) $robotSeq($task)]
71 if {![string length $n]} {
72 set robotSeq($task) -1
74 set statusfile [open $task/status w]
75 puts $statusfile "$status($task,unvisited) $status($task,bad) $status($task,visited)"
80 if {[file isfile $n/frobots.txt]} {
81 puts "ok returning http://[string range $n $off end]/robots.txt"
82 return http://[string range $n $off end]/robots.txt
83 } elseif {[file isdirectory $n]} {
84 set sb [RobotFileNext1 $n http://[string range $n $off end]]
85 if {[string length $sb]} {
89 puts "no more work at end of RobotFileNext n=$n"
95 proc RobotFileExist {task area host path} {
98 if {$debuglevel > 3} {
99 puts "RobotFileExist begin area=$area host=$host path=$path"
101 set lpath [split $path /]
102 set l [llength $lpath]
104 set t [lindex $lpath $l]
106 set npath $task/$area/$host[join [lrange $lpath 0 $l] /d]/f$t
107 if {$debuglevel > 3} {
108 puts "RobotFileExist end npath=$npath"
110 return [file exists $npath]
113 proc RobotFileUnlink {task area host path} {
115 # puts "RobotFileUnlink begin"
116 # puts "area=$area host=$host path=$path"
117 set lpath [split $path /]
118 set l [llength $lpath]
120 set t [lindex $lpath $l]
122 set npath $task/$area/$host[join [lrange $lpath 0 $l] /d]/f$t
123 # puts "npath=$npath"
124 set comp [split $npath /]
125 if {[catch {exec rm [join $comp /]}]} return
127 set l [llength $comp]
130 incr status($task,$area) -1
131 for {set i $l} {$i > 0} {incr i -1} {
132 set path [join [lrange $comp 0 $i] /]
133 if {![catch {glob $path/*}]} return
136 # puts "RobotFileUnlink end"
139 proc RobotFileClose {out} {
140 if [string compare $out stdout] {
145 proc RobotFileOpen {task area host path {mode w}} {
151 if {![info exists workdir]} {
154 if {$debuglevel > 3} {
155 puts "RobotFileOpen orgPwd=$orgPwd area=$area host=$host path=$path mode=$mode"
157 if {[string compare $orgPwd $workdir]} {
158 puts "ooops. RobotFileOpen failed"
159 puts "workdir = $workdir"
164 set comp [split $task/$area/$host /]
165 set len [llength $comp]
168 # puts "1 comp=$comp"
170 for {set i 0} {$i <= $len} {incr i} {
171 set d [lindex $comp $i]
172 if {[catch {cd $d}]} {
175 if {![string compare $area unvisited] && $i == $len && $mode == "w"} {
176 if {[string compare $path /robots.txt]} {
177 set out [open frobots.txt w]
178 puts "creating robots.txt in $d"
180 incr status($task,unvisited)
186 set comp [split $path /]
187 set len [llength $comp]
190 # puts "2 path=$path comp=$comp"
192 for {set i 0} {$i < $len} {incr i} {
193 set d "d[lindex $comp $i]"
194 if {[string length $d] > 1} {
195 if {[catch {cd $d}]} {
201 set d [lindex $comp $len]
202 if {[string length $d]} {
203 set out [open f$d $mode]
205 set out [open f $mode]
208 incr status($task,$area)
215 proc RobotStartJob {fname t} {
219 set f [open $fname r]
221 puts "Reading $fname"
222 regexp {<status>([^<]*)</status>} $xml x status
224 if {$status == "done"} {
228 puts "status = $status"
237 set action $parm(action)
238 if {$type == "domain"} {
239 $action url http://$body/*
241 if {$type == "url"} {
244 if {$type == "mime"} {
248 set control($t,distance) $body
250 set control($t,filestatus) $body
252 if {$status == "pending"} {
253 regsub {<status>[^<]*</status>} $xml {<status>running</status>} xml2
254 set f [open $fname w]
255 puts -nonewline $f $xml2
260 proc RobotDoneJob {t} {
263 if {![info exists daemon_dir]} {
269 set f [open $fname r]
271 puts "Reading $fname"
272 regexp {<status>([^<]*)</status>} $xml x status
274 puts "status = $status"
277 regsub {<status>[^<]*</status>} $xml {<status>done</status>} xml2
278 set f [open $fname w]
279 puts -nonewline $f $xml2
283 proc RobotScanDir {} {
286 if {![info exists daemon_dir]} {
289 foreach d $daemon_dir {
290 if {[catch {set files [glob $d/*.tkl]}]} {
293 foreach fname $files {
294 if {[file isfile $fname] && [file readable $fname]} {
295 set t [file rootname $fname]
296 RobotStartJob $fname $t
302 proc RobotRR {task} {
303 global robotSeq robotsRunning tasks robotsMax status
305 puts "RobotRR -- running=$robotsRunning max=$robotsMax---------------"
306 incr robotsRunning -1
308 # only one task gets through...
309 if {[string compare [lindex $tasks 0] $task]} {
312 puts "RobotRR. task = $task"
313 while {$robotsRunning} {
317 if {[catch {RobotScanDir} msg]} {
318 puts "RobotScanDir failed"
322 set statusfile [open $t/status w]
323 puts $statusfile "$status($t,unvisited) $status($t,bad) $status($t,visited)"
330 proc RobotDaemonSig {} {
336 proc RobotDaemonLoop {} {
337 global daemon_cnt tasks robotsRunning status
345 if {[info exists tasks]} {
346 puts "daemon loop tasks $tasks"
351 while {$robotsRunning} {
355 after 30000 RobotDaemonSig
360 proc RobotRestart {task url sock} {
361 global URL robotsRunning
364 after cancel $URL($sock,cancel)
366 foreach v [array names URL $task,$url,*] {
370 incr robotsRunning -1
374 proc RobotStart {task} {
376 global robotsRunning robotsMax idletime status tasks
378 # puts "RobotStart $task running=$robotsRunning"
380 set url [RobotFileNext $task unvisited]
381 if {[string compare $url done] == 0} {
382 puts "In RobotStart task $task done"
386 if {[string compare $t $task]} {
392 if {![info exists ntasks]} {
401 if {![string length $url]} {
405 if {[string compare $url wait] == 0} {
406 after $idletime [list RobotRR $task]
409 set r [RobotGetUrl $task $url {}]
411 if {$robotsRunning >= $robotsMax} return
413 incr robotsRunning -1
414 if {![RobotFileExist $task bad $URL($task,$url,hostport) $URL($task,$url,path)]} {
415 set outf [RobotFileOpen $task bad $URL($task,$url,hostport) $URL($task,$url,path)]
418 RobotFileUnlink $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)
423 proc headSave {task url out} {
426 if {[info exists URL($task,$url,head,last-modified)]} {
427 puts $out "<lastmodified>$URL($task,$url,head,last-modified)</lastmodified>"
430 if {[info exists URL($task,$url,head,date)]} {
431 puts $out " <date>$URL($task,$url,head,date)</date>"
433 if {[info exists URL($task,$url,head,content-length)]} {
434 puts $out " <by>$URL($task,$url,head,content-length)</by>"
436 if {[info exists URL($task,$url,head,server)]} {
437 puts $out " <format>$URL($task,$url,head,server)</format>"
440 puts $out {<publisher>}
441 puts $out " <identifier>$url</identifier>"
442 if {[info exists URL($task,$url,head,content-type)]} {
443 puts $out " <type>$URL($task,$url,head,content-type)</type>"
445 puts $out {</publisher>}
448 proc RobotHref {task url hrefx hostx pathx} {
449 global URL control debuglevel
454 if {$debuglevel > 1} {
455 puts "Ref input url = $url href=$href"
458 if {[string first { } $href] >= 0} {
461 if {[string length $href] > 256} {
465 # Skip pages that have ? in them
466 # if {[string first {?} $url] >= 0 && [string first {?} $href] >= 0} {
469 # get method (if any)
470 if {![regexp {^([^/:]+):(.*)} $href x method hpath]} {
474 if {[string compare $method http]} {
479 if {[regexp {^//([^/]+)([^\#]*)} $hpath x host surl]} {
480 if {![string length $surl]} {
483 if {[info exist control($task,domains)]} {
485 foreach domain $control($task,domains) {
486 if {[string match $domain $host]} {
496 regexp {^([^\#]*)} $hpath x surl
497 set host $URL($task,$url,hostport)
499 if {![string length $surl]} {
502 if {[string first / $surl]} {
504 set curpath $URL($task,$url,path)
505 if {[info exists URL($task,$url,bpath)]} {
506 set curpath $URL($task,$url,bpath)
508 regexp {^([^\#?]*)} $curpath x dpart
509 set l [string last / $dpart]
510 if {[expr $l >= 0]} {
511 set surl [string range $dpart 0 $l]$surl
513 set surl $dpart/$surl
516 set surllist [split $surl /]
519 foreach c $surllist {
524 set path [lrange $path 0 $pathl]
537 if {$debuglevel > 4} {
538 puts "pathl=$pathl output path=$path"
540 set path [join $path /]
541 if {![string length $path]} {
544 regsub -all {~} $path {%7E} path
545 set href "$method://$host$path"
547 if {$debuglevel > 1} {
548 puts "Ref result = $href"
550 return [checkrule $task url $href]
553 proc RobotError {task url code} {
556 puts "Bad URL $url (code $code)"
559 if {[RobotFileExist $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)]} {
560 set inf [RobotFileOpen $task unvisited $URL($task,$url,hostport) $URL($task,$url,path) r]
561 RobotReadRecord $inf fromurl distance
564 RobotFileUnlink $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)
565 if {![RobotFileExist $task bad $URL($task,$url,hostport) $URL($task,$url,path)]} {
566 set outf [RobotFileOpen $task bad $URL($task,$url,hostport) $URL($task,$url,path)]
567 RobotWriteRecord $outf $fromurl $distance
572 proc RobotRedirect {task url tourl code} {
575 puts "Redirecting from $url to $tourl"
579 if {[RobotFileExist $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)]} {
580 set inf [RobotFileOpen $task unvisited $URL($task,$url,hostport) $URL($task,$url,path) r]
581 RobotReadRecord $inf fromurl distance
584 if {![RobotFileExist $task bad $URL($task,$url,hostport) $URL($task,$url,path)]} {
585 set outf [RobotFileOpen $task bad $URL($task,$url,hostport) $URL($task,$url,path)]
586 RobotWriteRecord $outf $fromurl $distance
589 if {[RobotHref $task $url tourl host path]} {
590 if {![RobotFileExist $task visited $host $path]} {
591 if {![RobotFileExist $task unvisited $host $path]} {
592 set outf [RobotFileOpen $task unvisited $host $path]
593 RobotWriteRecord $outf $fromurl $distance
598 set inf [RobotFileOpen $task visited $host $path r]
599 RobotReadRecord $inf oldurl olddistance
601 if {[string length $olddistance] == 0} {
604 if {[string length $distance] == 0} {
607 puts "distance=$distance olddistance=$olddistance"
608 if {[expr $distance < $olddistance]} {
609 set outf [RobotFileOpen $task unvisited $host $path]
610 RobotWriteRecord $outf $tourl $distance
615 if {[catch {RobotFileUnlink $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)}]} {
621 proc link {task url out href body distance} {
623 if {[expr $distance > $control($task,distance)]} return
625 if {![RobotHref $task $url href host path]} return
628 puts $out "<identifier>$href</identifier>"
629 puts $out "<description>$body</description>"
632 if {![RobotFileExist $task visited $host $path]} {
634 if {![RobotFileExist $task bad $host $path]} {
635 if {[RobotFileExist $task unvisited $host $path]} {
636 set inf [RobotFileOpen $task unvisited $host $path r]
637 RobotReadRecord $inf oldurl olddistance
643 if {[string length $olddistance] == 0} {
646 if {[expr $distance < $olddistance]} {
647 set outf [RobotFileOpen $task unvisited $host $path]
648 RobotWriteRecord $outf $url $distance
651 } elseif {[string compare $href $url]} {
652 set inf [RobotFileOpen $task visited $host $path r]
653 RobotReadRecord $inf xurl olddistance
655 if {[string length $olddistance] == 0} {
658 if {[expr $distance < $olddistance]} {
659 puts "OK remarking url=$url href=$href"
660 puts "olddistance = $olddistance"
661 puts "newdistance = $distance"
662 set outf [RobotFileOpen $task unvisited $host $path]
663 RobotWriteRecord $outf $url $distance
669 proc RobotTextHtml {task url out} {
672 # set title so we can emit it for the body
674 # if true, nothing will be indexed
676 # if true, nothing will be followed
681 if {$control($task,distance) < 1000 && [info exists URL($task,$url,dist)]} {
682 set fdistance $URL($task,$url,dist)
683 set distance [expr $fdistance + 1]
685 htmlSwitch $URL($task,$url,buf) \
689 # collect metadata and save NAME= CONTENT=..
692 puts -nonewline $out "<meta"
693 set al [array names parm]
695 set al [string tolower $a]
696 puts -nonewline $out " $al"
697 puts -nonewline $out {="}
698 puts -nonewline $out $parm($a)
699 puts -nonewline $out {"}
702 set metaname [string tolower $parm($a)]
705 set metacontent $parm($a)
711 # go through robots directives (af any)
712 if {![string compare $metaname robots]} {
713 set direcs [split [string tolower $metacontent] ,]
714 if {[lsearch $direcs noindex] >= 0} {
717 if {[lsearch $direcs nofollow] >= 0} {
722 # don't print title of document content if noindex is used
724 puts $out "<title>$title</title>"
725 regsub -all {<!--[^-]*-->} $body { } abody
726 regsub -all -nocase {<script[^<]*</script>} $abody {} bbody
727 regsub -all {<[^\>]+>} $bbody {} nbody
728 puts $out "<documentcontent>"
730 puts $out "</documentcontent>"
734 if {![info exists parm(href)]} {
737 set href [string trim $parm(href)]
738 if {![RobotHref $task $url href host path]} continue
739 set URL($task,$url,bpath) $path
741 # <a href="...."> .. </a>
742 # we're not using nonest - otherwise body isn't set
743 if {$nofollow} continue
744 if {![info exists parm(href)]} {
747 link $task $url $out [string trim $parm(href)] $body $distance
749 if {$nofollow} continue
750 if {![info exists parm(href)]} {
753 link $task $url $out [string trim $parm(href)] $body $distance
755 if {![info exists parm(src)]} {
758 link $task $url $out [string trim $parm(src)] $body $fdistance
762 proc RobotsTxt {task url} {
765 RobotsTxt0 $task URL(URL($task,$url,hostport),robots) $URL($task,$url,buf)
768 proc RobotsTxt0 {task v buf} {
771 foreach l [split $buf \n] {
772 if {[regexp {([-A-Za-z]+):[ ]*([^\# ]+)} $l match cmd arg]} {
773 set arg [string trim $arg]
774 puts "cmd=$cmd arg=$arg"
775 switch -- [string tolower $cmd] {
778 set pat [string tolower $arg]*
779 set section [string match $pat $agent]
783 puts "rule [list 0 $arg]"
784 lappend $v [list 0 $arg]
789 puts "rule [list 1 $arg]"
790 lappend $v [list 1 $arg]
798 proc RobotTextPlain {task url out} {
801 puts $out "<documentcontent>"
802 regsub -all {<} $URL($task,$url,buf) {\<} content
804 puts $out "</documentcontent>"
806 if {![string compare $URL($task,$url,path) /robots.txt]} {
811 proc RobotWriteMetadata {task url out} {
817 if {[RobotFileExist $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)]} {
818 set inf [RobotFileOpen $task unvisited $URL($task,$url,hostport) $URL($task,$url,path) r]
819 RobotReadRecord $inf fromurl distance
822 set URL($task,$url,dist) $distance
823 puts $out "<distance>"
824 puts $out " $distance"
825 puts $out "</distance>"
826 headSave $task $url $out
827 puts "Parsing $url distance=$distance"
828 switch $URL($task,$url,head,content-type) {
830 if {[string length $distance]} {
831 RobotTextHtml $task $url $out
835 RobotTextPlain $task $url $out
841 proc Robot200 {task url} {
844 set out [RobotFileOpen $task raw $URL($task,$url,hostport) $URL($task,$url,path)]
845 puts -nonewline $out $URL($task,$url,buf)
848 set out [RobotFileOpen $task visited $URL($task,$url,hostport) $URL($task,$url,path)]
849 RobotWriteMetadata $task $url $out
852 RobotFileUnlink $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)
855 proc RobotReadContent {task url sock binary} {
858 set buffer [read $sock 16384]
859 set readCount [string length $buffer]
861 if {$readCount <= 0} {
863 RobotRestart $task $url $sock
864 } elseif {!$binary && [string first \0 $buffer] >= 0} {
866 RobotRestart $task $url $sock
868 # puts "Got $readCount bytes"
869 set URL($task,$url,buf) $URL($task,$url,buf)$buffer
873 proc RobotReadHeader {task url sock} {
874 global URL debuglevel
876 if {$debuglevel > 1} {
877 puts "HTTP head $url"
879 if {[catch {set buffer [read $sock 2148]}]} {
880 RobotError $task $url 404
881 RobotRestart $task $url $sock
884 set readCount [string length $buffer]
886 if {$readCount <= 0} {
887 RobotError $task $url 404
888 RobotRestart $task $url $sock
890 # puts "Got $readCount bytes"
891 set URL($task,$url,buf) $URL($task,$url,buf)$buffer
893 set n [string first \r\n\r\n $URL($task,$url,buf)]
897 set headbuf [string range $URL($task,$url,buf) 0 $n]
899 set URL($task,$url,buf) [string range $URL($task,$url,buf) $n end]
901 regexp {^HTTP/([0-9.]+)[ ]+([0-9]+)} $headbuf x version code
902 set lines [split $headbuf \n]
903 foreach line $lines {
904 if {[regexp {^([^:]+):[ ]+([^;]*)} $line x name value]} {
905 set URL($task,$url,head,[string tolower $name]) [string trim $value]
908 puts "HTTP CODE $code"
909 set URL($task,$url,state) skip
912 RobotRedirect $task $url $URL($task,$url,head,location) 301
913 RobotRestart $task $url $sock
916 RobotRedirect $task $url $URL($task,$url,head,location) 302
917 RobotRestart $task $url $sock
920 if {![info exists URL($task,$url,head,content-type)]} {
921 set URL($task,$url,head,content-type) {}
924 switch -glob -- $URL($task,$url,head,content-type) {
929 if {![regexp {/robots.txt$} $url]} {
930 if {![checkrule $task mime $URL($task,$url,head,content-type)]} {
931 RobotError $task $url mimedeny
932 RobotRestart $task $url $sock
936 fileevent $sock readable [list RobotReadContent $task $url $sock $binary]
939 RobotError $task $url $code
940 RobotRestart $task $url $sock
947 proc RobotSockCancel {task url sock} {
949 puts "RobotSockCancel sock=$sock url=$url"
950 RobotError $task $url 401
951 RobotRestart $task $url $sock
954 proc RobotConnect {task url sock} {
955 global URL agent acceptLanguage
957 fconfigure $sock -translation {lf crlf} -blocking 0
958 fileevent $sock readable [list RobotReadHeader $task $url $sock]
959 puts $sock "GET $URL($task,$url,path) HTTP/1.0"
960 puts $sock "Host: $URL($task,$url,host)"
961 puts $sock "User-Agent: $agent"
962 if {[string length $acceptLanguage]} {
963 puts $sock "Accept-Language: $acceptLanguage"
966 set URL($sock,cancel) [after 30000 [list RobotSockCancel $task $url $sock]]
967 if {[catch {flush $sock}]} {
968 RobotError $task $url 404
969 RobotRestart $task $url $sock
977 proc RobotGetUrl {task url phost} {
978 global URL robotsRunning
980 puts "Retrieve running=$robotsRunning url=$url task=$task"
981 if {![regexp {([^:]+)://([^/]+)(.*)} $url x method hostport path]} {
984 if {![regexp {([^:]+):([0-9]+)} $hostport x host port]} {
988 set URL($task,$url,method) $method
989 set URL($task,$url,host) $host
990 set URL($task,$url,hostport) $hostport
991 set URL($task,$url,path) $path
992 set URL($task,$url,state) head
993 set URL($task,$url,buf) {}
995 if {[string compare $path /robots.txt]} {
997 if {![info exists URL($hostport,robots)]} {
998 puts "READING robots.txt for host $hostport"
999 if {[RobotFileExist $task visited $hostport /robots.txt]} {
1000 set inf [RobotFileOpen $task visited $hostport /robots.txt r]
1001 set buf [read $inf 32768]
1004 set buf "User-agent: *\nAllow: /\n"
1006 RobotsTxt0 $task URL($hostport,robots) $buf
1008 if {[info exists URL($hostport,robots)]} {
1009 foreach l $URL($hostport,robots) {
1010 if {[string first [lindex $l 1] $path] == 0} {
1011 set ok [lindex $l 0]
1017 puts "skipped due to robots.txt"
1021 if [catch {set sock [socket -async $host $port]}] {
1024 RobotConnect $task $url $sock
1029 if {![llength [info commands htmlSwitch]]} {
1030 set e [info sharedlibextension]
1031 if {[catch {load ./tclrobot$e}]} {
1036 set agent "zmbot/0.2"
1037 if {![catch {set os [exec uname -s -r]}]} {
1038 set agent "$agent ($os)"
1041 puts "agent: $agent"
1052 set acceptLanguage {}
1055 # Rules: allow, deny, url
1057 proc checkrule {task type this} {
1061 if {$debuglevel > 3} {
1062 puts "CHECKRULE $type $this"
1064 if {[info exist control($task,alrules)]} {
1065 foreach l $control($task,alrules) {
1066 if {$debuglevel > 3} {
1070 if {[lindex $l 1] != $type} continue
1071 # consider mask (! negates)
1072 set masks [lindex $l 2]
1074 foreach mask $masks {
1075 if {$debuglevel > 4} {
1076 puts "consider single mask $mask"
1078 if {[string index $mask 0] == "!"} {
1079 set mask [string range $mask 1 end]
1080 if {[string match $mask $this]} continue
1082 if {![string match $mask $this]} continue
1086 if {$debuglevel > 4} {
1090 # OK, we have a match
1091 if {[lindex $l 0] == "allow"} {
1092 if {$debuglevel > 3} {
1093 puts "CHECKRULE MATCH OK"
1097 if {$debuglevel > 3} {
1098 puts "CHECKFULE MATCH FAIL"
1104 if {$debuglevel > 3} {
1105 puts "CHECKRULE MATCH OK"
1112 global debuglevel task
1114 if {[RobotHref $task http://www.indexdata.dk/ href host path]} {
1115 if {![RobotFileExist $task visited $host $path]} {
1116 set outf [RobotFileOpen $task unvisited $host $path]
1117 RobotWriteRecord $outf href 0
1118 RobotFileClose $outf
1123 proc deny {type stuff} {
1126 lappend control($task,alrules) [list deny $type $stuff]
1129 proc allow {type stuff} {
1132 lappend control($task,alrules) [list allow $type $stuff]
1135 proc debug {level} {
1138 set debuglevel $level
1142 global tasks task status robotSeq control
1146 if {[info exists tasks]} {
1147 if {[lsearch -exact $tasks $t] >= 0} {
1153 set status($t,unvisited) 0
1154 set status($t,visited) 0
1155 set status($t,bad) 0
1156 set status($t,raw) 0
1157 set status($t,active) 1
1159 set control($t,distance) 10
1163 # Little utility that ensures that at least one task is present (main).
1166 if {![info exist tasks]} {
1175 set l [llength $argv]
1178 puts {tclrobot: usage:}
1179 puts {tclrobot [-j jobs] [-i idle] [-c count] [-d domain] [-D dir] [-r rules] [url ..]}
1180 puts " Example: -c 3 -d '*.dk' http://www.indexdata.dk/"
1187 set arg [lindex $argv $i]
1188 switch -glob -- $arg {
1190 set t [string range $arg 2 end]
1191 if {![string length $t]} {
1192 set t [lindex $argv [incr i]]
1197 set dir [string range $arg 2 end]
1198 if {![string length $dir]} {
1199 set dir [lindex $argv [incr i]]
1201 lappend daemon_dir $dir
1204 set robotsMax [string range $arg 2 end]
1205 if {![string length $robotsMax]} {
1206 set robotsMax [lindex $argv [incr i]]
1211 set control($task,distance) [string range $arg 2 end]
1212 if {![string length $control($task,distance)]} {
1213 set control($task,distance) [lindex $argv [incr i]]
1218 set dom [string range $arg 2 end]
1219 if {![string length $dom]} {
1220 set dom [lindex $argv [incr i]]
1222 lappend control($task,domains) $dom
1225 set idletime [string range $arg 2 end]
1226 if {![string length $idletime]} {
1227 set idletime [lindex $argv [incr i]]
1232 set acceptLanguage [string range $arg 2 end]
1233 if {![string length $acceptLanguage]} {
1234 set acceptLanguage [lindex $argv [incr i]]
1239 set rfile [string range $arg 2 end]
1240 if {![string length $rfile]} {
1241 set rfile [lindex $argv [incr i]]
1243 catch {unset maxdistance}
1245 if {[info exists maxdistance]} {
1246 set control($task,distance) $maxdistance
1252 if {[RobotHref $task http://www.indexdata.dk/ href host path]} {
1253 if {![RobotFileExist $task visited $host $path]} {
1254 set outf [RobotFileOpen $task unvisited $host $path]
1255 RobotWriteRecord $outf href 0
1256 RobotFileClose $outf
1264 if {![info exist robotsMax]} {
1268 if {[info exist daemon_dir]} {
1273 puts "max distance=$control($t,distance)"
1274 if {[info exists control($t,domains)]} {
1275 puts "domains=$control($t,domains)"
1278 puts "max jobs=$robotsMax"
1284 while {$robotsRunning} {
1288 if {[info exists tasks]} {
1290 set statusfile [open $t/status w]
1291 puts $statusfile "$status($t,unvisited) $status($t,bad) $status($t,visited)"