Files
yankee-gnome-fire-consumer/SullyGnomeRobot.py
2025-03-10 13:52:44 -04:00

152 lines
3.8 KiB
Python

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.support.select import Select
import RestAPIClient
import DiskUtils
from selenium.webdriver import FirefoxOptions
def download(url):
print(f"launch -> SullyGnomeRobot -> download() -> {url}")
opts = FirefoxOptions()
opts.add_argument("--headless")
driver=webdriver.Firefox(options=opts)
#driver = webdriver.Firefox()
driver.get(url)
print("url / ", driver.current_url)
##########################################
print("stage / start / tblControl_length check")
try:
dataset_drop_down_element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.NAME, "tblControl_length"))
)
dataset_drop_down_element = Select(dataset_drop_down_element)
dataset_drop_down_element.select_by_visible_text("100")
except Exception as e:
print(f"Element with class name 'tblControl_length' not found: {e}")
RestAPIClient.addDownloadFailure(url, "Element with class name 'tblControl_length' not found")
driver.quit()
return False
print("stage / complete / tblControl_length check")
##########################################
print("stage / start / TableExportLinkButton check")
try:
WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.CLASS_NAME, "TableExportLinkButton"))
)
link = driver.find_element(By.CLASS_NAME, "TableExportLinkButton")
link.click()
except Exception as e:
print(f"Element with class name 'TableExportLinkButton' not found: {e}")
RestAPIClient.addDownloadFailure(url,"Element with class name 'TableExportLinkButton' not found")
driver.quit()
return False
print("stage / complete / TableExportLinkButton check")
##########################################
print("file download started. now checking if exist before closing")
counter = 0
max_iterations = 5
while True:
print(f"Loop iteration: {counter}")
counter += 1
if counter == max_iterations:
print(f"Loop exited after {max_iterations} iterations.")
print("shutting down the robot")
driver.quit()
print("robot shut down. breaking. and dying.")
RestAPIClient.addDownloadFailure(url,"file not found on disk")
print("stage / complete / database updated w/ FAILURE")
break
print("checking is file exists... [loop]")
existsAlreadyVersionOne = DiskUtils.check_download_exists_matching_url_version_one(url)
print("file already exists (v1) / ",existsAlreadyVersionOne)
if existsAlreadyVersionOne:
print(f"The file matching the url '{url}' DOES INDEED exists. (version 1)")
print("sleeping for 4 secs before shutting down robot")
time.sleep(4)
print("shutting down the robot")
driver.quit()
print("robot shut down. breaking. and dying.")
RestAPIClient.addDownloadSuccess(url)
print("stage / complete / database updated w/ success")
break
else:
print(f"The file matching the url '{url}' DOES NOT exist.")
print("sleeping for some time before checking again...");
time.sleep(1)
print(" file is finished downloading.moving on.")
##########################################
print(f"complete -> SullyGnomeRobot -> download() -> {url}")