Loading Crawler/src/main/java/Debug.class (352 B) File changed.No diff preview for this file type. View original file View changed file Crawler/src/main/java/RegexParser.class (3.84 KiB) File changed.No diff preview for this file type. View original file View changed file Crawler/src/main/java/RegexParser.java +2 −0 Original line number Diff line number Diff line Loading @@ -30,6 +30,8 @@ public class RegexParser { return sentences; } public static List<String> extractLinks(String text) { List<String> links = new ArrayList<>(); // Regular expression pattern to match URLs Loading Crawler/src/main/java/ScratchCrawler.class +212 B (15 KiB) File changed.No diff preview for this file type. View original file View changed file Crawler/src/main/java/ScratchCrawler.java +11 −6 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.Iterator; Loading @@ -21,7 +22,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import java.nio.file.Files; import java.nio.file.Paths; public class ScratchCrawler { public static int MAX_PAGES = 100; // Maximum pages to crawl public static int TIMEOUT = 30; // Timeout for each page in seconds Loading Loading @@ -143,8 +143,6 @@ public class ScratchCrawler { int linksExtracted = 0; // Number of links extracted from the page while ((line = reader.readLine()) != null && pageContent.length() < max_storage) { // While there are lines to read pageContent.append(line); // Add the line to the page content // Extract links from the page List<String> links = RegexParser.extractLinks(line); // Extract the links from the line for (String link : links) { // For each link Loading @@ -153,7 +151,11 @@ public class ScratchCrawler { linksExtracted++; // Increment the number of links extracted } } pageContent.append(line); // Add the line to the page content } // Write the page content to the file, up to storage limit writer.println(pageContent.toString().substring(0, Math.min(max_storage, pageContent.length()))); Loading Loading @@ -386,11 +388,14 @@ public class ScratchCrawler { e.printStackTrace(); } }); long startTime = System.currentTimeMillis(); try { future.get(TIMEOUT, java.util.concurrent.TimeUnit.SECONDS); // Set a timeout of 30 seconds for each page future.get(TIMEOUT, TimeUnit.SECONDS); // Set a timeout of 30 seconds for each page } catch (Exception e) { future.cancel(true); // Cancel the future if (printStats) System.out.println("Page read timed out. Read took longer than " + TIMEOUT + " seconds."); long elapsedTime = System.currentTimeMillis() - startTime; System.out.println("Exception: " + e); if (printStats) System.out.println("Page read timed out. Read took longer than " + elapsedTime / 1000.0 + " seconds."); } executor.shutdownNow(); } Loading Loading
Crawler/src/main/java/Debug.class (352 B) File changed.No diff preview for this file type. View original file View changed file
Crawler/src/main/java/RegexParser.class (3.84 KiB) File changed.No diff preview for this file type. View original file View changed file
Crawler/src/main/java/RegexParser.java +2 −0 Original line number Diff line number Diff line Loading @@ -30,6 +30,8 @@ public class RegexParser { return sentences; } public static List<String> extractLinks(String text) { List<String> links = new ArrayList<>(); // Regular expression pattern to match URLs Loading
Crawler/src/main/java/ScratchCrawler.class +212 B (15 KiB) File changed.No diff preview for this file type. View original file View changed file
Crawler/src/main/java/ScratchCrawler.java +11 −6 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.Iterator; Loading @@ -21,7 +22,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import java.nio.file.Files; import java.nio.file.Paths; public class ScratchCrawler { public static int MAX_PAGES = 100; // Maximum pages to crawl public static int TIMEOUT = 30; // Timeout for each page in seconds Loading Loading @@ -143,8 +143,6 @@ public class ScratchCrawler { int linksExtracted = 0; // Number of links extracted from the page while ((line = reader.readLine()) != null && pageContent.length() < max_storage) { // While there are lines to read pageContent.append(line); // Add the line to the page content // Extract links from the page List<String> links = RegexParser.extractLinks(line); // Extract the links from the line for (String link : links) { // For each link Loading @@ -153,7 +151,11 @@ public class ScratchCrawler { linksExtracted++; // Increment the number of links extracted } } pageContent.append(line); // Add the line to the page content } // Write the page content to the file, up to storage limit writer.println(pageContent.toString().substring(0, Math.min(max_storage, pageContent.length()))); Loading Loading @@ -386,11 +388,14 @@ public class ScratchCrawler { e.printStackTrace(); } }); long startTime = System.currentTimeMillis(); try { future.get(TIMEOUT, java.util.concurrent.TimeUnit.SECONDS); // Set a timeout of 30 seconds for each page future.get(TIMEOUT, TimeUnit.SECONDS); // Set a timeout of 30 seconds for each page } catch (Exception e) { future.cancel(true); // Cancel the future if (printStats) System.out.println("Page read timed out. Read took longer than " + TIMEOUT + " seconds."); long elapsedTime = System.currentTimeMillis() - startTime; System.out.println("Exception: " + e); if (printStats) System.out.println("Page read timed out. Read took longer than " + elapsedTime / 1000.0 + " seconds."); } executor.shutdownNow(); } Loading