-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
186 lines (146 loc) · 4.12 KB
/
index.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { DOMParser, /*Element,*/ } from "https://deno.land/x/[email protected]/deno-dom-wasm.ts";
import config from "./config.js"
// Functions and data
// Reconstruct links that only contain the path (and not hostname/protocol)
const reconstruct = (link) => {
if (link.startsWith("http")) {
return link;
}
if (link.indexOf("://") !== -1) {
return link;
}
let newlink = "";
if (link.startsWith("//")) {
link = link.replace("//", "/");
newlink = `https://${workingUrl.host}${link}`;
return newlink;
}
if (link.startsWith("/")) {
newlink = `https://${workingUrl.host}${link}`;
return newlink;
}
if (link.startsWith("#")) {
newlink = `${workingUrl.href}${link}`
return newlink
}
if (link[0] !== "/") { // could be broken
newlink = `${workingUrl.href}${link}`
return newlink;
}
}
const data = [/*
{
hostname: "en.wikipedia.org",
visitedAll: false,
visitedCount: 2,
links: [
{ link: "https://en.wikipedia.org", visited: true },
{ link: "https://en.wikipedia.org/wiki/Website", visited: true },
]
}*/
]
// Take the hostname, and return it's index in "data"
const getHostIndex = (urlObject) => {
let found = false;
let index;
data.forEach((host, i) => {
if (host.hostname === urlObject.hostname) {
found = true;
index = i;
}
})
if (found === false) {
return null;
}
else return index
}
// Change working url
const changeWorkingUrl = (/*urlObject*/) => {
data.every((el) => {
let changed = false
// switch website if visisted more times than "switchiness"
if (el.visitedCount >= config.switchiness) return true
el.links.every(link => {
if (link.visited === false) {
workingUrl = new URL(link.link);
link.visited = true;
changed = true;
return false;
}
else {
return true;
}
})
if (changed === true) return false
else return true
})
}
// Main app
const entry = new URL(config.entry)
let workingUrl = entry;
// Start crawl loop
while (true) {
let fetchResponse;
try { fetchResponse = await fetch(workingUrl.href) } catch { continue }
const rawdoc = await fetchResponse.text()
//DEBUG
console.log(fetchResponse.headers.get("content-type"))
console.log(workingUrl.href)
if (!fetchResponse.headers.get("content-type").startsWith("text/html")) {
changeWorkingUrl(workingUrl);
continue;
}
const document = new DOMParser().parseFromString(rawdoc, "text/html");
// forEach element with a href attribute
// should run once per link
document?.querySelectorAll('[href]')?.forEach(el => {
// get full path of link
let looplink = reconstruct(el.getAttribute("href"))
// skip iteration if getting full path fails
try {
looplink = new URL(looplink)
}
catch {
return;
}
// Checks
// Check if hostname exists in "data". if so, check if looplink is duplicate. if not, push looplink
let hostExists = false;
let hostIndex;
let linkExists = false;
data.forEach((host, i) => {
// check if hostname exists
if (host.hostname === looplink.hostname) {
hostExists = true;
hostIndex = i;
host.links.forEach(linkObj => {
if (linkObj.link === looplink.href) {
linkExists = true
}
})
}
})
// Create new object in "data" if hostname does not already exist
// new object contains looplink
if (!hostExists) {
data.push({
hostname: looplink.hostname,
visitedAll: false,
visitedCount: 0,
links: [
{ link: looplink.href, visited: false }
]
})
}
if (linkExists === false && hostIndex != null) {
data[hostIndex].links.push({ link: looplink.href, visited: false })
}
}) // end of forEach
// Increment visited counter
data[getHostIndex(workingUrl)].visitedCount++
console.log("visitedCount:", data[getHostIndex(workingUrl)].visitedCount )
// Change working URL
changeWorkingUrl(workingUrl);
// Write to file
Deno.writeTextFileSync("./files/file.json", JSON.stringify(data))
}