-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawl.js
More file actions
86 lines (71 loc) · 1.87 KB
/
crawl.js
File metadata and controls
86 lines (71 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { JSDOM } from "jsdom";
function normalizeURL(url) {
const normURL = new URL(url);
let newURL = `${normURL.host}${normURL.pathname}`;
if (newURL.endsWith("/")) {
newURL = newURL.slice(0, -1);
}
return newURL;
}
function isAbsoluteURL(url) {
return url.startsWith("http://") || url.startsWith("https://");
}
function isRelativeURL(url) {
return !isAbsoluteURL(url);
}
function toAbsoluteURL(url, baseURL) {
return baseURL + url;
}
function isSameDomain(url, domain) {
return isRelativeURL(url) || url.startsWith(domain);
}
function getURLsFromHTML(htmlBody, baseURL) {
const dom = new JSDOM(htmlBody);
let anchors = dom.window.document.querySelectorAll("a");
let urls = [];
for (let i = 0; i < anchors.length; i++) {
let url = anchors[i].getAttribute("href");
if (isRelativeURL(url)) {
url = toAbsoluteURL(url, baseURL);
}
urls.push(url);
}
return urls;
}
async function fetchURLs(url) {
let resp = null;
try {
resp = await fetch(url);
} catch (err) {
console.error(`Error: ${err.message}`);
return [];
}
if (resp.status >= 400) {
console.error(`HTTP error on URL ${url}`);
return [];
}
if (resp.headers.get("content-type") != "text/html") {
console.error(`Content type is not text / html for URL ${url}`);
return [];
}
let htmlBody = await resp.text();
return getURLsFromHTML(htmlBody);
}
async function crawlPage(baseURL, currentURL = baseURL, pages = {}) {
if (!isSameDomain(currentURL, baseURL)) {
return pages;
}
let nCurrentURL = normalizeURL(currentURL);
if (nCurrentURL in pages) {
pages[nCurrentURL] += 1;
return pages;
} else {
pages[nCurrentURL] = 1;
}
let urls = await fetchURLs(currentURL);
for (let i = 0; i < urls.length; i++) {
crawlPage(baseURL, urls[i], pages);
}
return pages;
}
export { normalizeURL, getURLsFromHTML, crawlPage };