Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions src/app/api/crawl/crawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ class Crawler {
private pages: Page[] = [];
private queue: { url: string; depth: number }[] = [];

constructor(private maxDepth = 2, private maxPages = 1) { }
constructor(private maxDepth = 2, private maxPages = 1) {}

async crawl(startUrl: string): Promise<Page[]> {
// Capture the hostname of the start URL
let startHostname;
try {
startHostname = new URL(startUrl).hostname;
} catch (error) {
throw new Error(`Invalid URL: ${startUrl}`);
}

// Add the start URL to the queue
this.addToQueue(startUrl);

Expand All @@ -34,8 +42,8 @@ class Crawler {
// Parse the HTML and add the page to the list of crawled pages
this.pages.push({ url, content: this.parseHtml(html) });

// Extract new URLs from the page HTML and add them to the queue
this.addNewUrlsToQueue(this.extractUrls(html, url), depth);
// Pass startHostname to addNewUrlsToQueue
this.addNewUrlsToQueue(this.extractUrls(html, url), depth, startHostname);
}

// Return the list of crawled pages
Expand All @@ -58,8 +66,22 @@ class Crawler {
this.queue.push({ url, depth });
}

private addNewUrlsToQueue(urls: string[], depth: number) {
this.queue.push(...urls.map(url => ({ url, depth: depth + 1 })));
private addNewUrlsToQueue(
urls: string[],
depth: number,
startHostname: string
) {
const filteredUrls = urls.filter((url) => {
try {
const hostname = new URL(url).hostname;
return hostname === startHostname;
} catch (e) {
console.error(`Invalid URL: ${url}`);
return false;
}
});

this.queue.push(...filteredUrls.map((url) => ({ url, depth: depth + 1 })));
}

private async fetchPage(url: string): Promise<string> {
Expand All @@ -80,8 +102,20 @@ class Crawler {

private extractUrls(html: string, baseUrl: string): string[] {
const $ = cheerio.load(html);
Copy link

@athrael-soju athrael-soju Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor observation: This will give a 'deprecated' warning. You can do import { load } from 'cheerio'; and use the load by itself instead.

const relativeUrls = $('a').map((_, link) => $(link).attr('href')).get() as string[];
return relativeUrls.map(relativeUrl => new URL(relativeUrl, baseUrl).href);
const relativeUrls = $('a')
.map((_, link) => $(link).attr('href'))
.get() as string[];

try {
const relativeMap = relativeUrls.map(
(relativeUrl) => new URL(relativeUrl, baseUrl).href
);

return relativeMap;
} catch (error) {
console.error('Error extracting URLs:', error);
return [];
}
}
}

Expand Down