-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmail-interface.js
More file actions
206 lines (173 loc) · 7.16 KB
/
gmail-interface.js
File metadata and controls
206 lines (173 loc) · 7.16 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
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const { log, retry } = require('./utils');
/**
* Interface to run the Python Gmail creator script
* @param {Object} options - Configuration options
* @param {number} options.accountCount - Number of accounts to create
* @param {string} options.outputFile - Path to the output file (default: 'created.txt')
* @returns {Promise<Array<{email: string, password: string}>>} - Array of created accounts
*/
async function createGmailAccounts(options = {}) {
const {
accountCount = 1,
outputFile = path.join(__dirname, 'created.txt')
} = options;
// Make sure the output file doesn't exist before starting
if (fs.existsSync(outputFile)) {
fs.unlinkSync(outputFile);
}
// Path to the Python script
const scriptPath = path.join(__dirname, 'Gmail', 'gmail-creator.py');
// Check if the script exists
if (!fs.existsSync(scriptPath)) {
throw new Error(`Gmail creator script not found at ${scriptPath}`);
}
log('Starting Gmail account creation using Python script...', 'info');
// Install required Python dependencies first
log('Installing required Python dependencies...', 'info');
try {
const requirementsPath = path.join(__dirname, 'Gmail', 'requirements.txt');
await new Promise((resolve, reject) => {
// Use pip3 explicitly to ensure Python 3 is used
const pipProcess = spawn('pip3', ['install', '-r', requirementsPath]);
pipProcess.stdout.on('data', (data) => {
log(`Pip output: ${data.toString().trim()}`, 'info');
});
pipProcess.stderr.on('data', (data) => {
const errorText = data.toString().trim();
// Only log as warning if it's not a critical error
if (errorText.includes('WARNING') || errorText.includes('DEPRECATION')) {
log(`Pip warning: ${errorText}`, 'warn');
} else {
log(`Pip error: ${errorText}`, 'error');
}
});
pipProcess.on('close', (code) => {
if (code !== 0) {
log(`Pip process exited with code ${code}`, 'error');
reject(new Error(`Failed to install Python dependencies with code ${code}`));
} else {
log('Python dependencies installed successfully', 'success');
resolve();
}
});
pipProcess.on('error', (error) => {
log(`Error spawning pip process: ${error.message}`, 'error');
reject(new Error(`Failed to start pip: ${error.message}`));
});
});
} catch (error) {
log(`Failed to install Python dependencies: ${error.message}`, 'error');
throw new Error(`Failed to install Python dependencies: ${error.message}`);
}
// Create a modified version of the script with the account count
log(`Creating temporary script with ${accountCount} accounts...`, 'info');
const originalScript = fs.readFileSync(scriptPath, 'utf8');
// Update the config in the script to match the requested account count
const modifiedScript = originalScript.replace(
/\'account_count\'\s*:\s*\d+/,
`'account_count': ${accountCount}`
);
// Write the modified script to a temporary file
const tempScriptPath = path.join(__dirname, 'Gmail', 'temp-gmail-creator.py');
fs.writeFileSync(tempScriptPath, modifiedScript);
return new Promise((resolve, reject) => {
// Spawn the Python process
log('Running Python script...', 'info');
// Use python3 explicitly to ensure Python 3 is used
const pythonProcess = spawn('python3', [tempScriptPath]);
let stdoutData = '';
let stderrData = '';
// Collect stdout data
pythonProcess.stdout.on('data', (data) => {
const output = data.toString();
stdoutData += output;
log(`Python script output: ${output.trim()}`, 'info');
// Check for error indicators in the output and stop the process if found
if (output.includes("Exiting script due to error") ||
output.includes("Exiting script due to TimeoutException") ||
output.includes("Error finding username field")) {
log('Critical error detected in Python script output. Stopping the process.', 'error');
pythonProcess.kill(); // Kill the process
reject(new Error(`Gmail account creation failed: ${output}`));
}
});
// Collect stderr data
pythonProcess.stderr.on('data', (data) => {
const error = data.toString();
stderrData += error;
log(`Python script error: ${error.trim()}`, 'error');
// Always stop on stderr output
log('Error detected in Python script. Stopping the process.', 'error');
pythonProcess.kill(); // Kill the process
reject(new Error(`Gmail account creation failed: ${error}`));
});
// Handle process completion
pythonProcess.on('close', (code) => {
// Clean up the temporary script
if (fs.existsSync(tempScriptPath)) {
try {
fs.unlinkSync(tempScriptPath);
log('Temporary script cleaned up', 'info');
} catch (e) {
log(`Failed to clean up temporary script: ${e.message}`, 'warn');
}
}
if (code !== 0) {
log(`Python process exited with code ${code}`, 'error');
return reject(new Error(`Gmail account creation failed with code ${code}: ${stderrData}`));
}
log('Python Gmail creator completed successfully', 'success');
// Check if the output file exists
if (!fs.existsSync(outputFile)) {
return reject(new Error('Gmail account creation did not produce any output file'));
}
// Read and parse the output file
try {
const fileContent = fs.readFileSync(outputFile, 'utf8');
const accounts = parseCreatedAccounts(fileContent);
if (accounts.length === 0) {
return reject(new Error('No Gmail accounts were created'));
}
log(`Successfully created ${accounts.length} Gmail accounts`, 'success');
resolve(accounts);
} catch (error) {
reject(new Error(`Failed to parse Gmail accounts: ${error.message}`));
}
});
// Handle process errors
pythonProcess.on('error', (error) => {
log(`Error spawning Python process: ${error.message}`, 'error');
reject(new Error(`Failed to start Gmail creator: ${error.message}`));
});
});
}
/**
* Parse the created.txt file to extract account information
* @param {string} fileContent - Content of the created.txt file
* @returns {Array<{email: string, password: string}>} - Array of created accounts
*/
function parseCreatedAccounts(fileContent) {
const accounts = [];
const lines = fileContent.split('\n');
for (let i = 0; i < lines.length; i += 3) {
const usernameLine = lines[i];
const passwordLine = lines[i + 1];
if (usernameLine && passwordLine) {
const username = usernameLine.replace('Username: ', '').trim();
const password = passwordLine.replace('Password: ', '').trim();
if (username && password) {
accounts.push({
email: `${username}@gmail.com`,
password
});
}
}
}
return accounts;
}
module.exports = {
createGmailAccounts
};