Spaces:
Running
Running
Commit
·
b0a85e5
1
Parent(s):
51ddbb2
Bug-Fixed
Browse files
server.py
CHANGED
|
@@ -39,7 +39,6 @@ class EmailService:
|
|
| 39 |
self.smtp_server = "smtp.gmail.com"; self.smtp_port = 587
|
| 40 |
if not self.sender_email or not self.password:
|
| 41 |
print("[Email] WARNING: Email credentials not found in secrets.")
|
| 42 |
-
|
| 43 |
def send_report(self, recipients, subject, body, attachments=None):
|
| 44 |
if not self.sender_email or not self.password: return False
|
| 45 |
try:
|
|
@@ -53,8 +52,7 @@ class EmailService:
|
|
| 53 |
with smtplib.SMTP(self.smtp_server, self.smtp_port) as server:
|
| 54 |
server.starttls(); server.login(self.sender_email, self.password); server.send_message(msg)
|
| 55 |
print(f"Email sent successfully to {', '.join(recipients)}"); return True
|
| 56 |
-
except Exception as e:
|
| 57 |
-
print(f"Failed to send email: {e}"); return False
|
| 58 |
|
| 59 |
class GoogleDriveService:
|
| 60 |
def __init__(self):
|
|
@@ -64,15 +62,12 @@ class GoogleDriveService:
|
|
| 64 |
from google.oauth2 import service_account
|
| 65 |
from googleapiclient.discovery import build
|
| 66 |
base64_creds = os.getenv('GDRIVE_SA_KEY_BASE64')
|
| 67 |
-
if not base64_creds or not self.folder_id:
|
| 68 |
-
raise ValueError("Google Drive secrets not found.")
|
| 69 |
creds_json = base64.b64decode(base64_creds).decode('utf-8'); creds_dict = json.loads(creds_json)
|
| 70 |
self.creds = service_account.Credentials.from_service_account_info(creds_dict, scopes=['https://www.googleapis.com/auth/drive'])
|
| 71 |
self.service = build('drive', 'v3', credentials=self.creds)
|
| 72 |
print("[G-Drive] Service initialized securely from secrets.")
|
| 73 |
-
except Exception as e:
|
| 74 |
-
print(f"[G-Drive] CRITICAL ERROR: Could not initialize Google Drive service: {e}")
|
| 75 |
-
|
| 76 |
def upload_file(self, filename, file_content):
|
| 77 |
if not self.service: return False
|
| 78 |
try:
|
|
@@ -82,18 +77,15 @@ class GoogleDriveService:
|
|
| 82 |
self.service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
| 83 |
print(f"[G-Drive] File '{filename}' uploaded successfully.")
|
| 84 |
return True
|
| 85 |
-
except Exception as e:
|
| 86 |
-
print(f"[G-Drive] ERROR: File upload failed: {e}"); return False
|
| 87 |
|
| 88 |
email_service = EmailService()
|
| 89 |
drive_service = GoogleDriveService()
|
| 90 |
|
| 91 |
def get_email_list():
|
| 92 |
try:
|
| 93 |
-
with open('config/emails.conf', 'r') as f:
|
| 94 |
-
|
| 95 |
-
except FileNotFoundError:
|
| 96 |
-
return []
|
| 97 |
|
| 98 |
def run_automation_process(session_id):
|
| 99 |
global bot_instance
|
|
@@ -102,7 +94,7 @@ def run_automation_process(session_id):
|
|
| 102 |
data = session_data.get(session_id, {}); csv_content = data.get('csv_content')
|
| 103 |
df = pd.read_csv(io.StringIO(csv_content));
|
| 104 |
if 'Status' not in df.columns: df.insert(1, 'Status', '')
|
| 105 |
-
patient_list = df[(df['Status'] != 'Done') & (df['Status'] != 'Bad')]['Name'].tolist()
|
| 106 |
socketio.emit('initial_stats', {'total': len(patient_list)})
|
| 107 |
results = bot_instance.process_patient_list(patient_list)
|
| 108 |
is_terminated = bot_instance.termination_event.is_set()
|
|
@@ -141,15 +133,13 @@ def status_page():
|
|
| 141 |
|
| 142 |
@socketio.on('connect')
|
| 143 |
def handle_connect():
|
| 144 |
-
print(f'Frontend connected. Allowed origin: {FRONTEND_ORIGIN}')
|
| 145 |
-
emit('email_list', {'emails': get_email_list()})
|
| 146 |
|
| 147 |
@socketio.on('initialize_session')
|
| 148 |
def handle_init(data):
|
| 149 |
session_id = 'user_session'; global bot_instance
|
| 150 |
session_data[session_id] = {'csv_content': data['content'], 'emails': data['emails'], 'filename': data['filename']}
|
| 151 |
-
if bot_instance:
|
| 152 |
-
bot_instance.shutdown()
|
| 153 |
bot_instance = QuantumBot(socketio, app)
|
| 154 |
|
| 155 |
is_success, error_message = bot_instance.initialize_driver()
|
|
@@ -160,31 +150,24 @@ def handle_init(data):
|
|
| 160 |
|
| 161 |
@socketio.on('start_login')
|
| 162 |
def handle_login(credentials):
|
| 163 |
-
if not bot_instance:
|
| 164 |
-
return emit('error', {'message': 'Bot not initialized.'})
|
| 165 |
is_success, error_message = bot_instance.login(credentials['username'], credentials['password'])
|
| 166 |
-
if is_success:
|
| 167 |
-
|
| 168 |
-
else:
|
| 169 |
-
emit('error', {'message': f'Login failed: {error_message}'})
|
| 170 |
|
| 171 |
@socketio.on('submit_otp')
|
| 172 |
def handle_otp(data):
|
| 173 |
-
if not bot_instance:
|
| 174 |
-
return emit('error', {'message': 'Bot not initialized.'})
|
| 175 |
is_success, error_message = bot_instance.submit_otp(data['otp'])
|
| 176 |
if is_success:
|
| 177 |
emit('login_successful')
|
| 178 |
session_id = 'user_session'
|
| 179 |
socketio.start_background_task(target=run_automation_process, args=(session_id,))
|
| 180 |
-
else:
|
| 181 |
-
emit('error', {'message': f'OTP failed: {error_message}'})
|
| 182 |
|
| 183 |
@socketio.on('terminate_process')
|
| 184 |
def handle_terminate():
|
| 185 |
-
if bot_instance:
|
| 186 |
-
print("Termination signal received.")
|
| 187 |
-
bot_instance.stop()
|
| 188 |
|
| 189 |
if __name__ == '__main__':
|
| 190 |
print("====================================================================")
|
|
|
|
| 39 |
self.smtp_server = "smtp.gmail.com"; self.smtp_port = 587
|
| 40 |
if not self.sender_email or not self.password:
|
| 41 |
print("[Email] WARNING: Email credentials not found in secrets.")
|
|
|
|
| 42 |
def send_report(self, recipients, subject, body, attachments=None):
|
| 43 |
if not self.sender_email or not self.password: return False
|
| 44 |
try:
|
|
|
|
| 52 |
with smtplib.SMTP(self.smtp_server, self.smtp_port) as server:
|
| 53 |
server.starttls(); server.login(self.sender_email, self.password); server.send_message(msg)
|
| 54 |
print(f"Email sent successfully to {', '.join(recipients)}"); return True
|
| 55 |
+
except Exception as e: print(f"Failed to send email: {e}"); return False
|
|
|
|
| 56 |
|
| 57 |
class GoogleDriveService:
|
| 58 |
def __init__(self):
|
|
|
|
| 62 |
from google.oauth2 import service_account
|
| 63 |
from googleapiclient.discovery import build
|
| 64 |
base64_creds = os.getenv('GDRIVE_SA_KEY_BASE64')
|
| 65 |
+
if not base64_creds or not self.folder_id: raise ValueError("Google Drive secrets not found.")
|
|
|
|
| 66 |
creds_json = base64.b64decode(base64_creds).decode('utf-8'); creds_dict = json.loads(creds_json)
|
| 67 |
self.creds = service_account.Credentials.from_service_account_info(creds_dict, scopes=['https://www.googleapis.com/auth/drive'])
|
| 68 |
self.service = build('drive', 'v3', credentials=self.creds)
|
| 69 |
print("[G-Drive] Service initialized securely from secrets.")
|
| 70 |
+
except Exception as e: print(f"[G-Drive] CRITICAL ERROR: Could not initialize Google Drive service: {e}")
|
|
|
|
|
|
|
| 71 |
def upload_file(self, filename, file_content):
|
| 72 |
if not self.service: return False
|
| 73 |
try:
|
|
|
|
| 77 |
self.service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
| 78 |
print(f"[G-Drive] File '{filename}' uploaded successfully.")
|
| 79 |
return True
|
| 80 |
+
except Exception as e: print(f"[G-Drive] ERROR: File upload failed: {e}"); return False
|
|
|
|
| 81 |
|
| 82 |
email_service = EmailService()
|
| 83 |
drive_service = GoogleDriveService()
|
| 84 |
|
| 85 |
def get_email_list():
|
| 86 |
try:
|
| 87 |
+
with open('config/emails.conf', 'r') as f: return [line.strip() for line in f if line.strip()]
|
| 88 |
+
except FileNotFoundError: return []
|
|
|
|
|
|
|
| 89 |
|
| 90 |
def run_automation_process(session_id):
|
| 91 |
global bot_instance
|
|
|
|
| 94 |
data = session_data.get(session_id, {}); csv_content = data.get('csv_content')
|
| 95 |
df = pd.read_csv(io.StringIO(csv_content));
|
| 96 |
if 'Status' not in df.columns: df.insert(1, 'Status', '')
|
| 97 |
+
patient_list = df[ (df['Status'] != 'Done') & (df['Status'] != 'Bad') ]['Name'].tolist()
|
| 98 |
socketio.emit('initial_stats', {'total': len(patient_list)})
|
| 99 |
results = bot_instance.process_patient_list(patient_list)
|
| 100 |
is_terminated = bot_instance.termination_event.is_set()
|
|
|
|
| 133 |
|
| 134 |
@socketio.on('connect')
|
| 135 |
def handle_connect():
|
| 136 |
+
print(f'Frontend connected. Allowed origin: {FRONTEND_ORIGIN}'); emit('email_list', {'emails': get_email_list()})
|
|
|
|
| 137 |
|
| 138 |
@socketio.on('initialize_session')
|
| 139 |
def handle_init(data):
|
| 140 |
session_id = 'user_session'; global bot_instance
|
| 141 |
session_data[session_id] = {'csv_content': data['content'], 'emails': data['emails'], 'filename': data['filename']}
|
| 142 |
+
if bot_instance: bot_instance.shutdown()
|
|
|
|
| 143 |
bot_instance = QuantumBot(socketio, app)
|
| 144 |
|
| 145 |
is_success, error_message = bot_instance.initialize_driver()
|
|
|
|
| 150 |
|
| 151 |
@socketio.on('start_login')
|
| 152 |
def handle_login(credentials):
|
| 153 |
+
if not bot_instance: return emit('error', {'message': 'Bot not initialized.'})
|
|
|
|
| 154 |
is_success, error_message = bot_instance.login(credentials['username'], credentials['password'])
|
| 155 |
+
if is_success: emit('otp_required')
|
| 156 |
+
else: emit('error', {'message': f'Login failed: {error_message}'})
|
|
|
|
|
|
|
| 157 |
|
| 158 |
@socketio.on('submit_otp')
|
| 159 |
def handle_otp(data):
|
| 160 |
+
if not bot_instance: return emit('error', {'message': 'Bot not initialized.'})
|
|
|
|
| 161 |
is_success, error_message = bot_instance.submit_otp(data['otp'])
|
| 162 |
if is_success:
|
| 163 |
emit('login_successful')
|
| 164 |
session_id = 'user_session'
|
| 165 |
socketio.start_background_task(target=run_automation_process, args=(session_id,))
|
| 166 |
+
else: emit('error', {'message': f'OTP failed: {error_message}'})
|
|
|
|
| 167 |
|
| 168 |
@socketio.on('terminate_process')
|
| 169 |
def handle_terminate():
|
| 170 |
+
if bot_instance: print("Termination signal received."); bot_instance.stop()
|
|
|
|
|
|
|
| 171 |
|
| 172 |
if __name__ == '__main__':
|
| 173 |
print("====================================================================")
|
worker.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
# worker.py
|
| 2 |
import time
|
| 3 |
import threading
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
from selenium import webdriver
|
| 7 |
from selenium.webdriver.chrome.service import Service as ChromeService
|
|
@@ -28,8 +27,8 @@ class QuantumBot:
|
|
| 28 |
# Use system Chromium installed in the image
|
| 29 |
options.binary_location = "/usr/bin/chromium"
|
| 30 |
|
| 31 |
-
# Do NOT set --user-data-dir; let ChromeDriver create a temp profile
|
| 32 |
-
# Use an ephemeral DevTools port to avoid collisions
|
| 33 |
options.add_argument("--remote-debugging-port=0")
|
| 34 |
|
| 35 |
# Stable/headless startup flags
|
|
@@ -47,7 +46,7 @@ class QuantumBot:
|
|
| 47 |
service = ChromeService(executable_path="/usr/bin/chromedriver")
|
| 48 |
self.driver = webdriver.Chrome(service=service, options=options)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
self.driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
|
| 52 |
'source': "Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"
|
| 53 |
})
|
|
|
|
| 1 |
# worker.py
|
| 2 |
import time
|
| 3 |
import threading
|
|
|
|
| 4 |
|
| 5 |
from selenium import webdriver
|
| 6 |
from selenium.webdriver.chrome.service import Service as ChromeService
|
|
|
|
| 27 |
# Use system Chromium installed in the image
|
| 28 |
options.binary_location = "/usr/bin/chromium"
|
| 29 |
|
| 30 |
+
# Do NOT set --user-data-dir; let ChromeDriver create a fresh temp profile per session
|
| 31 |
+
# Use an ephemeral DevTools port to avoid collisions in constrained environments
|
| 32 |
options.add_argument("--remote-debugging-port=0")
|
| 33 |
|
| 34 |
# Stable/headless startup flags
|
|
|
|
| 46 |
service = ChromeService(executable_path="/usr/bin/chromedriver")
|
| 47 |
self.driver = webdriver.Chrome(service=service, options=options)
|
| 48 |
|
| 49 |
+
# Reduce webdriver detection
|
| 50 |
self.driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
|
| 51 |
'source': "Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"
|
| 52 |
})
|