121 lines
4.3 KiB
Python
121 lines
4.3 KiB
Python
import sys
|
|
import json
|
|
import time
|
|
import openpyxl
|
|
from PyQt6.QtWidgets import (
|
|
QApplication, QWidget, QPushButton, QLabel, QVBoxLayout, QFileDialog, QCheckBox, QHBoxLayout
|
|
)
|
|
from selenium import webdriver
|
|
from selenium.webdriver.common.by import By
|
|
from selenium.webdriver.common.keys import Keys
|
|
from selenium.webdriver.chrome.options import Options
|
|
|
|
class FileSearchApp(QWidget):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.initUI()
|
|
self.file_path = ""
|
|
self.extensions = []
|
|
|
|
def initUI(self):
|
|
layout = QVBoxLayout()
|
|
|
|
self.label = QLabel("Select an Excel file with numbers")
|
|
layout.addWidget(self.label)
|
|
|
|
self.btn_select = QPushButton("Choose File")
|
|
self.btn_select.clicked.connect(self.select_file)
|
|
layout.addWidget(self.btn_select)
|
|
|
|
self.ext_checkboxes = []
|
|
self.ext_list = [".zip", ".tiff", ".idml", ".indd", ".pptx", ".tif", ".psd"]
|
|
|
|
ext_layout = QHBoxLayout()
|
|
for ext in self.ext_list:
|
|
checkbox = QCheckBox(ext)
|
|
checkbox.stateChanged.connect(self.update_extensions)
|
|
self.ext_checkboxes.append(checkbox)
|
|
ext_layout.addWidget(checkbox)
|
|
layout.addLayout(ext_layout)
|
|
|
|
self.btn_start = QPushButton("Start Search")
|
|
self.btn_start.clicked.connect(self.start_search)
|
|
layout.addWidget(self.btn_start)
|
|
|
|
self.setLayout(layout)
|
|
self.setWindowTitle("File Search Tool")
|
|
self.setGeometry(300, 300, 400, 200)
|
|
|
|
def select_file(self):
|
|
file_name, _ = QFileDialog.getOpenFileName(self, "Select File", "", "Excel Files (*.xlsx)")
|
|
if file_name:
|
|
self.file_path = file_name
|
|
self.label.setText(f"Selected File: {file_name}")
|
|
|
|
def update_extensions(self):
|
|
self.extensions = [cb.text() for cb in self.ext_checkboxes if cb.isChecked()]
|
|
|
|
def load_cookies(self, driver, cookies_file):
|
|
with open(cookies_file, 'r') as file:
|
|
cookies = json.load(file)
|
|
for cookie in cookies:
|
|
if 'sameSite' not in cookie:
|
|
cookie['sameSite'] = 'Lax'
|
|
driver.add_cookie(cookie)
|
|
|
|
def get_file_link(self, driver, number):
|
|
search_box = driver.find_element(By.NAME, "fulltext")
|
|
search_box.clear()
|
|
search_box.send_keys(number)
|
|
search_box.send_keys(Keys.RETURN)
|
|
time.sleep(3)
|
|
|
|
for ext in self.extensions:
|
|
try:
|
|
file_link = driver.find_element(By.XPATH, f"//a[contains(@href, '{ext}')]" ).get_attribute("href")
|
|
if file_link:
|
|
return file_link
|
|
except Exception:
|
|
continue
|
|
return None
|
|
|
|
def start_search(self):
|
|
if not self.file_path:
|
|
self.label.setText("Select a file before starting!")
|
|
return
|
|
if not self.extensions:
|
|
self.label.setText("Select at least one file type!")
|
|
return
|
|
|
|
options = Options()
|
|
# options.add_argument("user-data-dir=/Users/your_user/Library/Application Support/Google/Chrome/Profile 1")
|
|
options.add_argument("user-data-dir=/Users/vadymsamoilenko/Library/Application Support/Google/Chrome/Profile 1")
|
|
options.add_argument("--remote-debugging-port=9223")
|
|
driver = webdriver.Chrome(options=options)
|
|
|
|
try:
|
|
driver.get("https://mmmspinco.brand-portal.adobe.com/mediaportal.html/content/dam/mac/mmmspinco")
|
|
time.sleep(5)
|
|
input("Log in through the browser and press Enter...")
|
|
|
|
wb = openpyxl.load_workbook(self.file_path)
|
|
sheet = wb.active
|
|
|
|
for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, min_col=1, max_col=1):
|
|
number = row[0].value
|
|
if number:
|
|
link = self.get_file_link(driver, str(number))
|
|
if link:
|
|
sheet.cell(row=row[0].row, column=2, value=link)
|
|
|
|
save_path = self.file_path.replace(".xlsx", "_updated.xlsx")
|
|
wb.save(save_path)
|
|
self.label.setText(f"File saved: {save_path}")
|
|
finally:
|
|
driver.quit()
|
|
|
|
if __name__ == "__main__":
|
|
app = QApplication(sys.argv)
|
|
ex = FileSearchApp()
|
|
ex.show()
|
|
sys.exit(app.exec())
|