-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
196 lines (168 loc) · 5.7 KB
/
Copy pathmain.js
File metadata and controls
196 lines (168 loc) · 5.7 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
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const path = require('path');
const fs = require('fs').promises;
const XLSX = require('xlsx');
const MapsScraper = require('./scraper');
let mainWindow;
let scraper = null;
let scrapedResults = [];
async function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
mainWindow.loadFile('index.html');
// Open DevTools in development
if (process.env.NODE_ENV === 'development') {
mainWindow.webContents.openDevTools();
}
}
// Initialize app
app.whenReady().then(createWindow);
app.on('window-all-closed', async () => {
if (scraper) {
await scraper.cleanup();
}
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// Helper function to flatten nested object for CSV export
function flattenObject(obj, prefix = '') {
return Object.keys(obj).reduce((acc, k) => {
const pre = prefix.length ? prefix + '_' : '';
if (typeof obj[k] === 'object' && obj[k] !== null && !Array.isArray(obj[k])) {
Object.assign(acc, flattenObject(obj[k], pre + k));
} else {
acc[pre + k] = Array.isArray(obj[k]) ? obj[k].join('; ') : obj[k];
}
return acc;
}, {});
}
// Export functionality
async function exportResults(format, results) {
try {
const options = {
title: 'Save Scraping Results',
defaultPath: app.getPath('downloads'),
filters: [
{ name: format.toUpperCase(), extensions: [format.toLowerCase()] }
]
};
const { filePath } = await dialog.showSaveDialog(mainWindow, options);
if (!filePath) {
return { success: false, message: 'Export cancelled' };
}
switch (format) {
case 'csv': {
const flattenedResults = results.map(result => flattenObject(result));
const headers = Array.from(new Set(
flattenedResults.flatMap(obj => Object.keys(obj))
));
const csv = [
headers.join(','),
...flattenedResults.map(row =>
headers.map(header =>
JSON.stringify(row[header] || '')
).join(',')
)
].join('\n');
await fs.writeFile(filePath, csv, 'utf-8');
break;
}
case 'json': {
await fs.writeFile(filePath, JSON.stringify(results, null, 2), 'utf-8');
break;
}
case 'excel': {
const flattenedResults = results.map(result => flattenObject(result));
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.json_to_sheet(flattenedResults);
XLSX.utils.book_append_sheet(wb, ws, 'Scraping Results');
XLSX.writeFile(wb, filePath);
break;
}
default:
throw new Error('Unsupported export format');
}
return {
success: true,
message: `Results exported successfully to ${filePath}`
};
} catch (error) {
console.error('Export error:', error);
return {
success: false,
message: `Export failed: ${error.message}`
};
}
}
// IPC Handlers
ipcMain.on('start-scraping', async (event, data) => {
try {
const { searchTerm, location, resultCount, isHeadless } = data;
// Create new scraper instance
scraper = new MapsScraper({
onProgress: (progress) => {
event.reply('scraping-progress', progress);
},
onResult: (results) => {
scrapedResults = results;
event.reply('scraping-results', results);
}
});
// Initialize browser with Chrome check/download
try {
event.reply('scraping-progress', {
status: 'info',
message: 'Initializing browser and checking Chrome installation...'
});
await scraper.initialize(isHeadless);
} catch (error) {
throw new Error(`Browser initialization failed: ${error.message}`);
}
event.reply('scraping-progress', {
status: 'started',
message: 'Starting scraping process...'
});
const results = await scraper.scrape(searchTerm, location, resultCount);
scrapedResults = results;
event.reply('scraping-complete', {
message: 'Scraping completed successfully',
results
});
} catch (error) {
console.error('Scraping error:', error);
event.reply('scraping-error', {
message: error.message
});
} finally {
if (scraper) {
await scraper.cleanup();
scraper = null;
}
}
});
ipcMain.on('stop-scraping', async (event) => {
if (scraper) {
await scraper.stop();
await scraper.cleanup();
scraper = null;
event.reply('scraping-stopped', {
message: 'Scraping process stopped by user'
});
}
});
ipcMain.on('export-results', async (event, { format }) => {
const result = await exportResults(format, scrapedResults);
event.reply('export-complete', result);
});