]> gitweb.factorcode.org Git - factor.git/blob - extra/spider/spider.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[factor.git] / extra / spider / spider.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors fry html.parser html.parser.analyzer
4 http.client kernel tools.time sets assocs sequences
5 concurrency.combinators io threads namespaces math multiline
6 math.parser inspector urls logging combinators.short-circuit
7 continuations calendar prettyprint dlists deques locals
8 spider.unique-deque combinators concurrency.semaphores ;
9 IN: spider
10
11 TUPLE: spider base count max-count sleep max-depth initial-links
12 filters spidered todo nonmatching quiet currently-spidering
13 #threads semaphore follow-robots? robots ;
14
15 TUPLE: spider-result url depth headers
16 fetched-in parsed-html links processed-in fetched-at ;
17
18 : <spider> ( base -- spider )
19     >url
20     spider new
21         over >>base
22         over >>currently-spidering
23         swap 0 <unique-deque> [ push-url ] keep >>todo
24         <unique-deque> >>nonmatching
25         0 >>max-depth
26         0 >>count
27         1/0. >>max-count
28         H{ } clone >>spidered
29         1 [ >>#threads ] [ <semaphore> >>semaphore ] bi ;
30
31 : <spider-result> ( url depth -- spider-result )
32     spider-result new
33         swap >>depth
34         swap >>url ;
35
36 <PRIVATE
37
38 : apply-filters ( links spider -- links' )
39     filters>> [ '[ [ _ 1&& ] filter ] call( seq -- seq' ) ] when* ;
40
41 : push-links ( links level unique-deque -- )
42     '[ _ _ push-url ] each ;
43
44 : add-todo ( links level spider -- )
45     todo>> push-links ;
46
47 : add-nonmatching ( links level spider -- )
48     nonmatching>> push-links ;
49
50 : filter-base-links ( spider spider-result -- base-links nonmatching-links )
51     [ base>> host>> ] [ links>> prune ] bi*
52     [ host>> = ] with partition ;
53
54 : add-spidered ( spider spider-result -- )
55     [ [ 1 + ] change-count ] dip
56     2dup [ spidered>> ] [ dup url>> ] bi* rot set-at
57     [ filter-base-links ] 2keep
58     depth>> 1 + swap
59     [ add-nonmatching ]
60     [ tuck [ apply-filters ] 2dip add-todo ] 2bi ;
61
62 : normalize-hrefs ( base links -- links' )
63     [ derive-url ] with map ;
64
65 : print-spidering ( spider-result -- )
66     [ url>> ] [ depth>> ] bi
67     "depth: " write number>string write
68     ", spidering: " write . yield ;
69
70 :: fill-spidered-result ( spider spider-result -- )
71     f spider-result url>> spider spidered>> set-at
72     [ spider-result url>> http-get ] benchmark :> fetched-in :> html :> headers
73     [
74         html parse-html
75         spider currently-spidering>>
76         over find-all-links normalize-hrefs
77     ] benchmark :> processed-in :> links :> parsed-html
78     spider-result
79         headers >>headers
80         fetched-in >>fetched-in
81         parsed-html >>parsed-html
82         links >>links
83         processed-in >>processed-in
84         now >>fetched-at drop ;
85
86 :: spider-page ( spider spider-result -- )
87     spider quiet>> [ spider-result print-spidering ] unless
88     spider spider-result fill-spidered-result
89     spider quiet>> [ spider-result describe ] unless
90     spider spider-result add-spidered ;
91
92 \ spider-page ERROR add-error-logging
93
94 : spider-sleep ( spider -- ) sleep>> [ sleep ] when* ;
95
96 : queue-initial-links ( spider -- )
97     [
98         [ currently-spidering>> ] [ initial-links>> ] bi normalize-hrefs 0
99     ] keep add-todo ;
100
101 : spider-page? ( spider -- ? )
102     {
103         [ todo>> deque>> deque-empty? not ]
104         [ [ todo>> peek-url depth>> ] [ max-depth>> ] bi < ]
105         [ [ count>> ] [ max-count>> ] bi < ]
106     } 1&& ;
107
108 : setup-next-url ( spider -- spider spider-result )
109     dup todo>> peek-url url>> >>currently-spidering
110     dup todo>> pop-url [ url>> ] [ depth>> ] bi <spider-result> ;
111
112 : spider-next-page ( spider -- )
113     setup-next-url spider-page ;
114
115 PRIVATE>
116
117 : run-spider-loop ( spider -- )
118     dup spider-page? [
119         [ spider-next-page ] [ spider-sleep ] [ run-spider-loop ] tri
120     ] [
121         drop
122     ] if ;
123
124 : run-spider ( spider -- spider )
125     "spider" [
126         dup queue-initial-links [ run-spider-loop ] keep
127     ] with-logging ;