Changed the file name from GUI main.py to main.py
This commit is contained in:
parent
72fc1ce571
commit
0ebb07134c
3 changed files with 167 additions and 167 deletions
64
11main.py
Normal file
64
11main.py
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
import json
|
||||
import time
|
||||
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
|
||||
import openpyxl
|
||||
|
||||
def load_cookies(driver, cookies_file):
|
||||
"""Load cookies from a file and add them to the browser."""
|
||||
with open(cookies_file, 'r') as file:
|
||||
cookies = json.load(file)
|
||||
for cookie in cookies:
|
||||
if 'sameSite' not in cookie or cookie['sameSite'] not in ['Strict', 'Lax', 'None']:
|
||||
cookie['sameSite'] = 'Lax'
|
||||
driver.add_cookie(cookie)
|
||||
|
||||
def get_file_link(driver, number):
|
||||
"""Search and get the file link by 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)
|
||||
|
||||
try:
|
||||
file_link = driver.find_element(By.XPATH,"//a[contains(@href, '.psd') or contains(@href, '.indd') or contains(@href, '.tif') or contains(@href, '.tiff') or contains(@href, '.ai')]").get_attribute("href")
|
||||
# file_link = driver.find_element(By.XPATH, "//a[contains(@href, '.psd')]").get_attribute("href")
|
||||
return file_link
|
||||
except Exception as e:
|
||||
print(f"Error getting link for number {number}: {e}")
|
||||
return None
|
||||
|
||||
def main():
|
||||
# Set up the web driver to work with an already running browser
|
||||
options = Options()
|
||||
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("Please log in manually in the browser, then press Enter to continue...")
|
||||
|
||||
|
||||
wb = openpyxl.load_workbook("data.xlsx")
|
||||
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 = get_file_link(driver, str(number))
|
||||
if link:
|
||||
sheet.cell(row=row[0].row, column=2, value=link)
|
||||
|
||||
wb.save("data.xlsx")
|
||||
finally:
|
||||
driver.quit()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
121
GUI main.py
121
GUI main.py
|
|
@ -1,121 +0,0 @@
|
|||
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())
|
||||
149
main.py
149
main.py
|
|
@ -1,64 +1,121 @@
|
|||
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
|
||||
import openpyxl
|
||||
|
||||
def load_cookies(driver, cookies_file):
|
||||
"""Load cookies from a file and add them to the browser."""
|
||||
with open(cookies_file, 'r') as file:
|
||||
cookies = json.load(file)
|
||||
for cookie in cookies:
|
||||
if 'sameSite' not in cookie or cookie['sameSite'] not in ['Strict', 'Lax', 'None']:
|
||||
cookie['sameSite'] = 'Lax'
|
||||
driver.add_cookie(cookie)
|
||||
class FileSearchApp(QWidget):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.initUI()
|
||||
self.file_path = ""
|
||||
self.extensions = []
|
||||
|
||||
def get_file_link(driver, number):
|
||||
"""Search and get the file link by 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)
|
||||
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)
|
||||
|
||||
try:
|
||||
file_link = driver.find_element(By.XPATH,"//a[contains(@href, '.psd') or contains(@href, '.indd') or contains(@href, '.tif') or contains(@href, '.tiff') or contains(@href, '.ai')]").get_attribute("href")
|
||||
# file_link = driver.find_element(By.XPATH, "//a[contains(@href, '.psd')]").get_attribute("href")
|
||||
return file_link
|
||||
except Exception as e:
|
||||
print(f"Error getting link for number {number}: {e}")
|
||||
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 main():
|
||||
# Set up the web driver to work with an already running browser
|
||||
options = Options()
|
||||
options.add_argument("user-data-dir=/Users/vadymsamoilenko/Library/Application Support/Google/Chrome/Profile 1")
|
||||
options.add_argument("--remote-debugging-port=9223")
|
||||
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
|
||||
|
||||
driver = webdriver.Chrome(options=options)
|
||||
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)
|
||||
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...")
|
||||
|
||||
input("Please log in manually in the browser, then press Enter to continue...")
|
||||
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)
|
||||
|
||||
wb = openpyxl.load_workbook("data.xlsx")
|
||||
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 = get_file_link(driver, str(number))
|
||||
if link:
|
||||
sheet.cell(row=row[0].row, column=2, value=link)
|
||||
|
||||
wb.save("data.xlsx")
|
||||
finally:
|
||||
driver.quit()
|
||||
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__":
|
||||
main()
|
||||
app = QApplication(sys.argv)
|
||||
ex = FileSearchApp()
|
||||
ex.show()
|
||||
sys.exit(app.exec())
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue