-
Notifications
You must be signed in to change notification settings - Fork 3
/
search_engine.js
211 lines (159 loc) · 5.51 KB
/
search_engine.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
require('dotenv').config();
// const { chromium } = require('playwright');
const cheerio = require('cheerio');
const { renderPage } = require('./page_pool');
const WEB_STORE = process.env.WEB_STORE;
/**
*
* @param {string} url
* @returns {boolean} true/false - whether url is valid or not
*/
function isValidUrl(url) {
const pattern = /^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/gm;
return pattern.test(url);
}
/**
*
* @param {string} inputString
* @returns {string} outputString with removed whitespaces
*/
function removeWhitespaceAndJoin(inputString) {
const stringWithoutWhitespace = inputString.replace(/\s/g, ''); // Replace whitespace with ''
const finalString = stringWithoutWhitespace.split('›').join('/'); // Split by '>' and join by ''
return finalString;
}
/**
*
* @param {string} inputString
* @returns
*/
function removeSubstringsBetweenAngles(inputString) {
// Regular expression to match substrings between '<' and '>'
const regex = /<[^>]*>/g;
// Replace matched substrings with an empty string
const stringWithoutSubstrings = inputString.replace(regex, '');
return stringWithoutSubstrings;
}
/**
*
* @param {string} htmlString
* @returns {string} - extracted text from html
*/
function extractTextFromHTML(htmlString) {
const noStyleHtml = htmlString.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '');
// Remove <script> tags and their content
const noScriptHtml = noStyleHtml.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
let $ = cheerio.load(noScriptHtml);
// Remove content between <style> and </style> tags
$('style').remove();
// Remove content between <script> and </script> tags
$('script').remove();
// Remove inline style attributes
$('[style]').removeAttr('style');
// console.log("after removing see html : ",$.html().substring(0,2000))
// Use .text() method to get the combined text from all elements
let extractedText = $.text().trim();
extractedText = extractedText.replace(/{[^}]+}/g, '');
// Remove extra white spaces (spaces, tabs, line breaks)
extractedText = extractedText.replace(/\s+/g, ' ');
return extractedText.trim();
}
/**
*
* @param {string} linkUrl
* @param {number} data_limit_per_page
* @returns {string} data on the given web source
*/
const retrive_data_from_web_page = async (linkUrl,data_limit_per_page) => {
// const page = await context.newPage();
const page = await renderPage();
try{
await page.goto(linkUrl,timeout=100_000);
}catch(err){
return '';
}
const pageContent = await page.textContent('body');
// Log the text content of the page
const textContent = extractTextFromHTML(pageContent).trim().substring(0, data_limit_per_page)
return textContent;
}
/**
*
* @param {number} links_threshold
* @param {number} data_limit_per_page
* @returns {Promise<Object.<string, string>>} search_result - a map containing key:linkUrl, value:data
*/
const search = async (search_query, links_threshold, data_limit_per_page = 10000,retried=false) => {
const search_result = {};
// Launch the browser
// const browser = await chromium.launch({ headless: false });
// const context = await browser.newContext();
// const page = await context.newPage();
const page = await renderPage();
const textareaSelector = 'textarea'; // Update this to your textarea selector
await page.goto(WEB_STORE,timeout=100_000);
await page.focus(textareaSelector);
await page.keyboard.type(search_query);
// Simulate hitting the "Enter" key
await page.keyboard.press('Enter');
await new Promise(resolve => setTimeout(resolve, 2000))
const linkElements = await page.$$('span a');
const all_links = []
for (const linkElement of linkElements) {
const hrefValue = await linkElement.getAttribute('href');
// const textContent = await linkElement.innerText();
// console.log("textContent : ",textContent);
// const linkUrl = removeWhitespaceAndJoin(textContent)
const linkUrl = hrefValue;
const isValid = isValidUrl(linkUrl)
if (isValid) {
if (all_links.length < links_threshold) {
all_links.push(linkUrl);
}
else break;
}
}
// console.log("all_links : ", all_links)
const pagesContent = []
const all_data_promises = [];
for (let linkUrl of all_links) {
all_data_promises.push(
retrive_data_from_web_page(linkUrl,data_limit_per_page)
)
}
const all_data=await Promise.all(all_data_promises);
for(let link_index=0;link_index<all_links.length;link_index++){
const curr_link=all_links[link_index]
const curr_data=all_data[link_index]
search_result[curr_link]=curr_data;
}
return search_result;
}
// (async () => {
// // Example usage:
// // const htmlString = `
// // <div>
// // This is <b>bold</b> and <i>italic</i>.
// // <style>
// // body {
// // background-color: lightblue;
// // }
// // </style>
// // <script>
// // alert("Hello!");
// // </script>
// // <script>
// // {"topSectionLinks":[{"title":"Home","url":"/","navbarId":"HOME"},{"title":"Gam
// // es & Quizzes","url":"/quiz/browse","navbarId":"QUIZZES"}
// // </script>
// // <p>Paragraph with <a href="#">link</a>.</p>
// // </div>
// // `;
// // const extractedText = extractTextFromHTML(htmlString);
// // console.log(extractedText);
// const search_result = await search("Who is Sundar Pichai", 3)
// console.log("search_result : ", search_result)
// })()
module.exports={
search:search
}