Apache Nutch is an extensible and scalable web crawler
NUTCH-446 RobotRulesParser should ignore Crawl-delay values of other bots in robots.txt, contributed by Doğacan Güney
Sami Siren5ae900e24705
diff --git a/CHANGES.txt b/CHANGES.txtindex ebc88e5b0..acd7895af 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -11,6 +11,9 @@ Unreleased changes (1.0-dev)(Eelco Lempsink via ab)4. NUTCH-456 - Parse msexcel plugin speedup (Heiko Dietze via siren)++ 5. NUTCH-446 - RobotRulesParser should ignore Crawl-delay values of other+ bots in robots.txt (Dogacan Guney via siren)Release 0.9 - 2007-04-02diff --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 a8dcb8269..c5f166ccc 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@@ -389,15 +389,17 @@ public class RobotRulesParser implements Configurable {} 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());+ if (addRules) {+ 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);}- currentRules.setCrawlDelay(crawlDelay);}}}@@ -500,7 +502,7 @@ public class RobotRulesParser implements Configurable {/** command-line main for testing */public static void main(String[] argv) {- if (argv.length != 3) {+ if (argv.length < 3) {System.out.println("Usage:");System.out.println(" java <robots-file> <url-file> <agent-name>+");System.out.println("");@@ -513,7 +515,7 @@ public class RobotRulesParser implements Configurable {try {FileInputStream robotsIn= new FileInputStream(argv[0]);LineNumberReader testsIn= new LineNumberReader(new FileReader(argv[1]));- String[] robotNames= new String[argv.length - 1];+ String[] robotNames= new String[argv.length - 2];for (int i= 0; i < argv.length - 2; i++)robotNames[i]= argv[i+2];diff --git a/src/plugin/lib-http/src/test/org/apache/nutch/protocol/http/api/TestRobotRulesParser.java b/src/plugin/lib-http/src/test/org/apache/nutch/protocol/http/api/TestRobotRulesParser.javaindex d6365df98..364a4ca75 100644--- a/src/plugin/lib-http/src/test/org/apache/nutch/protocol/http/api/TestRobotRulesParser.java+++ b/src/plugin/lib-http/src/test/org/apache/nutch/protocol/http/api/TestRobotRulesParser.java@@ -262,6 +262,26 @@ public class TestRobotRulesParser extends TestCase {}}}++ public void testCrawlDelay() {+ RobotRulesParser p = new RobotRulesParser(new String[] { "nutchbot" });+ String delayRule1 = "User-agent: nutchbot" + CR ++ "Crawl-delay: 10" + CR ++ "User-agent: foobot" + CR ++ "Crawl-delay: 20" + CR ++ "User-agent: *" + CR ++ "Disallow:/baz" + CR;+ String delayRule2 = "User-agent: foobot" + CR ++ "Crawl-delay: 20" + CR ++ "User-agent: *" + CR ++ "Disallow:/baz" + CR;+ RobotRuleSet rules = p.parseRules(delayRule1.getBytes());+ long crawlDelay = rules.getCrawlDelay();+ assertTrue("testing crawl delay for agent nutchbot - rule 1", (crawlDelay == 10000));+ rules = p.parseRules(delayRule2.getBytes());+ crawlDelay = rules.getCrawlDelay();+ assertTrue("testing crawl delay for agent nutchbot - rule 2", (crawlDelay == -1));+ }// helper