From ecc6949df992265875ac27cb75c59fe4f75536aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CSamoilenkoVadym=E2=80=9D?= <“samoylenko.vadym@gmail.com”> Date: Fri, 14 Feb 2025 12:20:48 +0000 Subject: [PATCH] release --- .DS_Store | Bin 0 -> 6148 bytes .gitignore | 181 ++++++++++++++++++++++++++ main.py | 376 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 557 insertions(+) create mode 100644 .DS_Store create mode 100644 .gitignore create mode 100644 main.py diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 best_ratio: + best_ratio = ratio + best_match = href + self.logger.info(f"Found better match: {href} (ratio: {ratio})") + + except Exception as e: + self.logger.error(f"Error processing link: {e}") + continue + + return best_match if best_match and best_ratio >= 0.5 else None + + except Exception as e: + self.logger.error(f"Error finding PSD links: {e}") + return None + + def process_row(self, row, total_rows): + if self.driver is None: + raise ValueError("WebDriver is not initialized") + + if not row or not hasattr(row[0], 'value'): + return None, None + + asset_id = row[0].value + file_name = row[2].value if len(row) >= 3 else None + found_link = None + + if not asset_id or not file_name: + return row[0].row if hasattr(row[0], 'row') else None, None + + self.status_label.text = f"Processing {asset_id}" + self.logger.info(f"Processing Asset ID: {asset_id} with filename: {file_name}") + + for attempt in range(MAX_RETRIES): + try: + self.driver.get(f"{PORTAL_URL}/aem/search.html") + time.sleep(SEARCH_DELAY) + + search_box = WebDriverWait(self.driver, WAIT_TIMEOUT).until( + EC.presence_of_element_located((By.CSS_SELECTOR, "input[name='fulltext']")) + ) + search_box.clear() + search_box.send_keys(asset_id) + search_box.send_keys(Keys.RETURN) + + WebDriverWait(self.driver, LONG_TIMEOUT).until( + EC.presence_of_element_located((By.ID, "granite-omnisearch-result")) + ) + time.sleep(SEARCH_DELAY) + + asset_element = WebDriverWait(self.driver, WAIT_TIMEOUT).until( + EC.presence_of_element_located(( + By.XPATH, + f"//coral-card-subtitle[contains(@class, 'foundation-collection-item-subtitle') and text()='{file_name}']" + )) + ) + + if asset_element: + asset_card = asset_element.find_element(By.XPATH, "./ancestor::coral-card") + self.driver.execute_script("arguments[0].scrollIntoView(true);", asset_card) + time.sleep(1) + + self.driver.execute_script(""" + var element = arguments[0]; + var rect = element.getBoundingClientRect(); + var event = new MouseEvent('mouseover', { + 'view': window, + 'bubbles': true, + 'cancelable': true, + 'clientX': rect.left + rect.width/2, + 'clientY': rect.top + rect.height/2 + }); + element.dispatchEvent(event); + """, asset_card) + time.sleep(1) + + properties_button = WebDriverWait(self.driver, 10).until( + EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='Properties']")) + ) + self.driver.execute_script("arguments[0].click();", properties_button) + time.sleep(2) + + psd_links = WebDriverWait(self.driver, LONG_TIMEOUT).until( + EC.presence_of_all_elements_located(( + By.XPATH, + "//div[contains(@class, 'references-referencing')]//a[contains(@data-asset-path, '.psd')]" + )) + ) + + if psd_links: + found_link = self.find_matching_link(file_name, psd_links) + if found_link: + break + else: + self.logger.warning(f"Asset card not found for {asset_id}") + + except Exception as e: + self.logger.error(f"Error processing {asset_id} (attempt {attempt + 1}): {e}") + if attempt == MAX_RETRIES - 1: + break + time.sleep(2) + + if hasattr(row[0], 'row'): + progress = (row[0].row / total_rows) * 100 if total_rows > 0 else 0 + self.progress.value = progress + + return row[0].row if hasattr(row[0], 'row') else None, found_link + + def start_search(self, instance): + if not self.file_path or self.continue_button.disabled: + popup = Popup( + title='Warning', + content=Label(text='Ensure you are logged in before starting!'), + size_hint=(0.8, 0.3) + ) + popup.open() + return + + try: + wb = openpyxl.load_workbook(self.file_path) + if wb is None: + raise ValueError("Failed to load workbook") + + sheet = wb.active + if sheet is None: + raise ValueError("Failed to get active sheet") + + total_rows = sheet.max_row - 1 if sheet.max_row > 1 else 1 + + for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row): + try: + row_num, found_link = self.process_row(row, total_rows) + if row_num is not None: + sheet.cell(row=row_num, column=2, value=found_link if found_link else "Links not found") + save_path = self.file_path.replace(".xlsx", "_updated.xlsx") + wb.save(save_path) + except Exception as e: + self.logger.error(f"Error processing row: {e}") + continue + + self.logger.info("Processing completed") + success_popup = Popup( + title='Success', + content=Label(text=f"Processing completed. File saved."), + size_hint=(0.8, 0.3) + ) + success_popup.open() + + except Exception as e: + self.logger.error(f"Error during search: {e}") + error_popup = Popup( + title='Error', + content=Label(text=f"An error occurred: {str(e)}"), + size_hint=(0.8, 0.3) + ) + error_popup.open() + finally: + if self.driver: + self.driver.quit() + +if __name__ == '__main__': + FileSearchApp().run() \ No newline at end of file