-
Notifications
You must be signed in to change notification settings - Fork 256
/
JSoupParserBolt.java
481 lines (395 loc) · 18 KB
/
JSoupParserBolt.java
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/**
* Licensed to DigitalPebble Ltd under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* DigitalPebble licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.digitalpebble.stormcrawler.bolt;
import static com.digitalpebble.stormcrawler.Constants.StatusStreamName;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
import org.apache.storm.metric.api.MultiCountMetric;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;
import org.apache.tika.config.TikaConfig;
import org.apache.tika.detect.Detector;
import org.apache.tika.mime.MediaType;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;
import org.slf4j.LoggerFactory;
import org.w3c.dom.DocumentFragment;
import com.digitalpebble.stormcrawler.Constants;
import com.digitalpebble.stormcrawler.Metadata;
import com.digitalpebble.stormcrawler.parse.DocumentFragmentBuilder;
import com.digitalpebble.stormcrawler.parse.Outlink;
import com.digitalpebble.stormcrawler.parse.ParseData;
import com.digitalpebble.stormcrawler.parse.ParseFilter;
import com.digitalpebble.stormcrawler.parse.ParseFilters;
import com.digitalpebble.stormcrawler.parse.ParseResult;
import com.digitalpebble.stormcrawler.parse.TextExtractor;
import com.digitalpebble.stormcrawler.persistence.Status;
import com.digitalpebble.stormcrawler.protocol.HttpHeaders;
import com.digitalpebble.stormcrawler.protocol.ProtocolResponse;
import com.digitalpebble.stormcrawler.util.CharsetIdentification;
import com.digitalpebble.stormcrawler.util.ConfUtils;
import com.digitalpebble.stormcrawler.util.RefreshTag;
import com.digitalpebble.stormcrawler.util.RobotsTags;
/**
* Parser for HTML documents only which uses ICU4J to detect the charset
* encoding. Kindly donated to storm-crawler by shopstyle.com.
*/
@SuppressWarnings("serial")
public class JSoupParserBolt extends StatusEmitterBolt {
/** Metadata key name for tracking the anchors */
public static final String ANCHORS_KEY_NAME = "anchors";
private static final org.slf4j.Logger LOG = LoggerFactory
.getLogger(JSoupParserBolt.class);
private MultiCountMetric eventCounter;
private ParseFilter parseFilters = null;
private Detector detector = TikaConfig.getDefaultConfig().getDetector();
private boolean detectMimeType = true;
private boolean trackAnchors = true;
private boolean emitOutlinks = true;
private int maxOutlinksPerPage = -1;
private boolean robots_noFollow_strict = true;
/**
* If a Tuple is not HTML whether to send it to the status stream as an
* error or pass it on the default stream
**/
private boolean treat_non_html_as_error = true;
/**
* Length of content to use for detecting the charset. Set to -1 to use the
* full content (will make the parser slow), 0 to deactivate the detection
* altogether, or any other value (at least a few hundred bytes).
**/
private int maxLengthCharsetDetection = -1;
private TextExtractor textExtractor;
private String protocolMDprefix;
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void prepare(Map conf, TopologyContext context,
OutputCollector collector) {
super.prepare(conf, context, collector);
eventCounter = context.registerMetric(this.getClass().getSimpleName(),
new MultiCountMetric(), 10);
parseFilters = ParseFilters.fromConf(conf);
emitOutlinks = ConfUtils.getBoolean(conf, "parser.emitOutlinks", true);
trackAnchors = ConfUtils.getBoolean(conf, "track.anchors", true);
robots_noFollow_strict = ConfUtils.getBoolean(conf,
RobotsTags.ROBOTS_NO_FOLLOW_STRICT, true);
treat_non_html_as_error = ConfUtils.getBoolean(conf,
"jsoup.treat.non.html.as.error", true);
detectMimeType = ConfUtils.getBoolean(conf, "detect.mimetype", true);
maxLengthCharsetDetection = ConfUtils.getInt(conf,
"detect.charset.maxlength", -1);
maxOutlinksPerPage = ConfUtils.getInt(conf,
"parser.emitOutlinks.max.per.page", -1);
protocolMDprefix = ConfUtils.getString(conf,
ProtocolResponse.PROTOCOL_MD_PREFIX_PARAM, "");
textExtractor = new TextExtractor(conf);
}
@Override
public void execute(Tuple tuple) {
byte[] content = tuple.getBinaryByField("content");
String url = tuple.getStringByField("url");
Metadata metadata = (Metadata) tuple.getValueByField("metadata");
LOG.info("Parsing : starting {}", url);
// check that its content type is HTML
// look at value found in HTTP headers
boolean CT_OK = false;
String mimeType = metadata.getFirstValue(HttpHeaders.CONTENT_TYPE, this.protocolMDprefix);
if (detectMimeType) {
try {
mimeType = guessMimeType(url, mimeType, content);
} catch (Exception e) {
String errorMessage = "Exception while guessing mimetype on "
+ url + ": " + e;
handleException(url, e, metadata, tuple, "mimetype guessing",
errorMessage);
return;
}
// store identified type in md
metadata.setValue("parse.Content-Type", mimeType);
}
if (StringUtils.isNotBlank(mimeType)) {
if (mimeType.toLowerCase().contains("html")) {
CT_OK = true;
}
}
// go ahead even if no mimetype is available
else {
CT_OK = true;
}
if (!CT_OK) {
if (this.treat_non_html_as_error) {
String errorMessage = "Exception content-type " + mimeType
+ " for " + url;
RuntimeException e = new RuntimeException(errorMessage);
handleException(url, e, metadata, tuple,
"content-type checking", errorMessage);
} else {
LOG.info("Unsupported mimetype {} - passing on : {}", mimeType,
url);
collector.emit(tuple, new Values(url, content, metadata, ""));
collector.ack(tuple);
}
return;
}
long start = System.currentTimeMillis();
String charset = CharsetIdentification.getCharset(metadata, content,
maxLengthCharsetDetection);
// get the robots tags from the fetch metadata
RobotsTags robotsTags = new RobotsTags(metadata);
Map<String, List<String>> slinks;
String text = "";
final org.jsoup.nodes.Document jsoupDoc;
try {
String html = Charset.forName(charset)
.decode(ByteBuffer.wrap(content)).toString();
jsoupDoc = Parser.htmlParser().parseInput(html, url);
// extracts the robots directives from the meta tags
Element robotelement = jsoupDoc
.selectFirst("meta[name~=(?i)robots][content]");
if (robotelement != null) {
robotsTags.extractMetaTags(robotelement.attr("content"));
}
// store a normalised representation in metadata
// so that the indexer is aware of it
robotsTags.normaliseToMetadata(metadata);
// do not extract the links if no follow has been set
// and we are in strict mode
if (robotsTags.isNoFollow() && robots_noFollow_strict) {
slinks = new HashMap<>(0);
} else {
Elements links = jsoupDoc.select("a[href]");
slinks = new HashMap<>(links.size());
for (Element link : links) {
// abs:href tells jsoup to return fully qualified domains
// for
// relative urls.
// e.g.: /foo will resolve to http://shopstyle.com/foo
String targetURL = link.attr("abs:href");
// nofollow
boolean noFollow = "nofollow".equalsIgnoreCase(link
.attr("rel"));
// remove altogether
if (noFollow && robots_noFollow_strict) {
continue;
}
// link not specifically marked as no follow
// but whole page is
if (!noFollow && robotsTags.isNoFollow()) {
noFollow = true;
}
String anchor = link.text();
if (StringUtils.isNotBlank(targetURL)) {
// any existing anchors for the same target?
List<String> anchors = slinks.get(targetURL);
if (anchors == null) {
anchors = new LinkedList<>();
slinks.put(targetURL, anchors);
}
// track the anchors only if no follow is false
if (!noFollow && StringUtils.isNotBlank(anchor)) {
anchors.add(anchor);
}
}
}
}
Element body = jsoupDoc.body();
if (body != null) {
text = textExtractor.text(body);
}
} catch (Throwable e) {
String errorMessage = "Exception while parsing " + url + ": " + e;
handleException(url, e, metadata, tuple, "content parsing",
errorMessage);
return;
}
// store identified charset in md
metadata.setValue("parse.Content-Encoding", charset);
long duration = System.currentTimeMillis() - start;
LOG.info("Parsed {} in {} msec", url, duration);
// redirection?
try {
String redirection = null;
Element redirElement = jsoupDoc
.selectFirst("meta[http-equiv~=(?i)refresh][content]");
if (redirElement != null) {
redirection = RefreshTag.extractRefreshURL(redirElement
.attr("content"));
}
if (StringUtils.isNotBlank(redirection)) {
// stores the URL it redirects to
// used for debugging mainly - do not resolve the target
// URL
LOG.info("Found redir in {} to {}", url, redirection);
metadata.setValue("_redirTo", redirection);
if (allowRedirs() && StringUtils.isNotBlank(redirection)) {
emitOutlink(tuple, new URL(url), redirection, metadata);
}
// Mark URL as redirected
collector
.emit(com.digitalpebble.stormcrawler.Constants.StatusStreamName,
tuple, new Values(url, metadata,
Status.REDIRECTION));
collector.ack(tuple);
eventCounter.scope("tuple_success").incr();
return;
}
} catch (MalformedURLException e) {
LOG.error("MalformedURLException on {}", url);
}
List<Outlink> outlinks = toOutlinks(url, metadata, slinks);
ParseResult parse = new ParseResult(outlinks);
// parse data of the parent URL
ParseData parseData = parse.get(url);
parseData.setMetadata(metadata);
parseData.setText(text);
parseData.setContent(content);
// apply the parse filters if any
try {
DocumentFragment fragment = null;
// lazy building of fragment
if (parseFilters.needsDOM()) {
fragment = DocumentFragmentBuilder.fromJsoup(jsoupDoc);
}
parseFilters.filter(url, content, fragment, parse);
} catch (RuntimeException e) {
String errorMessage = "Exception while running parse filters on "
+ url + ": " + e;
handleException(url, e, metadata, tuple, "content filtering",
errorMessage);
return;
}
if (emitOutlinks) {
final List<Outlink> outlinksAfterLimit = (maxOutlinksPerPage == -1) ? parse
.getOutlinks() : parse.getOutlinks().stream()
.limit(maxOutlinksPerPage).collect(Collectors.toList());
for (Outlink outlink : outlinksAfterLimit) {
collector.emit(
StatusStreamName,
tuple,
new Values(outlink.getTargetURL(), outlink
.getMetadata(), Status.DISCOVERED));
}
}
// emit each document/subdocument in the ParseResult object
// there should be at least one ParseData item for the "parent" URL
for (Map.Entry<String, ParseData> doc : parse) {
ParseData parseDoc = doc.getValue();
collector.emit(
tuple,
new Values(doc.getKey(), parseDoc.getContent(), parseDoc
.getMetadata(), parseDoc.getText()));
}
LOG.info("Total for {} - {} msec", url, System.currentTimeMillis()
- start);
collector.ack(tuple);
eventCounter.scope("tuple_success").incr();
}
private void handleException(String url, Throwable e, Metadata metadata,
Tuple tuple, String errorSource, String errorMessage) {
LOG.error(errorMessage);
// send to status stream in case another component wants to update
// its status
metadata.setValue(Constants.STATUS_ERROR_SOURCE, errorSource);
metadata.setValue(Constants.STATUS_ERROR_MESSAGE, errorMessage);
collector.emit(StatusStreamName, tuple, new Values(url, metadata,
Status.ERROR));
collector.ack(tuple);
// Increment metric that is context specific
String s = "error_" + errorSource.replaceAll(" ", "_") + "_";
eventCounter.scope(s + e.getClass().getSimpleName()).incrBy(1);
// Increment general metric
eventCounter.scope("parse exception").incrBy(1);
}
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
super.declareOutputFields(declarer);
// output of this module is the list of fields to index
// with at least the URL, text content
declarer.declare(new Fields("url", "content", "metadata", "text"));
}
public String guessMimeType(String URL, String httpCT, byte[] content) {
org.apache.tika.metadata.Metadata metadata = new org.apache.tika.metadata.Metadata();
if (StringUtils.isNotBlank(httpCT)) {
// pass content type from server as a clue
metadata.set(org.apache.tika.metadata.Metadata.CONTENT_TYPE, httpCT);
}
// use full URL as a clue
metadata.set(org.apache.tika.metadata.Metadata.RESOURCE_NAME_KEY, URL);
metadata.set(org.apache.tika.metadata.Metadata.CONTENT_LENGTH,
Integer.toString(content.length));
try (InputStream stream = new ByteArrayInputStream(content)) {
MediaType mt = detector.detect(stream, metadata);
return mt.toString();
} catch (IOException e) {
throw new IllegalStateException("Unexpected IOException", e);
}
}
private List<Outlink> toOutlinks(String url, Metadata metadata,
Map<String, List<String>> slinks) {
Map<String, Outlink> outlinks = new HashMap<>();
URL sourceUrl;
try {
sourceUrl = new URL(url);
} catch (MalformedURLException e) {
// we would have known by now as previous components check whether
// the URL is valid
LOG.error("MalformedURLException on {}", url);
eventCounter.scope("error_invalid_source_url").incrBy(1);
return new LinkedList<Outlink>();
}
for (Map.Entry<String, List<String>> linkEntry : slinks.entrySet()) {
String targetURL = linkEntry.getKey();
Outlink ol = filterOutlink(sourceUrl, targetURL, metadata);
if (ol == null) {
eventCounter.scope("outlink_filtered").incr();
continue;
}
// the same link could already be there post-normalisation
Outlink old = outlinks.get(ol.getTargetURL());
if (old != null) {
ol = old;
}
List<String> anchors = linkEntry.getValue();
if (trackAnchors && anchors.size() > 0) {
ol.getMetadata().addValues(ANCHORS_KEY_NAME, anchors);
// sets the first anchor
ol.setAnchor(anchors.get(0));
}
if (old == null) {
outlinks.put(ol.getTargetURL(), ol);
eventCounter.scope("outlink_kept").incr();
}
}
return new LinkedList<Outlink>(outlinks.values());
}
}