MCPcopy Create free account
hub / github.com/apache/solr / webCrawl

Method webCrawl

solr/core/src/java/org/apache/solr/cli/PostTool.java:632–702  ·  view source on GitHub ↗

A very simple crawler, pulling URLs to fetch from a backlog and then recurses N levels deep if recursive>0. Links are parsed from HTML through first getting an XHTML version using SolrCell with extractOnly, and followed if they are local. The crawler pauses for a default delay of 10 seconds betwe

(int level, OutputStream out)

Source from the content-addressed store, hash-verified

630 * @return number of pages crawled on this level and below
631 */
632 protected int webCrawl(int level, OutputStream out) {
633 int numPages = 0;
634 LinkedHashSet<URI> stack = backlog.get(level);
635 int rawStackSize = stack.size();
636 stack.removeAll(visited);
637 int stackSize = stack.size();
638 LinkedHashSet<URI> subStack = new LinkedHashSet<>();
639 info(
640 "Entering crawl at level "
641 + level
642 + " ("
643 + rawStackSize
644 + " links total, "
645 + stackSize
646 + " new)");
647 for (URI uri : stack) {
648 try {
649 visited.add(uri);
650 URL url = uri.toURL();
651 PostTool.PageFetcherResult result = pageFetcher.readPageFromUrl(url);
652 if (result.httpStatus == 200) {
653 url = (result.redirectUrl != null) ? result.redirectUrl : url;
654 URI postUri =
655 new URI(
656 appendParam(
657 solrUpdateUrl.toString(),
658 "literal.id="
659 + URLEncoder.encode(url.toString(), UTF_8)
660 + "&literal.url="
661 + URLEncoder.encode(url.toString(), UTF_8)));
662 ByteBuffer content = result.content;
663 boolean success =
664 postData(
665 new ByteArrayInputStream(content.array(), content.arrayOffset(), content.limit()),
666 null,
667 out,
668 result.contentType,
669 postUri);
670 if (success) {
671 info("POSTed web resource " + url + " (depth: " + level + ")");
672 Thread.sleep(delay * 1000L);
673 numPages++;
674 // Pull links from HTML pages only
675 if (recursive > level && result.contentType.equals("text/html")) {
676 Set<URI> children =
677 pageFetcher.getLinksFromWebPage(
678 url,
679 new ByteArrayInputStream(
680 content.array(), content.arrayOffset(), content.limit()),
681 result.contentType,
682 postUri);
683 subStack.addAll(children);
684 }
685 } else {
686 warn("An error occurred while posting " + uri);
687 }
688 } else {
689 warn("The URL " + uri + " returned a HTTP result status of " + result.httpStatus);

Callers 1

postWebPagesMethod · 0.95

Calls 15

infoMethod · 0.95
appendParamMethod · 0.95
postDataMethod · 0.95
warnMethod · 0.95
encodeMethod · 0.80
getMethod · 0.65
sizeMethod · 0.65
addMethod · 0.65
toStringMethod · 0.65
removeAllMethod · 0.45
readPageFromUrlMethod · 0.45
sleepMethod · 0.45

Tested by

no test coverage detected