-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.js
More file actions
315 lines (270 loc) · 11.8 KB
/
Copy pathscraper.js
File metadata and controls
315 lines (270 loc) · 11.8 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
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
const puppeteer = require('puppeteer-core');
const chromeFinder = require('chrome-finder');
const downloadChromium = require('download-chromium');
const path = require('path');
const fs = require('fs').promises;
class MapsScraper {
constructor(options = {}) {
this.browser = null;
this.page = null;
this.isRunning = false;
this.progressCallback = options.onProgress || (() => {});
this.resultCallback = options.onResult || (() => {});
}
async findOrDownloadChrome() {
try {
// First try to find installed Chrome
const chromePath = chromeFinder();
if (chromePath) {
return chromePath;
}
} catch (error) {
console.log('No Chrome installation found, downloading Chromium...');
}
// If no Chrome found, download Chromium
try {
const downloadPath = path.join(process.cwd(), 'chrome');
// Check if Chromium is already downloaded
try {
await fs.access(downloadPath);
return downloadPath;
} catch {
// Download if not exists
this.progressCallback({
status: 'info',
message: 'Downloading Chrome (required for first run)...'
});
const chromePath = await downloadChromium({
revision: '1000044',
installPath: downloadPath,
progressCallback: (progress) => {
this.progressCallback({
status: 'progress',
message: `Downloading Chrome: ${Math.round(progress * 100)}%`,
current: progress * 100,
total: 100
});
}
});
return chromePath;
}
} catch (error) {
throw new Error(`Failed to download Chrome: ${error.message}`);
}
}
async initialize(isHeadless = true) {
try {
const chromePath = await this.findOrDownloadChrome();
this.browser = await puppeteer.launch({
headless: isHeadless,
executablePath: chromePath,
defaultViewport: { width: 1366, height: 768 },
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
this.page = await this.browser.newPage();
await this.page.setDefaultNavigationTimeout(30000);
} catch (error) {
throw new Error(`Failed to initialize browser: ${error.message}`);
}
}
async wait(ms) {
await new Promise(resolve => setTimeout(resolve, ms));
}
async scrape(searchTerm, location, resultCount) {
try {
this.isRunning = true;
// Navigate to Google Maps
this.progressCallback({ status: 'info', message: 'Opening Google Maps...' });
await this.page.goto('https://www.google.com/maps');
await this.page.waitForSelector('#searchboxinput');
// Enter search query
const searchQuery = location ? `${searchTerm} in ${location}` : searchTerm;
this.progressCallback({ status: 'info', message: `Searching for: ${searchQuery}` });
await this.page.type('#searchboxinput', searchQuery);
// Click search button and wait for results
await this.page.click('#searchbox-searchbutton');
await this.page.waitForSelector('.hfpxzc');
// Collection for storing results
const results = new Set();
let noNewResultsStartTime = null;
const MAX_WAIT_TIME = 10000; // 10 seconds
while (results.size < resultCount && this.isRunning) {
const newResults = await this.extractVisibleResults();
const previousSize = results.size;
newResults.forEach(result => {
if (!Array.from(results).some(r => JSON.parse(r).name === result.name)) {
results.add(JSON.stringify(result));
}
});
this.progressCallback({
status: 'progress',
message: `Found ${results.size} results...`,
current: results.size,
total: resultCount
});
if (previousSize === results.size) {
if (!noNewResultsStartTime) {
noNewResultsStartTime = Date.now();
}
else if (Date.now() - noNewResultsStartTime >= MAX_WAIT_TIME) {
break;
}
} else {
noNewResultsStartTime = null;
}
if (results.size < resultCount) {
await this.scrollResultsList();
await this.wait(1000);
}
}
const parsedResults = [...results].map(r => JSON.parse(r));
const detailedResults = [];
for (const result of parsedResults.slice(0, resultCount)) {
if (!this.isRunning) break;
this.progressCallback({
status: 'info',
message: `Scraping business ${detailedResults.length + 1} of ${resultCount}`,
current: detailedResults.length + 1,
total: resultCount,
currentTask: `Getting details for: ${result.name}`
});
try {
const details = await this.getPlaceDetails(result);
detailedResults.push({ ...result, ...details });
this.resultCallback(detailedResults);
await this.wait(500);
} catch (error) {
console.error(`Error getting details for ${result.name}:`, error);
detailedResults.push({ ...result, error: 'Failed to get details' });
}
}
return detailedResults;
} catch (error) {
throw new Error(`Scraping failed: ${error.message}`);
}
}
async extractVisibleResults() {
return await this.page.evaluate(() => {
const results = [];
const items = document.querySelectorAll('.hfpxzc');
items.forEach(item => {
const name = item.getAttribute('aria-label');
const link = item.getAttribute('href');
if (name && link) {
results.push({
name: name,
link: link
});
}
});
return results;
});
}
async getPlaceDetails(result) {
try {
await this.page.goto(result.link, { waitUntil: 'networkidle0' });
await this.wait(1000);
return await this.page.evaluate(() => {
const getElement = (selector) => {
const element = document.querySelector(selector);
return element ? element.textContent.trim() : null;
};
// Basic Information
const name = document.querySelector('h1.DUwDvf')?.textContent?.trim();
const ratingElement = document.querySelector('.F7nice');
const rating = ratingElement ? parseFloat(ratingElement.textContent) : null;
const totalReviews = document.querySelector('.F7nice + span')?.textContent?.match(/\d+/)?.[0];
const category = document.querySelector('button.DkEaL')?.textContent?.trim();
// Contact & Location
const address = document.querySelector('button[data-item-id="address"]')?.textContent?.trim();
const phoneButton = document.querySelector('button[data-item-id^="phone:tel:"]');
const phone = phoneButton ? phoneButton.getAttribute('aria-label')?.replace('Phone: ', '') : null;
const website = document.querySelector('a[data-item-id="authority"]')?.href;
const plusCode = document.querySelector('button[data-item-id="oloc"]')?.textContent?.trim();
// Operating Hours
const hoursData = {};
const hoursRows = document.querySelectorAll('table.eK4R0e tbody tr');
hoursRows.forEach(row => {
const day = row.querySelector('.ylH6lf')?.textContent?.trim();
const hours = row.querySelector('.mxowUb')?.textContent?.trim();
if (day && hours) {
hoursData[day] = hours;
}
});
// Current Status
const openStatus = document.querySelector('.ZDu9vd')?.textContent?.trim();
// Reviews Summary
const reviewsStats = {};
const ratingBars = document.querySelectorAll('.Bd93Zb tr.BHOKXe');
ratingBars.forEach(bar => {
const stars = bar.querySelector('.yxmtmf')?.textContent?.trim();
const count = bar.getAttribute('aria-label')?.match(/\d+/)?.[0];
if (stars && count) {
reviewsStats[stars] = parseInt(count);
}
});
// Popular Topics
const topics = Array.from(document.querySelectorAll('.KNfEk .tXNTee'))
.map(topic => ({
name: topic.querySelector('.uEubGf')?.textContent?.trim(),
count: topic.querySelector('.bC3Nkc')?.textContent?.trim()
}))
.filter(topic => topic.name && topic.count);
// Amenities
const amenities = Array.from(document.querySelectorAll('.Io6YTe'))
.map(amenity => amenity.textContent.trim())
.filter(Boolean);
return {
basicInfo: {
name,
rating,
totalReviews: totalReviews ? parseInt(totalReviews) : null,
category
},
contact: {
address,
phone,
website,
plusCode
},
hours: {
currentStatus: openStatus,
schedule: hoursData
},
reviews: {
summary: reviewsStats,
topics
},
amenities,
scrapedAt: new Date().toISOString()
};
});
} catch (error) {
console.error('Error in getPlaceDetails:', error);
return {
error: error.message
};
}
}
async scrollResultsList() {
await this.page.evaluate(() => {
const resultContainer = document.querySelector('div[role="feed"]');
if (resultContainer) {
resultContainer.scrollTo(0, resultContainer.scrollHeight);
} else {
window.scrollTo(0, document.body.scrollHeight);
}
});
}
async stop() {
this.isRunning = false;
}
async cleanup() {
if (this.browser) {
await this.browser.close();
this.browser = null;
this.page = null;
}
}
}
module.exports = MapsScraper;