1 package net.matuschek.examples; 2 3 6 7 import java.net.URL ; 8 9 import net.matuschek.spider.WebRobot; 10 import net.matuschek.spider.WebRobotCallback; 11 12 import org.apache.log4j.BasicConfigurator; 13 14 22 public class StopDownload { 23 24 class DownloadStopper implements WebRobotCallback { 25 26 27 int max = 0; 28 29 30 int count = 0; 31 32 33 WebRobot robot = null; 34 35 36 public DownloadStopper(int max, WebRobot robot) { 37 this.max = max; 38 this.robot = robot; 39 } 40 41 45 public void webRobotRetrievedDoc(String url, int size) { 46 count ++; 47 if (count >= max) { 48 robot.stopRobot(); 49 } 50 } 51 52 public void webRobotDone() {}; 54 public void webRobotSleeping(boolean sleeping) {} 55 public void webRobotUpdateQueueStatus(int length) {} 56 } 57 58 59 60 public StopDownload() { 61 } 62 63 64 65 public void run() throws Exception { 66 WebRobot robby = new WebRobot(); 67 robby.setStartURL(new URL ("http://www.matuschek.net")); 68 robby.setMaxDepth(1); 69 robby.setSleepTime(0); 70 71 DownloadStopper stopit = new DownloadStopper(5,robby); 73 robby.setWebRobotCallback(stopit); 74 75 robby.run(); 76 } 77 78 79 public static void main(String [] args) 80 throws Exception 81 { 82 BasicConfigurator.configure(); 83 StopDownload stopper = new StopDownload(); 84 stopper.run(); 85 } 86 } 87 88 | Popular Tags |