-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-extractor.js
More file actions
91 lines (75 loc) · 2.48 KB
/
python-extractor.js
File metadata and controls
91 lines (75 loc) · 2.48 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
/**
* Python UI Automation Extractor
* Calls Python script to extract email data via Windows UI Automation
*/
import { spawn } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* Call Python script to extract email data
*/
export async function extractViaUIAutomation() {
return new Promise((resolve, reject) => {
const pythonScript = path.join(__dirname, 'email_extractor.py');
// Spawn Python process
const python = spawn('python', [pythonScript]);
let stdout = '';
let stderr = '';
python.stdout.on('data', (data) => {
stdout += data.toString();
});
python.stderr.on('data', (data) => {
stderr += data.toString();
});
python.on('close', (code) => {
if (code !== 0) {
console.error('Python script error:', stderr);
reject(new Error(`Python script exited with code ${code}`));
return;
}
try {
const result = JSON.parse(stdout);
resolve(result);
} catch (error) {
console.error('Failed to parse Python output:', stdout);
reject(error);
}
});
python.on('error', (error) => {
console.error('Failed to start Python:', error);
reject(error);
});
});
}
/**
* Extract email data from active window
*/
export async function extractFromAnyApp(windowInfo) {
console.log('🐍 Using Python UI Automation...');
try {
const result = await extractViaUIAutomation();
console.log('✅ Python extraction result:', result);
return {
isEmail: result.is_email || false,
confidence: result.is_email ? 90 : 50,
context: result.context || 'other',
sender: result.sender,
recipient: result.recipient,
subject: result.subject,
summary: result.body || result.window_title,
indicators: ['ui-automation', 'python']
};
} catch (error) {
console.error('Python UI Automation failed:', error);
// Fallback to basic detection
return {
isEmail: false,
confidence: 0,
context: 'other',
error: error.message,
indicators: ['ui-automation-failed']
};
}
}