Update files
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import os
|
|
| 3 |
import time
|
| 4 |
import tempfile
|
| 5 |
import logging
|
|
|
|
| 6 |
from selenium import webdriver
|
| 7 |
from selenium.webdriver.common.by import By
|
| 8 |
from selenium.webdriver.common.by import By
|
|
@@ -34,6 +35,22 @@ logger = logging.getLogger(__name__)
|
|
| 34 |
os.environ['MPLCONFIGDIR'] = tempfile.mkdtemp()
|
| 35 |
logger.info(f"Matplotlib config directory set to: {os.environ['MPLCONFIGDIR']}")
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def setup_selenium():
|
| 38 |
try:
|
| 39 |
logger.info("Initializing Selenium Firefox driver...")
|
|
@@ -65,6 +82,7 @@ def setup_selenium():
|
|
| 65 |
# Configure service with log_path
|
| 66 |
service = Service(
|
| 67 |
executable_path=geckodriver_path,
|
|
|
|
| 68 |
log_path=os.path.join(tempfile.gettempdir(), 'geckodriver.log')
|
| 69 |
)
|
| 70 |
|
|
@@ -73,10 +91,27 @@ def setup_selenium():
|
|
| 73 |
service=service
|
| 74 |
# executable_path=os.path.join(os.getcwd(), 'geckodriver')
|
| 75 |
)
|
|
|
|
| 76 |
return driver
|
| 77 |
except Exception as e:
|
| 78 |
-
logger.error(
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
def form_input_text(word):
|
| 82 |
# word = 'go'
|
|
@@ -103,6 +138,8 @@ def fetch_sentences(input_text):
|
|
| 103 |
# Open a website
|
| 104 |
driver.get("https://copilot.microsoft.com/chat")
|
| 105 |
print(f"...after: driver.get('https://copilot.microsoft.com/chat')")
|
|
|
|
|
|
|
| 106 |
# Wait for an element to load
|
| 107 |
WebDriverWait(driver, 50).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
|
| 108 |
time.sleep(10) # Keeps the browser open for 10 seconds
|
|
@@ -190,9 +227,18 @@ def scrape_website(url):
|
|
| 190 |
# return fetch_sentences(form_input_text(current_word))
|
| 191 |
|
| 192 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
return fetch_sentences(form_input_text(current_word))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
except Exception as e:
|
| 195 |
-
|
|
|
|
| 196 |
|
| 197 |
|
| 198 |
# driver = None
|
|
|
|
| 3 |
import time
|
| 4 |
import tempfile
|
| 5 |
import logging
|
| 6 |
+
import subprocess
|
| 7 |
from selenium import webdriver
|
| 8 |
from selenium.webdriver.common.by import By
|
| 9 |
from selenium.webdriver.common.by import By
|
|
|
|
| 35 |
os.environ['MPLCONFIGDIR'] = tempfile.mkdtemp()
|
| 36 |
logger.info(f"Matplotlib config directory set to: {os.environ['MPLCONFIGDIR']}")
|
| 37 |
|
| 38 |
+
def verify_firefox_installation():
|
| 39 |
+
if not os.path.exists('/usr/bin/firefox-esr'):
|
| 40 |
+
logger.error("Firefox binary not found at /usr/bin/firefox-esr")
|
| 41 |
+
return False
|
| 42 |
+
return True
|
| 43 |
+
|
| 44 |
+
def verify_geckodriver():
|
| 45 |
+
try:
|
| 46 |
+
result = subprocess.run(['/app/geckodriver', '--version'],
|
| 47 |
+
capture_output=True, text=True)
|
| 48 |
+
logger.info(f"Geckodriver version: {result.stdout}")
|
| 49 |
+
return True
|
| 50 |
+
except:
|
| 51 |
+
logger.error("Geckodriver verification failed")
|
| 52 |
+
return False
|
| 53 |
+
|
| 54 |
def setup_selenium():
|
| 55 |
try:
|
| 56 |
logger.info("Initializing Selenium Firefox driver...")
|
|
|
|
| 82 |
# Configure service with log_path
|
| 83 |
service = Service(
|
| 84 |
executable_path=geckodriver_path,
|
| 85 |
+
service_args=['--marionette-port', '2828'],
|
| 86 |
log_path=os.path.join(tempfile.gettempdir(), 'geckodriver.log')
|
| 87 |
)
|
| 88 |
|
|
|
|
| 91 |
service=service
|
| 92 |
# executable_path=os.path.join(os.getcwd(), 'geckodriver')
|
| 93 |
)
|
| 94 |
+
logger.info("Selenium driver initialized successfully")
|
| 95 |
return driver
|
| 96 |
except Exception as e:
|
| 97 |
+
logger.error("Possible causes:")
|
| 98 |
+
logger.error("- Missing or incompatible geckodriver")
|
| 99 |
+
logger.error("- Firefox binary not found")
|
| 100 |
+
logger.error("- Insufficient permissions")
|
| 101 |
+
logger.error("- Missing system dependencies")
|
| 102 |
+
raise RuntimeError(f"Failed to initialize WebDriver: {str(e)}")
|
| 103 |
+
|
| 104 |
+
def check_selenium_environment():
|
| 105 |
+
try:
|
| 106 |
+
driver = setup_selenium()
|
| 107 |
+
driver.quit()
|
| 108 |
+
return True
|
| 109 |
+
except:
|
| 110 |
+
return False
|
| 111 |
+
|
| 112 |
+
if not check_selenium_environment():
|
| 113 |
+
logger.critical("Selenium environment check failed!")
|
| 114 |
+
exit(1)
|
| 115 |
|
| 116 |
def form_input_text(word):
|
| 117 |
# word = 'go'
|
|
|
|
| 138 |
# Open a website
|
| 139 |
driver.get("https://copilot.microsoft.com/chat")
|
| 140 |
print(f"...after: driver.get('https://copilot.microsoft.com/chat')")
|
| 141 |
+
|
| 142 |
+
logger.info("Waiting for page to load...")
|
| 143 |
# Wait for an element to load
|
| 144 |
WebDriverWait(driver, 50).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
|
| 145 |
time.sleep(10) # Keeps the browser open for 10 seconds
|
|
|
|
| 227 |
# return fetch_sentences(form_input_text(current_word))
|
| 228 |
|
| 229 |
try:
|
| 230 |
+
if not verify_firefox_installation():
|
| 231 |
+
return "Error: Firefox not properly installed"
|
| 232 |
+
|
| 233 |
+
current_word = 'go'
|
| 234 |
return fetch_sentences(form_input_text(current_word))
|
| 235 |
+
|
| 236 |
+
except RuntimeError as e:
|
| 237 |
+
logger.error(f"Runtime error: {str(e)}")
|
| 238 |
+
return f"System error: {str(e)}"
|
| 239 |
except Exception as e:
|
| 240 |
+
logger.error(f"Unexpected error: {str(e)}")
|
| 241 |
+
return f"Unexpected error occurred: {str(e)}"
|
| 242 |
|
| 243 |
|
| 244 |
# driver = None
|