-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebgrep.rkt
More file actions
310 lines (287 loc) · 13.8 KB
/
webgrep.rkt
File metadata and controls
310 lines (287 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#lang at-exp racket
(require net/url
racket/contract/base
racket/contract/region
racket/format
racket/function
racket/hash
racket/list
racket/match
racket/math
racket/port
racket/promise
racket/set
racket/string)
(define-logger webgrep)
(define (url->content! url-string)
(with-handlers ([url-exception? (thunk* "")]
[exn:fail? (thunk* "")])
(call/input-url (string->url url-string)
get-pure-port
port->string)))
(struct page (url depth content-matches children) #:transparent)
;; url? (listof regexp?) regexp? -> (promise/c page?)
(define (launch-page-search! url depth content-pats url-pat)
(delay/thread
(log-webgrep-info @~a{Searching: @url})
(define content (url->content! url))
(define content-matches (flatten
(map (λ (pat) (regexp-match* pat content))
content-pats)))
(define children (urls-matching url-pat content #:base url))
(page url depth content-matches children)))
(define (urls-matching pat content #:base base-url)
(define url-pat
(pregexp
#<<HERE
a[^>]+href *= *["']([^#][^ "]*)["']
HERE
))
(define all-urls (regexp-match* url-pat content #:match-select cadr))
(define all-urls/normalized
(map (normalize-relative-url base-url) all-urls))
(remove-duplicates
(filter (λ (url)
(define keep? (regexp-match? pat url))
(log-webgrep-debug
@~a{@(if keep? "Including" "Skipping") link @url due to user-supplied regexps})
keep?)
all-urls/normalized)))
(module+ test
(require ruinit)
(test-begin
#:name urls-matching
(ignore (define content
#<<HERE
returning parenthesized sub-patterns. It can be given as a ‘selector’
function which chooses an item from a list, or it can choose a list of
items. For example, you can use <span class="RktSym"><a href="pairs.html#%28def._%28%28quote._~23~25kernel%29._cdr%29%29" class="RktValLink" data-pltdoc="x">cdr</a></span> to get a list of lists
of parenthesized sub-patterns matches, or <span class="RktSym"><a href="values.html#%28def._%28%28quote._~23~25kernel%29._values%29%29" class="RktValLink" data-pltdoc="x">values</a></span> (as an
identity function) to get the full matches as well. (Note that the
selector must choose an element of its input list or a list of
elements, but it must not inspect its input as they can be either a
<a href="/foo.html">foo</a>
list of strings or a list of position pairs. Furthermore, the
selector must be consistent in its choice(s).)</p><p><div class="SIntrapara">Examples:</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._regexp-match%2A%29%29" class="RktValLink" data-pltdoc="x">regexp-match*</a></span><span class="hspace"> </span><span class="RktVal">#rx"x(.)"</span><span class="hspace"> </span><span class="RktVal">"12x4x6"</span><span class="hspace"> </span><span class="RktPn">#:match-select</span><span class="hspace"> </span><span class="RktSym"><a href="pairs.html#%28def._%28%28quote._~23~25kernel%29._cadr%29%29" class="RktValLink" data-pltdoc="x">cadr</a></span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">'("4" "6")</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._regexp-match%2A%29%29" class="RktValLink" data-pltdoc="x">regexp-match*</a></span><span class="hspace"> </span><span class="RktVal">#rx"x(.)"</span><span class="hspace"> </span><span class="RktVal">"12x4x6"</span><span class="hspace"> </span><span class="RktPn">#:match-select</span><span class="hspace"> </span><span class="RktSym"><a href="values.html#%28def._%28%28quote._~23~25kernel%29._values%29%29" class="RktValLink" data-pltdoc="x">values</a></span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">'(("x4" "4") ("x6" "6"))</span></p></td></tr></table></blockquote></div></p><p>In addition, specifying <span class="RktVar">gap-select</span> as a non-<span class="RktVal">#f</span> value
will make the result an interleaved list of the matches as well as the
separators between them matches, starting and ending with a separator.
HERE
))
(test-match (urls-matching #px".*" content #:base "base")
(list-no-order
"base/foo.html"
"pairs.html#%28def._%28%28quote._~23~25kernel%29._cdr%29%29"
;; No internal page links!
;; "#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._regexp-match%2A%29%29"
"pairs.html#%28def._%28%28quote._~23~25kernel%29._cadr%29%29"
;; "#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._regexp-match%2A%29%29"
"values.html#%28def._%28%28quote._~23~25kernel%29._values%29%29"))
(test-match (urls-matching #px"value" content #:base "base")
(list-no-order "values.html#%28def._%28%28quote._~23~25kernel%29._values%29%29"))))
(define ((normalize-relative-url base-url) url)
(if (or (string-prefix? url "/")
(not (regexp-match? #px"http(s)?://" url)))
(url->string (combine-url/relative (string->url base-url) url))
url))
;; results? := (hash/c url? (listof string?))
;; childmap? := (hash/c url? (listof url?))
;; todo-list? := (listof todo?)
(struct todo (url depth) #:transparent)
;; (listof (promise/c page?))
;; ->
;; (values todo-list?
;; childmap?
;; results?)
(define (collect-search-results finished-links
#:print-progress? [print-progress? #f])
(define-values {todos childmap results}
(for/lists {todos childmap results}
([link finished-links])
(match-define (page url depth content-matches children) (force link))
(match* {print-progress? (length content-matches)}
[{'url (not 0)} (displayln url)]
[{'all (not 0)} (displayln
@~a{
@url
==================================================
@~v[content-matches]
})]
[{_ _} (void)])
(values (map (λ (child) (todo child (add1 depth))) children)
(hash url children)
(hash url content-matches))))
(values (flatten todos)
(apply hash-union childmap
#:combine (λ (a b) a))
(apply hash-union results
#:combine (λ (a b) (remove-duplicates
(append a b))))))
(module+ test
(require (for-syntax racket/base))
(define-test-syntax (test-search-results promises
todos-pat
children-pat
results-pat)
#'(let-values ([{todos children results} (collect-search-results promises)])
(and/test (test-match todos todos-pat)
(test-match children children-pat)
(test-match results results-pat))))
(test-begin
#:name collect-search-results
(test-search-results (list (delay (page "u" 1 '() '())))
'()
(== (hash "u" '()))
(== (hash "u" '())))
(test-search-results (list (delay (page "u" 1 '("foo") '())))
'()
(== (hash "u" '()))
(== (hash "u" '("foo"))))
(test-search-results (list (delay (page "u" 1 '("foo") '("a"))))
(list (todo "a" 2))
(== (hash "u" '("a")))
(== (hash "u" '("foo"))))
(test-search-results (list (delay (page "u" 1 '("foo") '("a" "b"))))
(list (todo "a" 2) (todo "b" 2))
(== (hash "u" '("a" "b")))
(== (hash "u" '("foo"))))))
(define url-string? string?)
(define ((hash-has-key/c k) h)
(hash-has-key? h k))
(define ((todo-url-not-in/c set) t)
(not (member (todo-url t) set)))
;; url? natural? (hash/c url? any/c) -> (listof todo?)
;; Produce a list of the unseen children of URL
(define/contract (unseen-children url depth urls-seen)
(->i ([url url-string? ]
[depth natural?]
[urls-seen (url) (and/c (hash/c url-string? (listof url-string?))
(hash-has-key/c url))])
[result (url urls-seen)
(listof (and/c todo?
(todo-url-not-in/c (hash-keys urls-seen))))])
(define children/all (hash-ref urls-seen url))
(define seen-urls (hash-keys urls-seen))
(define children/unseen (set-subtract children/all seen-urls))
(map (λ (unseen-child) (todo unseen-child (add1 depth)))
children/unseen))
(module+ test
(test-begin
#:name unseen-children
(test-match (unseen-children "a" 2 (hash "a" '("b" "c")
"b" '("d" "e")))
(list-no-order (todo "c" 3)))
(test-match (unseen-children "b" 2 (hash "a" '("b" "c")
"b" '("d" "e")))
(list-no-order (todo "d" 3) (todo "e" 3)))
(test-match (unseen-children "z" 2 (hash "a" '("b" "c")
"b" '("d" "e")
"z" '("a" "b")))
'())))
;; url? (listof regexp?) (listof regexp?) natural? #:thread-limit natural?
;; ->
;; results?
;;
;; Return a mapping from url to content matches
(define (find-matches-in! base-url content-pats url-pat depth
#:thread-limit [thread-limit 50]
#:print-progress? [print-progress? #f])
(let loop ([links-todo (list (todo base-url 0))]
[links-waiting empty]
[urls->children (hash)]
[search-results (hash)]
[searched (set)])
(cond [(or (>= (length links-waiting) thread-limit)
(and (empty? links-todo)
(not (empty? links-waiting))))
(apply sync links-waiting)
(define-values {links-still-waiting links-done}
(partition promise-running? links-waiting))
(define-values {new-todos new-urls->children new-matches}
(collect-search-results links-done
#:print-progress? print-progress?))
(loop (append new-todos links-todo)
links-still-waiting
(hash-union urls->children new-urls->children
#:combine (λ (a b) a))
(hash-union search-results new-matches
#:combine (λ (a b) (remove-duplicates
(append a b))))
searched)]
[else
(match links-todo
['() search-results]
[(cons (todo url url-depth) todos-rest)
#:when (hash-has-key? urls->children url)
(define unseen-links-todo
(unseen-children url url-depth urls->children))
(loop (append unseen-links-todo (rest links-todo))
links-waiting
urls->children
search-results
searched)]
[(cons (todo url url-depth) todos-rest)
#:when (set-member? searched url)
(loop todos-rest
links-waiting
urls->children
search-results
searched)]
[(cons (todo url url-depth) todos-rest)
(loop todos-rest
(if (<= url-depth depth)
(cons (launch-page-search! url url-depth
content-pats url-pat)
links-waiting)
links-waiting)
urls->children
search-results
(set-add searched url))])])))
(module+ main
(require rscript/cmdline)
(match-define (cons flags args)
(command-line/declarative
#:multi
[("-c" "--content-pat")
'content-pats
"Patterns to find in page contents."
#:collect ["regexp" cons empty]]
#:once-each
[("-u" "--url-pat")
'url-pat
("Pattern of links to find in page contents."
"Default: any link")
#:collect ["regexp" take-latest ".*"]]
[("-r" "--root")
'root-url
"Url at which to root search."
#:mandatory
#:collect ["url" take-latest #f]]
[("-d" "--depth")
'depth
("Depth of linked pages to traverse."
"Default: 2")
#:collect ["natural" take-latest "2"]]
[("-l" "--links-only")
'links-only
("Only show links of pages with matching content,"
"not content matches as well.")
#:record]
[("-j" "--thread-limit")
'thread-limit
("Maximum number of threads to search in parallel."
"Default: 50")
#:collect ["natural" take-latest "50"]]))
(file-stream-buffer-mode (current-output-port) 'line)
(define full-line-content-pats
(map (λ (pat) @~a{(?m:^.*@|pat|.*$)})
(hash-ref flags 'content-pats)))
(void
(find-matches-in!
(hash-ref flags 'root-url)
full-line-content-pats
(pregexp (hash-ref flags 'url-pat))
(string->number (hash-ref flags 'depth))
#:thread-limit (string->number (hash-ref flags 'thread-limit))
#:print-progress? (if (hash-ref flags 'links-only)
'url
'all))))