Apache Nutch is an extensible and scalable web crawler
Add support for Crawl-delay in robots.txt (NUTCH-293).
Andrzej Bialeckie8f7f1d8dabb
diff --git a/CHANGES.txt b/CHANGES.txtindex 35df9c098..cc7c3ded8 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -200,6 +200,12 @@ Trunk (unreleased changes)71. NUTCH-320 DmozParser does not output list of urls to stdoutbut to a log file instead. Original functionality restored.+72. NUTCH-271 - Add ability to limit crawling to the set of initially+ injected hosts (db.ignore.external.links) (Philippe Eugene,+ Stefan Neufeind via ab)++73. NUTCH-293 - Support for Crawl-Delay (Stefan Groschupf via ab)+Release 0.7 - 2005-08-171. Added support for "type:" in queries. Search results are limited/qualifieddiff --git a/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.java b/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.javaindex 9b9b06d8e..861dc602d 100644--- a/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.java+++ b/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.java@@ -183,12 +183,14 @@ public abstract class HttpBase implements Protocol {}}- String host = blockAddr(u);+ long crawlDelay = robots.getCrawlDelay(this, u);+ long delay = crawlDelay > 0 ? crawlDelay : serverDelay;+ String host = blockAddr(u, delay);Response response;try {response = getResponse(u, datum, false); // make a request} finally {- unblockAddr(host);+ unblockAddr(host, delay);}int code = response.getCode();@@ -298,7 +300,7 @@ public abstract class HttpBase implements Protocol {return useHttp11;}- private String blockAddr(URL url) throws ProtocolException {+ private String blockAddr(URL url, long crawlDelay) throws ProtocolException {String host;if (byIP) {@@ -346,7 +348,7 @@ public abstract class HttpBase implements Protocol {long now = System.currentTimeMillis();long sleep = 0;if (done == 0) { // address is still in use- sleep = serverDelay; // wait at least delay+ sleep = crawlDelay; // wait at least delay} else if (now < done) { // address is on holdsleep = done - now; // wait until its free@@ -359,14 +361,14 @@ public abstract class HttpBase implements Protocol {}}- private void unblockAddr(String host) {+ private void unblockAddr(String host, long crawlDelay) {synchronized (BLOCKED_ADDR_TO_TIME) {int addrCount = ((Integer)THREADS_PER_HOST_COUNT.get(host)).intValue();if (addrCount == 1) {THREADS_PER_HOST_COUNT.remove(host);BLOCKED_ADDR_QUEUE.addFirst(host);BLOCKED_ADDR_TO_TIME.put- (host, new Long(System.currentTimeMillis() + serverDelay));+ (host, new Long(System.currentTimeMillis() + crawlDelay));} else {THREADS_PER_HOST_COUNT.put(host, new Integer(addrCount - 1));}diff --git a/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/RobotRulesParser.java b/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/RobotRulesParser.javaindex 0ae701f66..6f3bbe15c 100644--- a/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/RobotRulesParser.java+++ b/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/RobotRulesParser.java@@ -73,6 +73,7 @@ public class RobotRulesParser implements Configurable {ArrayList tmpEntries = new ArrayList();RobotsEntry[] entries = null;long expireTime;+ long crawlDelay = -1;/***/@@ -126,6 +127,20 @@ public class RobotRulesParser implements Configurable {return expireTime;}+ /**+ * Get Crawl-Delay, in milliseconds. This returns -1 if not set.+ */+ public long getCrawlDelay() {+ return crawlDelay;+ }++ /**+ * Set Crawl-Delay, in milliseconds+ */+ public void setCrawlDelay(long crawlDelay) {+ this.crawlDelay = crawlDelay;+ }+/*** Returns <code>false</code> if the <code>robots.txt</code> file* prohibits us from accessing the given <code>path</code>, or@@ -352,6 +367,19 @@ public class RobotRulesParser implements Configurable {if (addRules)currentRules.addPrefix(path, true);}+ } else if ( (line.length() >= 12)+ && (line.substring(0, 12).equalsIgnoreCase("Crawl-Delay:"))) {+ doneAgents = true;+ long crawlDelay = -1;+ String delay = line.substring("Crawl-Delay:".length(), line.length()).trim();+ if (delay.length() > 0) {+ try {+ crawlDelay = Long.parseLong(delay) * 1000; // sec to millisec+ } catch (Exception e) {+ LOG.info("can not parse Crawl-Delay:" + e.toString());+ }+ currentRules.setCrawlDelay(crawlDelay);+ }}}@@ -386,10 +414,9 @@ public class RobotRulesParser implements Configurable {return rules;}- public boolean isAllowed(HttpBase http, URL url)- throws ProtocolException, IOException {+ private RobotRuleSet getRobotRulesSet(HttpBase http, URL url) {- String host = url.getHost();+ String host = url.getHost().toLowerCase(); // normalize to lower caseRobotRuleSet robotRules = (RobotRuleSet)CACHE.get(host);@@ -414,13 +441,22 @@ public class RobotRulesParser implements Configurable {CACHE.put(host, robotRules); // cache rules for host}+ return robotRules;+ }+ public boolean isAllowed(HttpBase http, URL url)+ throws ProtocolException, IOException {String path = url.getPath(); // check rulesif ((path == null) || "".equals(path)) {path= "/";}- return robotRules.isAllowed(path);+ return getRobotRulesSet(http, url).isAllowed(path);+ }++ public long getCrawlDelay(HttpBase http, URL url)+ throws ProtocolException, IOException {+ return getRobotRulesSet(http, url).getCrawlDelay();}private final static int BUFSIZE= 2048;