-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexer.js
328 lines (298 loc) · 14.2 KB
/
indexer.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import { Builder, By, Key, until } from 'selenium-webdriver';
import chrome from 'selenium-webdriver/chrome.js';
import { EventEmitter } from 'events';
const indexerEvents = new EventEmitter();
import eventEmitter from './eventEmitter.js';
import { fetchGitHubContent } from './fetchGitHubContent.js';
import { displayLogMessage} from './blessed.js';
async function loadExistingData(baseUrl) {
let data_received = await fetchDataFromWorker(baseUrl, 'baseUrlSearch');
if (data_received) {
// console.log(`Data received from worker for ${baseUrl}:`); // Log the data received
let data = {
baseUrl: baseUrl,
data_received: data_received
};
eventEmitter.emit('dataSaved', { data });
return {
links: new Map(Object.entries(data_received.links)),
allLinks: new Set(Object.keys(data_received.links)),
totalWords: data_received.totalWords,
filteredTotalWords: data_received.filteredTotalWords
};
} else {
// console.log(`No data or links found for ${baseUrl}.`);
return {
links: new Map(),
allLinks: new Set(),
totalWords: 0,
filteredTotalWords: 0
};
}
}
async function saveLinks(links, baseUrl, totalWords, totalLinks, filteredTotalWords) {
saveData(links, baseUrl, totalWords, totalLinks, filteredTotalWords);
saveDataToWorker(baseUrl, totalWords, totalLinks, filteredTotalWords, links)
.catch(error => {
displayLogMessage(`Error: Failed to save data to worker asynchronously: ${error}`);
});
return
}
function saveData(links, baseUrl, totalWords, totalLinks, filteredTotalWords) {
let data_corpus = {
totalWords: totalWords,
totalLinks: totalLinks,
filteredTotalWords: filteredTotalWords,
links: links
};
let data = {
baseUrl: baseUrl,
data_received: data_corpus
};
eventEmitter.emit('dataSaved', { data });
}
const workerUrl = 'https://worker-aged-night-839d.i-f9f.workers.dev';
async function saveDataToWorker(baseUrl, totalWords, totalLinks, filteredTotalWords, links) {
// console.log('Saving data to worker');
try {
const data = {
type: 'indexData',
baseUrl: baseUrl,
totalWords: totalWords,
totalLinks: totalLinks,
filteredTotalWords: filteredTotalWords,
links: links // Ensure this is correctly structured for serialization
};
const response = await fetch(workerUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
const result = await response.json();
// console.log('result: ',result);
} catch (error) {
displayLogMessage(`Error: Failed to send data to worker: ${error}`);
}
}
async function fetchDataFromWorker(url, searchType) {
try {
const response = await fetch(workerUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ type: searchType, url: url })
});
if (!response.ok) {
const errorBody = await response.text();
if (response.status === 404) {
// console.log(`No data found for URL: ${url}`);
return null; // Return null for 404 Not Found
} else if (response.status === 422) {
// Handle the 422 Unprocessable Entity status
const errorData = JSON.parse(errorBody);
// console.log(`Data found but not suitable for URL: ${url}, Status: ${errorData.status}`);
return errorData; // Return the error data for further handling
}
displayLogMessage(`Error: HTTP error! status: ${response.status} ${response.statusText}`);
displayLogMessage(`Error: response body: ${errorBody}`);
// For other error statuses, you might still want to throw an error
throw new Error(`HTTP error! status: ${response.status} ${response.statusText}`);
}
const data = await response.json();
// console.log('Data received from worker:');
return data;
} catch (error) {
displayLogMessage(`Error: Failed to fetch data from worker: ${error}`);
// Instead of re-throwing the error, handle it gracefully
return { error: true, message: error.message };
}
}
function countWords(text) {
if (!text) return 0; // Return 0 if text is undefined or null
return text.split(/\s+/).filter(Boolean).length;
}
function countLineOccurrences(allTexts) {
let lineCounts = {};
allTexts.forEach(text => {
text.split('\n').forEach(line => {
lineCounts[line] = (lineCounts[line] || 0) + 1;
});
});
return lineCounts;
}
// Helper function to filter lines based on occurrences
function filterLines(text, lineCounts, maxOccurrences) {
return text.split('\n').filter(line => lineCounts[line] <= maxOccurrences).join('\n');
}
function buildDriver() {
let options = new chrome.Options();
// Set a custom user agent string
options.addArguments(`user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36`);
options.addArguments('--headless'); // Run in headless mode
options.addArguments('--disable-gpu'); // Disable GPU hardware acceleration
options.addArguments('--window-size=1920,1080');
// Initialize the Chrome driver with these options
let driver = new Builder()
.forBrowser('chrome')
.setChromeOptions(options)
.build();
return driver;
}
async function acceptCookies(driver) {
try {
// Wait for the cookie consent bar to appear and the button to be clickable
const cookieButton = await driver.wait(until.elementLocated(By.xpath("//button[contains(text(), 'Accept Cookies')]")), 1000);
await driver.wait(until.elementIsEnabled(cookieButton), 5000); // Ensure the button is clickable
await cookieButton.click();
return true; // Return true when cookies are successfully accepted
} catch (error) {
return false; // Return false if there was an error
}
}
async function indexLink(driver, url, allLinks, doneLinksCount, totalWords, filteredTotalWords, allTexts, maxOccurrences) {
try {
let allText; // Declare allText at the function scope
let wordCount;
let timestamp;
let cookieStatus = 'n/a';
let data_received = await fetchDataFromWorker(url, 'linkSearch');
let status = ''; // Declare status at the function scope
let extraData = null;
if (data_received && data_received.status === 'Looks good') {
// console.log('Data fetched successfully for URL:', url);
status = data_received.status; // Assign status from result
allText = data_received.content;
wordCount = data_received.word_count;
timestamp = data_received.indexed_timestamp;
// filteredText= result.filtered_content;
// filteredWordCount= result.filtered_word_count;
} else {
// console.log('proceeding with driver')
await driver.get(url);
let cookiesAccepted = await acceptCookies(driver); // Capture the return value
allText = await driver.executeScript("return document.documentElement.innerText");
wordCount = countWords(allText); // Use the robust countWords function
timestamp = new Date().toISOString();
cookieStatus = cookiesAccepted ? "Accepted" : "n/a";
let attempt = 0;
const maxAttempts = 3;
while (attempt < maxAttempts) {
try {
let linksElements = await driver.findElements(By.tagName('a'));
for (let link of linksElements) {
let href = await link.getAttribute('href');
if (href) {
let linkUrl = new URL(href, url); // Resolve relative URLs and ensure absolute URL
if (linkUrl.origin === new URL(url).origin) { // Check if the link belongs to the same domain
href = linkUrl.href.split('#')[0]; // Normalize and remove any fragment
if (!allLinks.has(href)) {
allLinks.add(href);
}
}
}
}
break;
} catch (error) {
attempt++;
if (attempt === maxAttempts) {
displayLogMessage(`Error: \nFailed to retrieve links after several attempts. Error: ${error}`);
}
}
}
if (allText.length < 100) {
status = 'Seems like it failed';
} else {
status = 'Looks good';
}
}
if (url.includes('github.com')) {
try {
// Fetch extra data from GitHub using the fetchGitHubContent function
extraData = await fetchGitHubContent(url);
allText = allText + '\n\n' + extraData;
} catch (error) {
displayLogMessage(`Error: fetching extra data for ${url}, ${error}`);
}
}
eventEmitter.emit('data_received', { message: `${doneLinksCount} / ${allLinks.size} links, ${filteredTotalWords} words processed` });
allTexts.push(allText); // Collect all texts for line occurrence counting
let lineCounts = countLineOccurrences(allTexts);
let filteredText = filterLines(allText, lineCounts, maxOccurrences); // Filter lines based on occurrences
let filteredWordCount = countWords(filteredText);
return { allLinks, allText, filteredText, status, wordCount, filteredWordCount, totalWords, cookieStatus, timestamp };
} catch (error) {
if (error.name === 'WebDriverError' && error.message.includes('net::ERR_NAME_NOT_RESOLVED')) {
displayLogMessage(`Error: The URL ${url} could not be resolved. It may be incorrect or the domain may be unreachable.`);
attempt++; // Increment the attempt counter
if (attempt < maxAttempts) {
driver = await buildDriver(); // Reinitialize the driver
return indexLink(driver, url, allLinks, doneLinksCount, totalWords, filteredTotalWords, allTexts, maxOccurrences); // Retry the operation
} else {
displayLogMessage(`Error: Maximum retry attempts reached, skipping URL: ${url}`);
return { allLinks, allText, filteredText, status: 'Seems like it failed', wordCount, filteredWordCount, totalWords, cookieStatus, timestamp };
}
} else {
displayLogMessage(`Error: indexing ${url}: ${error}`);
return { allLinks, allText, filteredText, status: 'Seems like it failed', wordCount, filteredWordCount, totalWords, cookieStatus, timestamp };
}
}
}
const MAX_WORDS = 1000000; // Limit of 1 million words
export async function main(baseUrl) {
let driver = buildDriver();
const maxOccurrences = 1;
let { links, allLinks, totalWords, filteredTotalWords} = await loadExistingData(baseUrl);
if (!links || links.size === 0) {
links = new Map([[baseUrl, { status: "not_indexed", word_count: 0 }]]);
allLinks = new Set();
totalWords = 0;
filteredTotalWords = 0;
}
let allTexts = [];
let doneLinksCount = 0;
for (let [url, info] of links) {
if (info.status === 'Looks good') {
doneLinksCount++;
}
}
try {
while (Array.from(links.values()).some(v => v.status === "not_indexed")) {
for (let [url, info] of links) {
if (info.status === "not_indexed") {
let result = await indexLink(driver, url, allLinks, doneLinksCount, totalWords, filteredTotalWords, allTexts, maxOccurrences);
if (!result) {
// console.error('No result returned from indexLink for URL:', url);
continue;
}
allLinks = result.allLinks;
totalWords += result.wordCount;
filteredTotalWords += result.filteredWordCount;
if (result.status === 'Looks good') {
doneLinksCount++;
}
eventEmitter.emit('data_received', { message: `${doneLinksCount} / ${allLinks.size} links, ${filteredTotalWords} words processed` });
// console.log('data_received', { message: `${doneLinksCount} / ${allLinks.size} links, ${filteredTotalWords} words processed` });
if (result.status === 'Seems like it failed') {
displayLogMessage(`Error: Failed to index ${url}. Exiting...`);
displayLogMessage(`Error: Content: ${result.allText}`);
if (url === baseUrl) {
let data = {baseUrl: baseUrl};
eventEmitter.emit('indexingFailed', { data });
}
return;
}
for (let link of allLinks) {
if (!links.has(link)) {
links.set(link, { status: "not_indexed", word_count: 0, filtered_word_count: 0, filtered_content: "", added_timestamp: result.timestamp });
}
}
links.set(url, { status: result.status, indexed_timestamp: result.timestamp, content: result.allText, word_count: result.wordCount, filtered_content: result.filteredText, filtered_word_count: result.filteredWordCount });
await saveLinks(Object.fromEntries(links), baseUrl, totalWords, allLinks.size, filteredTotalWords);
}
}
}
eventEmitter.emit('data_received', { message: `\nIndexing complete. Total links: ${allLinks.size}, Total words: ${totalWords}, Filtered total words: ${filteredTotalWords}`});
} finally {
await driver.quit();
}
return
}