Skip to content

Commit 93e4389

Browse files
committed
fix: update URL filtering logic and enhance logging in WebCrawler
1 parent 7aa3813 commit 93e4389

3 files changed

Lines changed: 168 additions & 177 deletions

File tree

apps/api/src/scraper/WebScraper/crawler.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,15 @@ export class WebCrawler {
7979
const sitemapLinks = await this.tryFetchSitemapLinks(this.initialUrl);
8080
if (sitemapLinks.length > 0) {
8181
let filteredLinks = sitemapLinks
82-
.filter((link) => !this.visited.has(link) && this.filterURL(link))
82+
.filter((link) => {
83+
let u = link;
84+
if (link.startsWith("/")) {
85+
u = new URL(link, this.initialUrl).href;
86+
} else if (!link.startsWith("#") && !link.startsWith("mailto:")) {
87+
u = new URL(link, this.initialUrl).href;
88+
}
89+
return !this.visited.has(u) && this.filterURL(u);
90+
})
8391
.slice(0, this.limit);
8492
return filteredLinks.map((link) => ({ url: link, html: "" }));
8593
}
@@ -119,9 +127,19 @@ export class WebCrawler {
119127
);
120128

121129
const filteredUrls = urls
122-
.filter(
123-
(urlObj) => !this.visited.has(urlObj.url) && this.filterURL(urlObj.url)
124-
)
130+
.filter((urlObj) => {
131+
let u = urlObj.url;
132+
if (urlObj.url.startsWith("/")) {
133+
u = new URL(urlObj.url, this.initialUrl).href;
134+
} else if (
135+
!urlObj.url.startsWith("#") &&
136+
!urlObj.url.startsWith("mailto:")
137+
) {
138+
u = new URL(urlObj.url, this.initialUrl).href;
139+
}
140+
141+
return !this.visited.has(u) && this.filterURL(u);
142+
})
125143
.slice(0, limit);
126144

127145
return filteredUrls.map((filteredUrl) => ({
@@ -222,17 +240,17 @@ export class WebCrawler {
222240
return fullUrl;
223241
}
224242

225-
// Logger.debug(
226-
// `Link filtered out: ${fullUrl} with tests: isInternalLink: ${this.isInternalLink(
227-
// fullUrl
228-
// )}, allowExternalLinks: ${
229-
// this.allowExternalLinks
230-
// }, isSocialMediaOrEmail: ${this.isSocialMediaOrEmail(
231-
// fullUrl
232-
// )}, matchesExcludes: ${this.matchesExcludes(
233-
// fullUrl
234-
// )}, matchesIncludes: ${this.matchesIncludes(fullUrl)}`
235-
// );
243+
Logger.trace(
244+
`Link filtered out: ${fullUrl} with tests: isInternalLink: ${this.isInternalLink(
245+
fullUrl
246+
)}, allowExternalLinks: ${
247+
this.allowExternalLinks
248+
}, isSocialMediaOrEmail: ${this.isSocialMediaOrEmail(
249+
fullUrl
250+
)}, matchesExcludes: ${this.matchesExcludes(
251+
fullUrl
252+
)}, matchesIncludes: ${this.matchesIncludes(fullUrl)}`
253+
);
236254
return null;
237255
}
238256

apps/puppeteer-service-ts/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
"author": "Jeff Pereira",
1313
"license": "ISC",
1414
"dependencies": {
15-
"@ulixee/hero": "2.0.0-alpha.30",
16-
"@ulixee/hero-core": "2.0.0-alpha.30",
17-
"@ulixee/hero-plugin-utils": "2.0.0-alpha.30",
18-
"@ulixee/net": "2.0.0-alpha.30",
19-
"@ulixee/commons": "2.0.0-alpha.30",
20-
"@ulixee/hero-interfaces": "2.0.0-alpha.30",
15+
"@ulixee/hero": "2.0.0-alpha.31",
16+
"@ulixee/hero-core": "2.0.0-alpha.31",
17+
"@ulixee/hero-plugin-utils": "2.0.0-alpha.31",
18+
"@ulixee/net": "2.0.0-alpha.31",
19+
"@ulixee/commons": "2.0.0-alpha.31",
20+
"@ulixee/hero-interfaces": "2.0.0-alpha.31",
2121
"body-parser": "^1.20.2",
2222
"dotenv": "^16.4.5",
2323
"express": "^4.19.2",

0 commit comments

Comments
 (0)