Commit b0a2c984 authored by Alexander Ross Melnick's avatar Alexander Ross Melnick
Browse files

Pain

parent 47ba6458
Loading
Loading
Loading
Loading
(352 B)

File changed.

No diff preview for this file type.

(3.84 KiB)

File changed.

No diff preview for this file type.

+2 −0
Original line number Diff line number Diff line
@@ -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
+212 B (15 KiB)

File changed.

No diff preview for this file type.

+11 −6
Original line number Diff line number Diff line
@@ -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;
@@ -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
@@ -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
@@ -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())));      

@@ -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