]> gitweb.factorcode.org Git - factor.git/blob - extra/spider/spider.factor
cb30fb6a29dd7851c37fad46b2c9d7dd526ddd4c
[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 io.pathnames ;
10 IN: spider
11
12 TUPLE: spider
13     base
14     { count integer initial: 0 }
15     { max-count number initial: 1/0. }
16     sleep
17     { max-depth integer initial: 0 }
18     initial-links
19     filters
20     spidered
21     todo
22     nonmatching
23     quiet?
24     currently-spidering
25     { #threads integer initial: 1 }
26     semaphore
27     follow-robots?
28     robots ;
29
30 TUPLE: spider-result url depth headers
31 fetched-in parsed-html links processed-in fetched-at ;
32
33 : <spider> ( base -- spider )
34     >url
35     spider new
36         over >>base
37         over >>currently-spidering
38         swap 0 <unique-deque> [ push-url ] keep >>todo
39         <unique-deque> >>nonmatching
40         H{ } clone >>spidered
41         1 <semaphore> >>semaphore ;
42
43 : <spider-result> ( url depth -- spider-result )
44     spider-result new
45         swap >>depth
46         swap >>url ; inline
47
48 <PRIVATE
49
50 : apply-filters ( links spider -- links' )
51     filters>> [
52         '[ [ _ 1&& ] filter ] call( seq -- seq' )
53     ] when* ;
54
55 : push-links ( links level unique-deque -- )
56     '[ _ _ push-url ] each ;
57
58 : add-todo ( links level spider -- )
59     todo>> push-links ;
60
61 : add-nonmatching ( links level spider -- )
62     nonmatching>> push-links ;
63
64 : filter-base-links ( spider spider-result -- base-links nonmatching-links )
65     [ base>> host>> ] [ links>> members ] bi*
66     [ host>> = ] with partition ;
67
68 :: add-spidered ( spider spider-result -- )
69     spider [ 1 + ] change-count drop
70
71     spider-result dup url>>
72     spider spidered>> set-at
73
74     spider spider-result filter-base-links :> ( matching nonmatching )
75     spider-result depth>> 1 + :> depth
76
77     nonmatching depth spider add-nonmatching
78
79     matching spider apply-filters depth spider add-todo ;
80
81 : normalize-hrefs ( base links -- links' )
82     [ derive-url ] with map ;
83
84 : print-spidering ( spider-result -- )
85     [ url>> ] [ depth>> ] bi
86     "depth: " write number>string write
87     ", spidering: " write . yield ;
88
89 : url-html? ( url -- ? )
90     path>> file-extension { ".htm" ".html" f } member? ;
91
92 :: fill-spidered-result ( spider spider-result -- )
93     spider-result url>> :> url
94     f url spider spidered>> set-at
95     [ url http-get ] benchmark :> ( headers html fetched-in )
96     [
97         url url-html? [
98             html parse-html
99             spider currently-spidering>>
100             over find-all-links normalize-hrefs
101         ] [
102             f { }
103         ] if
104     ] benchmark :> ( parsed-html links processed-in )
105     spider-result
106         headers >>headers
107         fetched-in >>fetched-in
108         parsed-html >>parsed-html
109         links >>links
110         processed-in >>processed-in
111         now >>fetched-at drop ;
112
113 :: spider-page ( spider spider-result -- )
114     spider quiet?>> [ spider-result print-spidering ] unless
115     spider spider-result fill-spidered-result
116     spider quiet?>> [ spider-result describe ] unless
117     spider spider-result add-spidered ;
118
119 \ spider-page ERROR add-error-logging
120
121 : spider-sleep ( spider -- ) sleep>> [ sleep ] when* ;
122
123 : queue-initial-links ( spider -- spider )
124     [ [ currently-spidering>> ] [ initial-links>> ] bi normalize-hrefs 0 ]
125     [ add-todo ]
126     [ ] tri ;
127
128 : spider-page? ( spider -- ? )
129     {
130         [ todo>> deque>> deque-empty? not ]
131         [ [ todo>> peek-url depth>> ] [ max-depth>> ] bi <= ]
132         [ [ count>> ] [ max-count>> ] bi < ]
133     } 1&& ;
134
135 : setup-next-url ( spider -- spider spider-result )
136     dup todo>> peek-url url>> >>currently-spidering
137     dup todo>> pop-url [ url>> ] [ depth>> ] bi <spider-result> ;
138
139 : spider-next-page ( spider -- )
140     setup-next-url
141     spider-page ;
142
143 PRIVATE>
144
145 : run-spider-loop ( spider -- )
146     dup spider-page? [
147         [ spider-next-page ] [ spider-sleep ] [ run-spider-loop ] tri
148     ] [
149         drop
150     ] if ;
151
152 : run-spider ( spider -- spider )
153     "spider" [
154         queue-initial-links
155         [ run-spider-loop ] keep
156     ] with-logging ;