Apache Nutch is an extensible and scalable web crawler
NUTCH-2021 Use protocol-selenium to Capture Screenshots of the Page as it is Fetched
Lewis John McGibbneya64ec0c751b1
diff --git a/CHANGES.txt b/CHANGES.txtindex f7cfb565c..959030947 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,8 @@ Nutch Change LogNutch Current Development 1.11-SNAPSHOT+* NUTCH-2021 Use protocol-selenium to Capture Screenshots of the Page as it is Fetched (lewismc)+* NUTCH-2058 Indexer plugin that allows RegEx replacements on the NutchDocumentfield values (Peter Ciuffetti via mattmann)diff --git a/conf/nutch-default.xml b/conf/nutch-default.xmlindex 2e5db8344..e9040cd44 100644--- a/conf/nutch-default.xml+++ b/conf/nutch-default.xml@@ -1736,4 +1736,40 @@ CAUTION: Set the parser.timeout to -1 or a bigger value than 30, when using this</description></property>+<!-- protocol-selenium plugin properties -->++<property>+ <name>selenium.driver</name>+ <value>firefox</value>+ <description>+ A String value representing the flavour of Selenium+ WebDriver() to use. Currently the following options+ exist - firefox, chrome, safari and opera.+ </description>+</property>++<property>+ <name>selenium.take.screenshot</name>+ <value>false</value>+ <description>+ Boolean property determining whether the protocol-selenium+ WebDriver should capture a screenshot of the URL. If set to+ true remember to define the 'selenium.screenshot.location'+ property as this determines the location screenshots should be+ persisted to on HDFS. If that property is not set, screenshots+ are simply discarded.+ </description>+</property>++<property>+ <name>selenium.screenshot.location</name>+ <value></value>+ <description>+ The location on disk where a URL screenshot should be saved+ to if the 'selenium.take.screenshot' proerty is set to true.+ By default this is null, in this case screenshots held in memory+ are simply discarded.+ </description>+</property>+</configuration>diff --git a/src/java/org/apache/nutch/tools/FileDumper.java b/src/java/org/apache/nutch/tools/FileDumper.javaindex c744d7ca3..ab44bba07 100644--- a/src/java/org/apache/nutch/tools/FileDumper.java+++ b/src/java/org/apache/nutch/tools/FileDumper.java@@ -26,8 +26,6 @@ import java.io.ByteArrayInputStream;import java.util.Arrays;import java.util.HashMap;import java.util.Map;-import java.security.MessageDigest;-import com.google.common.base.Strings;import org.apache.commons.cli.CommandLine;import org.apache.commons.cli.CommandLineParser;@@ -102,7 +100,7 @@ import org.slf4j.LoggerFactory;* {"mimeType":"video/quicktime","count":"2"}* {"mimeType":"image/gif","count":"63"}* ]- *+ * }* </pre>* <p>* In the case above, the tool would have been run with the <b>-mimeTypediff --git a/src/plugin/lib-selenium/build-ivy.xml b/src/plugin/lib-selenium/build-ivy.xmlnew file mode 100644index 000000000..7963956c4--- /dev/null+++ b/src/plugin/lib-selenium/build-ivy.xml@@ -0,0 +1,54 @@+<?xml version="1.0"?>+<!--+ Licensed to the Apache Software Foundation (ASF) under one or more+ contributor license agreements. See the NOTICE file distributed with+ this work for additional information regarding copyright ownership.+ The ASF 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.+-->+<project name="lib-selenium" default="deps-jar" xmlns:ivy="antlib:org.apache.ivy.ant">++ <property name="ivy.install.version" value="2.1.0" />+ <condition property="ivy.home" value="${env.IVY_HOME}">+ <isset property="env.IVY_HOME" />+ </condition>+ <property name="ivy.home" value="${user.home}/.ant" />+ <property name="ivy.checksums" value="" />+ <property name="ivy.jar.dir" value="${ivy.home}/lib" />+ <property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar" />++ <target name="download-ivy" unless="offline">++ <mkdir dir="${ivy.jar.dir}"/>+ <!-- download Ivy from web site so that it can be used even without any special installation -->+ <get src="http://repo2.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"+ dest="${ivy.jar.file}" usetimestamp="true"/>+ </target>++ <target name="init-ivy" depends="download-ivy">+ <!-- try to load ivy here from ivy home, in case the user has not already dropped+ it into ant's lib dir (note that the latter copy will always take precedence).+ We will not fail as long as local lib dir exists (it may be empty) and+ ivy is in at least one of ant's lib dir or the local lib dir. -->+ <path id="ivy.lib.path">+ <fileset dir="${ivy.jar.dir}" includes="*.jar"/>++ </path>+ <taskdef resource="org/apache/ivy/ant/antlib.xml"+ uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>+ </target>++ <target name="deps-jar" depends="init-ivy">+ <ivy:retrieve pattern="lib/[artifact]-[revision].[ext]"/>+ </target>++</project>diff --git a/src/plugin/lib-selenium/howto_upgrade_selenium.txt b/src/plugin/lib-selenium/howto_upgrade_selenium.txtnew file mode 100644index 000000000..9a456bef3--- /dev/null+++ b/src/plugin/lib-selenium/howto_upgrade_selenium.txt@@ -0,0 +1,6 @@+1. Upgrade various driver versions dependency in src/plugin/lib-selenium/ivy.xml++2. Upgrade Tika's own dependencies in src/plugin/lib-selenium/plugin.xml+ To get the list of dependencies and their versions execute:+ $ ant -f ./build-ivy.xml+ $ ls lib | sed 's/^/ <library name="/g' | sed 's/$/"\/>/g'diff --git a/src/plugin/lib-selenium/ivy.xml b/src/plugin/lib-selenium/ivy.xmlindex c11012514..ed8d0c350 100644--- a/src/plugin/lib-selenium/ivy.xml+++ b/src/plugin/lib-selenium/ivy.xml@@ -42,6 +42,10 @@<dependency org="com.opera" name="operadriver" rev="1.5"><exclude org="org.seleniumhq.selenium" name="selenium-remote-driver" /></dependency>+ <dependency org="com.codeborne" name="phantomjsdriver" rev="1.2.1" >+ <exclude org="org.seleniumhq.selenium" name="selenium-remote-driver" />+ <exclude org="org.seleniumhq.selenium" name="selenium-java" />+ </dependency><!-- end selenium dependencies --></dependencies>diff --git a/src/plugin/lib-selenium/plugin.xml b/src/plugin/lib-selenium/plugin.xmlindex c2589812c..5f49d94fb 100644--- a/src/plugin/lib-selenium/plugin.xml+++ b/src/plugin/lib-selenium/plugin.xml@@ -27,11 +27,54 @@<runtime><library name="lib-selenium.jar"><export name="*"/>- </library>+ </library>+ <library name="cglib-nodep-2.1_3.jar"/>+ <library name="commons-codec-1.9.jar"/>+ <library name="commons-collections-3.2.1.jar"/>+ <library name="commons-exec-1.1.jar"/>+ <library name="commons-io-2.4.jar"/>+ <library name="commons-jxpath-1.3.jar"/>+ <library name="commons-lang3-3.3.2.jar"/>+ <library name="commons-logging-1.1.3.jar"/>+ <library name="cssparser-0.9.14.jar"/>+ <library name="gson-2.3.jar"/>+ <library name="guava-18.0.jar"/>+ <library name="htmlunit-2.15.jar"/>+ <library name="htmlunit-core-js-2.15.jar"/>+ <library name="httpclient-4.3.4.jar"/>+ <library name="httpcore-4.3.2.jar"/>+ <library name="httpmime-4.3.3.jar"/>+ <library name="ini4j-0.5.2.jar"/>+ <library name="jetty-http-8.1.15.v20140411.jar"/>+ <library name="jetty-io-8.1.15.v20140411.jar"/>+ <library name="jetty-util-8.1.15.v20140411.jar"/>+ <library name="jetty-websocket-8.1.15.v20140411.jar"/>+ <library name="jna-3.4.0.jar"/>+ <library name="nekohtml-1.9.21.jar"/>+ <library name="netty-3.5.2.Final.jar"/>+ <library name="operadriver-1.5.jar"/>+ <library name="operalaunchers-1.1.jar"/>+ <library name="platform-3.4.0.jar"/>+ <library name="protobuf-java-2.4.1.jar"/>+ <library name="sac-1.3.jar"/>+ <library name="selenium-api-2.44.0.jar"/>+ <library name="selenium-chrome-driver-2.44.0.jar"/>+ <library name="selenium-firefox-driver-2.44.0.jar"/>+ <library name="selenium-htmlunit-driver-2.44.0.jar"/>+ <library name="selenium-ie-driver-2.44.0.jar"/>+ <library name="selenium-java-2.44.0.jar"/>+ <library name="selenium-remote-driver-2.44.0.jar"/>+ <library name="selenium-safari-driver-2.44.0.jar"/>+ <library name="selenium-support-2.44.0.jar"/>+ <library name="serializer-2.7.1.jar"/>+ <library name="webbit-0.4.14.jar"/>+ <library name="xalan-2.7.1.jar"/>+ <library name="xercesImpl-2.11.0.jar"/>+ <library name="xml-apis-1.4.01.jar"/></runtime><requires>- <library name="selenium-java-2.4.0.jar">+ <library name="selenium-java-2.44.0.jar"><export name="*"/></library><library name="operadriver-1.5.jar">diff --git a/src/plugin/lib-selenium/src/java/org/apache/nutch/protocol/selenium/HttpWebClient.java b/src/plugin/lib-selenium/src/java/org/apache/nutch/protocol/selenium/HttpWebClient.javaindex 98a48c51f..26c3c3127 100644--- a/src/plugin/lib-selenium/src/java/org/apache/nutch/protocol/selenium/HttpWebClient.java+++ b/src/plugin/lib-selenium/src/java/org/apache/nutch/protocol/selenium/HttpWebClient.java@@ -1,4 +1,4 @@-/**+/** Licensed to the Apache Software Foundation (ASF) under one or more* contributor license agreements. See the NOTICE file distributed with* this work for additional information regarding copyright ownership.@@ -17,19 +17,32 @@package org.apache.nutch.protocol.selenium;import org.apache.hadoop.conf.Configuration;+import org.apache.hadoop.fs.FSDataOutputStream;+import org.apache.hadoop.fs.FileSystem;+import org.apache.hadoop.fs.Path;+import org.apache.hadoop.io.IOUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.openqa.selenium.By;+import org.openqa.selenium.OutputType;+import org.openqa.selenium.TakesScreenshot;import org.openqa.selenium.WebDriver;+import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.firefox.FirefoxProfile;+import org.openqa.selenium.safari.SafariDriver;import org.openqa.selenium.support.ui.WebDriverWait;-+import com.opera.core.systems.OperaDriver;+import java.io.BufferedInputStream;+import java.io.File;+import java.io.FileInputStream;+import java.io.InputStream;+import java.io.OutputStream;import java.lang.String;public class HttpWebClient {- private static final Logger LOG = LoggerFactory.getLogger("org.apache.nutch.protocol");+ private static final Logger LOG = LoggerFactory.getLogger(HttpWebClient.class);public static ThreadLocal<WebDriver> threadWebDriver = new ThreadLocal<WebDriver>() {@@ -45,23 +58,67 @@ public class HttpWebClient {};};+ /**+ * Function for obtaining the HTML BODY using the selected+ * {@link org.openqa.selenium.WebDriver}.+ * There are a number of configuration properties within+ * <code>nutch-site.xml</code> which determine whether to+ * take screenshots of the rendered pages and persist them+ * as timestamped .png's into HDFS.+ * @param url the URL to fetch and render+ * @param conf the {@link org.apache.hadoop.conf.Configuration}+ * @return the rendered inner HTML page+ */public static String getHtmlPage(String url, Configuration conf) {- WebDriver driver = null;+ WebDriver driver = null;try {- driver = new FirefoxDriver();- //} WebDriver driver = threadWebDriver.get();- // if (driver == null) {- // driver = new FirefoxDriver();- // }-+ String driverType = conf.get("selenium.driver", "firefox");+ switch (driverType) {+ case "firefox":+ driver = new FirefoxDriver();+ break;+ case "chrome":+ driver = new ChromeDriver();+ break;+ case "safari":+ driver = new SafariDriver();+ break;+ case "opera":+ driver = new OperaDriver();+ break;+ default:+ LOG.error("The Selenium WebDriver choice {} is not available... defaulting to FirefoxDriver().", driverType);+ driver = new FirefoxDriver();+ break;+ }+ LOG.debug("Selenium {} WebDriver selected.", driverType);driver.get(url);// Wait for the page to load, timeout after 3 secondsnew WebDriverWait(driver, 3);- String innerHtml = driver.findElement(By.tagName("body")).getAttribute("innerHTML");+ if (conf.getBoolean("selenium.take.screenshot", false)) {+ File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);+ LOG.debug("In-memory screenshot taken of: {}", url);+ FileSystem fs = FileSystem.get(conf);+ Path screenshotPath = new Path(conf.get("selenium.screenshot.location") + "/" + srcFile.getName());+ if (screenshotPath != null) {+ OutputStream os = null;+ if (!fs.exists(screenshotPath)) {+ LOG.debug("No existing screenshot already exists... creating new file at {} {}.", screenshotPath, srcFile.getName());+ os = fs.create(screenshotPath);+ }+ InputStream is = new BufferedInputStream(new FileInputStream(srcFile));+ IOUtils.copyBytes(is, os, conf);+ LOG.debug("Screenshot for {} successfully saved to: {} {}", url, screenshotPath, srcFile.getName());+ } else {+ LOG.warn("Screenshot for {} not saved to HDFS (subsequently disgarded) as value for "+ + "'selenium.screenshot.location' is absent from nutch-site.xml.", url);+ }+ }+ String innerHtml = driver.findElement(By.tagName("body")).getAttribute("innerHTML");return innerHtml;// I'm sure this catch statement is a code smell ; borrowing it from lib-htmlunit@@ -75,4 +132,4 @@ public class HttpWebClient {public static String getHtmlPage(String url) {return getHtmlPage(url, null);}-}\ No newline at end of file+}diff --git a/src/plugin/protocol-selenium/ivy.xml b/src/plugin/protocol-selenium/ivy.xmlindex 2cfaaed1b..43c775a91 100644--- a/src/plugin/protocol-selenium/ivy.xml+++ b/src/plugin/protocol-selenium/ivy.xml@@ -42,6 +42,10 @@<dependency org="com.opera" name="operadriver" rev="1.5"><exclude org="org.seleniumhq.selenium" name="selenium-remote-driver" /></dependency>+ <dependency org="com.codeborne" name="phantomjsdriver" rev="1.2.1" >+ <exclude org="org.seleniumhq.selenium" name="selenium-remote-driver" />+ <exclude org="org.seleniumhq.selenium" name="selenium-java" />+ </dependency><!-- end selenium dependencies --></dependencies>