Skip to content

Commit

Permalink
Succesfully saving files to rosalution with auth; shouldn't run twice…
Browse files Browse the repository at this point in the history
… cause we can't confirm if it already exists T_T, would be more effort; am thinking to retrieve updated annotations, download for that file_id and compare the binary to see if they match;, in order to upload again
  • Loading branch information
SeriousHorncat committed Sep 26, 2024
1 parent 7849144 commit 98fdcaa
Showing 1 changed file with 68 additions and 36 deletions.
104 changes: 68 additions & 36 deletions etc/database/screen-capture-annotate/screen-capture-annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,41 +133,41 @@ def screencapture_dataset(self, unit_type, unit, unit_annotations, dataset):
image_name = today.strftime("%Y-%m-%d")
image_name = f"{unit}-{dataset['dataset']}-{image_name}"

# Set the path where the screenshot will be saved
print(f'{url}: Visiting', end='\r', flush=True)

self.driver.get(url)

width = self.driver.execute_script(
"return Math.max( document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth );"
)
height = self.driver.execute_script(
"return Math.max( document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight );"
)

self.driver.set_window_size(width, height)

if 'popup_selectors' in dataset:
for selector in dataset['popup_selectors']:
self.click_popup(url, selector)

try:
WebDriverWait(self.driver, 30).until(
EC.presence_of_element_located((dataset['selenium_by'], dataset["dom_attribute"]))
)

if "extra_dom_element_wait" in dataset:
WebDriverWait(self.driver, 30).until(
EC.presence_of_element_located((By.TAG_NAME,"circle"))
)
except TimeoutException as err:
print(f'{url}: Failed to locate visualization {err}', end='\r', flush=True)
return None

page_element = self.driver.find_element(dataset['selenium_by'], dataset["dom_attribute"])
# # Set the path where the screenshot will be saved
# print(f'{url}: Visiting', end='\r', flush=True)

# self.driver.get(url)

# width = self.driver.execute_script(
# "return Math.max( document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth );"
# )
# height = self.driver.execute_script(
# "return Math.max( document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight );"
# )

# self.driver.set_window_size(width, height)

# if 'popup_selectors' in dataset:
# for selector in dataset['popup_selectors']:
# self.click_popup(url, selector)

# try:
# WebDriverWait(self.driver, 30).until(
# EC.presence_of_element_located((dataset['selenium_by'], dataset["dom_attribute"]))
# )

# if "extra_dom_element_wait" in dataset:
# WebDriverWait(self.driver, 30).until(
# EC.presence_of_element_located((By.TAG_NAME,"circle"))
# )
# except TimeoutException as err:
# print(f'{url}: Failed to locate visualization {err}', end='\r', flush=True)
# return None

# page_element = self.driver.find_element(dataset['selenium_by'], dataset["dom_attribute"])
print(f'{url}: Found visualization, saving image', end='\r', flush=True)
file_name = f"tmp/{image_name}.png"
page_element.screenshot(file_name)
# page_element.screenshot(file_name)

print(f'{url}: Saving Operation Complete ', end='\n', flush=True)
return file_name
Expand Down Expand Up @@ -197,16 +197,48 @@ def capture_analysis(self, capture):
for unit in genomic_units[genomic_unit_type]:
unit_annotations = self.get_annotations(genomic_unit_type, unit)
for genomic_unit_dataset in DATASETS[genomic_unit_type]:
captured_dataset = capture.screencapture_dataset(genomic_unit_type, unit, unit_annotations, genomic_unit_dataset)
if captured_dataset:
captured_dataset[()]
captured_dataset_filepath = capture.screencapture_dataset(genomic_unit_type, unit, unit_annotations, genomic_unit_dataset)
if captured_dataset_filepath:
self.captured_datasets[(genomic_unit_type, unit, genomic_unit_dataset['dataset'])] = captured_dataset_filepath

def save_to_rosalution(self, rosalution_auth_header):
for entry in self.captured_datasets:
unit_type, unit, dataset = entry
api_url=f"{config['ROSALUTION_API_URL']}annotation/{unit}/{dataset}/attachment?genomic_unit_type={unit_type}"
filename = self.captured_datasets[entry].strip('/')[1]
files = {'upload_file': (filename, open(self.captured_datasets[entry], 'rb'), 'application/png', {'Expires': '0'})}
print(f'{entry}: Upload Operation Begin ', end='\r', flush=True)
response = requests.post(api_url, headers=rosalution_auth_header, files=files, verify=False)
# print(response.json())
# print(response.status_code)
result_text = "Success" if response.status_code == 201 or response.status_code == 200 else "Failed"
print(f'{entry}: Upload Operation {result_text} ', end='\n', flush=True)

rosalution_analyses = sys.argv[1:]

print(f'🔒 Authenticating with Rosalution...', end='\r', flush=True)
auth_headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
auth_data = f"grant_type=&scope=&client_id={config['ROSALUTION_CLIENT_ID']}&client_secret={config['ROSALUTION_CLIENT_SECRET']}"
auth_response = requests.post(f"{config['ROSALUTION_API_URL']}auth/token", headers=auth_headers, data=auth_data, verify=False)
auth_response_json = auth_response.json()
if 'access_token' not in auth_response_json:
print(f'🔒 Authenticating with Rosalution...Failed', end='\n', flush=True)
print(auth_response_json)
exit(2)

print(f'🔓 Authenticating with Rosalution...Complete', end='\n', flush=True)
rosalution_header = {
'Authorization': f"Bearer {auth_response_json['access_token']}"
}

print("Capturing Rosalution Analyses")
print(*[ f" 🧬 {analysis}" for analysis in rosalution_analyses], sep="\n")

with ScreenCaptureDatasets() as capture:
for analysis_name in rosalution_analyses:
analysis = RosalutionAnalysis(analysis_name)
analysis.capture_analysis(capture)
analysis.save_to_rosalution(rosalution_header)

0 comments on commit 98fdcaa

Please sign in to comment.