1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs combinators combinators.short-circuit
4 fry html.parser http.client io kernel locals math math.statistics
5 sequences sets splitting unicode.case unicode.categories urls
6 urls.encoding shuffle ;
7 IN: html.parser.analyzer
9 : scrape-html ( url -- headers vector )
12 : attribute ( tag string -- obj/f )
13 swap attributes>> [ at ] [ drop f ] if* ;
15 : attribute* ( tag string -- obj ? )
16 swap attributes>> [ at* ] [ drop f f ] if* ;
18 : attribute? ( tag string -- obj )
19 swap attributes>> [ key? ] [ drop f ] if* ;
21 : find-all ( seq quot -- alist )
22 [ <enum> >alist ] [ '[ second @ ] ] bi* filter ; inline
24 : loopn-index ( n quot -- )
25 [ iota ] [ '[ @ not ] ] bi* find 2drop ; inline
28 [ drop ] prepose loopn-index ; inline
30 ERROR: undefined-find-nth m n seq quot ;
32 : check-trivial-find ( m n seq quot -- m n seq quot )
33 pick 0 = [ undefined-find-nth ] when ; inline
35 : find-nth-from ( m n seq quot -- i/f elt/f )
36 check-trivial-find [ f ] 3dip '[
37 drop _ _ find-from [ dup [ 1 + ] when ] dip over
38 ] loopn [ dup [ 1 - ] when ] dip ; inline
40 : find-nth ( n seq quot -- i/f elt/f )
41 [ 0 ] 3dip find-nth-from ; inline
43 : find-last-nth-from ( m n seq quot -- i/f elt/f )
44 check-trivial-find [ f ] 3dip '[
45 drop _ _ find-last-from [ dup [ 1 - ] when ] dip over
46 ] loopn [ dup [ 1 + ] when ] dip ; inline
48 : find-last-nth ( n seq quot -- i/f elt/f )
49 [ [ nip length 1 - ] [ ] 2bi ] dip find-last-nth-from ; inline
51 : find-first-name ( vector string -- i/f tag/f )
52 >lower '[ name>> _ = ] find ; inline
54 ! Takes a sequence and a quotation expected to return -1 if the
55 ! element decrements the stack, 0 if it doesnt affect it and 1 if it
56 ! increments it. Then finds the matching element where the stack is
58 : stack-find ( seq quot -- i/f )
59 map cum-sum [ 0 = ] find drop ; inline
61 ! Produces a function which returns 1 if the input item is an opening
62 ! tag element with the specified name, -1 if it is a closing tag of
63 ! the same name and 0 otherwise.
64 : tag-classifier ( string -- quot )
66 '[ dup name>> _ = [ closing?>> [ -1 ] [ 1 ] if ] [ drop 0 ] if ] ; inline
68 : find-between* ( vector i/f tag/f -- vector )
70 [ tail-slice ] [ name>> ] bi*
71 dupd tag-classifier stack-find [ 1 + ] [ 1 ] if*
77 : find-between ( vector i/f tag/f -- vector )
78 find-between* dup length 3 >= [
79 [ rest-slice but-last-slice ] keep like
82 : find-between-first ( vector string -- vector' )
83 dupd find-first-name find-between ; inline
85 : find-between-all ( vector quot -- seq )
87 '[ _ [ closing?>> not ] bi and ] find-all
88 [ first2 find-between* ] with map ; inline
90 : remove-blank-text ( vector -- vector' )
93 [ text>> [ blank? ] all? not ] [ drop t ] if
96 : trim-text ( vector -- vector' )
99 [ [ [ blank? ] trim ] change-text ] when
102 : find-by-id ( vector id -- vector' elt/f )
103 '[ "id" attribute _ = ] find ;
105 : find-by-class ( vector id -- vector' elt/f )
106 '[ "class" attribute _ = ] find ;
108 : find-by-name ( vector string -- vector elt/f )
109 >lower '[ name>> _ = ] find ;
111 : find-by-id-between ( vector string -- vector' )
113 '[ "id" attribute _ = ] find find-between* ;
115 : find-by-class-between ( vector string -- vector' )
117 '[ "class" attribute _ = ] find find-between* ;
119 : find-by-class-id-between ( vector class id -- vector' )
122 [ "class" attribute _ = ]
123 [ "id" attribute _ = ] bi and
129 : find-by-attribute-key ( vector key -- vector' elt/? )
131 [ attributes>> at _ = ] filter sift ;
133 : find-by-attribute-key-value ( vector value key -- vector' )
135 [ attributes>> at over = ] with filter nip sift ;
137 : find-first-attribute-key-value ( vector value key -- i/f tag/f )
139 [ attributes>> at over = ] with find rot drop ;
141 : tag-link ( tag -- link/f ) "href" attribute ;
143 : find-links ( vector -- vector' )
144 [ { [ name>> "a" = ] [ "href" attribute ] } 1&& ]
147 : find-images ( vector -- vector' )
154 values [ "src" attribute ] map ;
156 : find-by-text ( seq quot -- tag )
157 [ dup name>> text = ] prepose find drop ; inline
159 : find-opening-tags-by-name ( name seq -- seq )
160 [ { [ name>> = ] [ closing?>> not ] } 1&& ] with find-all ;
162 : href-contains? ( str tag -- ? )
163 "href" attribute* [ subseq? ] [ 2drop f ] if ;
165 : find-hrefs ( vector -- vector' )
167 [ [ { [ name>> "a" = ] [ "href" attribute? ] } 1&& ] filter ] map sift
168 [ [ "href" attribute ] map ] map concat [ >url ] map ;
170 : find-frame-links ( vector -- vector' )
171 [ name>> "frame" = ] find-between-all
172 [ [ "src" attribute ] map sift ] map concat sift
175 : find-all-links ( vector -- vector' )
176 [ find-hrefs ] [ find-frame-links ] bi union ;
178 : find-forms ( vector -- vector' )
179 "form" over find-opening-tags-by-name
180 swap [ [ first2 ] dip find-between* ] curry map
181 [ [ name>> { "form" "input" } member? ] filter ] map ;
183 : find-html-objects ( vector string -- vector' )
184 over find-opening-tags-by-name
185 [ first2 find-between* ] with map ;
187 : form-action ( vector -- string )
188 [ name>> "form" = ] find nip "action" attribute ;
190 : hidden-form-values ( vector -- strings )
191 [ "type" attribute "hidden" = ] filter ;
196 [ bl bl bl bl [ write "=" write ] [ write bl ] bi* nl ] assoc-each ;
198 : form. ( vector -- )
199 [ closing?>> not ] filter
202 { [ dup name>> "form" = ]
203 [ "form action: " write "action" attribute print ] }
204 { [ dup name>> "input" = ] [ input. ] }
209 : query>assoc* ( str -- hash )
210 "?" split1 nip query>assoc ;
212 : html-class? ( tag string -- ? )
213 swap "class" attribute = ;
215 : html-id? ( tag string -- ? )
216 swap "id" attribute = ;
218 : opening-tag? ( tag -- ? )
221 TUPLE: link attributes clickable ;
223 : <link> ( vector -- link )
224 [ first attributes>> ]
225 [ [ name>> { text "img" } member? ] filter ] bi
228 : link. ( vector -- )
229 [ "href" attribute write nl ]
230 [ clickable>> [ bl bl text>> print ] each nl ] bi ;