125 lines
3.6 KiB
Python
125 lines
3.6 KiB
Python
from gsheet_api import GSheetAPI
|
|
from mayo import MayoSession
|
|
from dotenv import load_dotenv
|
|
import os
|
|
import re
|
|
|
|
load_dotenv()
|
|
|
|
# --- konfiguracja ---
|
|
DOC_NAME = os.getenv("DOC_NAME")
|
|
MAYO_URL = os.getenv("MAYO_URL")
|
|
LOGIN = os.getenv("MAYO_LOGIN")
|
|
PASSWORD = os.getenv("MAYO_PASSWORD")
|
|
RESULT_DOC = "gitary 2025"
|
|
|
|
def normalize(text):
|
|
if not text:
|
|
return ""
|
|
return re.sub(r"\s+", "", text)
|
|
|
|
def get_finish_type(row_data):
|
|
try:
|
|
color_top = row_data["color_top"].strip()
|
|
color_body = row_data["color_body"].strip()
|
|
|
|
if not color_top or not color_body:
|
|
return None
|
|
|
|
top_last_char = color_top[-1]
|
|
body_last_char = color_body[-1]
|
|
|
|
if top_last_char == 'G' and body_last_char == 'G':
|
|
return "GLOSS"
|
|
elif top_last_char == 'S' and body_last_char == 'S':
|
|
return "SATIN"
|
|
elif top_last_char in ('G', 'S') and body_last_char == 'M':
|
|
return "MIX"
|
|
elif top_last_char in ('M', 'R') and body_last_char in ('M', 'R'):
|
|
return "MAT"
|
|
except (KeyError, AttributeError):
|
|
return None
|
|
return None
|
|
|
|
def main():
|
|
# Inicjalizuj API raz na początku
|
|
gsheet_api = GSheetAPI()
|
|
|
|
print("📄 Pobieram listę arkuszy...")
|
|
try:
|
|
sheets = gsheet_api.list_sheets(DOC_NAME)
|
|
for i, name in enumerate(sheets):
|
|
print(f"{i+1}. {name}")
|
|
except Exception as e:
|
|
print(f"❌ Błąd podczas pobierania listy arkuszy: {e}")
|
|
return
|
|
|
|
sheet_name = input("\nWybierz arkusz do przetworzenia: ")
|
|
|
|
print(f"📋 Pobieram dane z arkusza: {sheet_name}")
|
|
try:
|
|
rows = gsheet_api.get_sheet_data(DOC_NAME, sheet_name)
|
|
except Exception as e:
|
|
print(f"❌ Błąd podczas pobierania danych z arkusza: {e}")
|
|
return
|
|
|
|
mayo = MayoSession(MAYO_URL, LOGIN, PASSWORD)
|
|
mayo.login()
|
|
|
|
rows_to_process = []
|
|
counter = 1
|
|
# Zakładamy: kolumna B = link, kolumna C = nr zam.
|
|
for row in rows[1:]:
|
|
if len(row) < 3:
|
|
continue # Pomiń wiersze, które nie mają wystarczającej liczby kolumn
|
|
|
|
link = row[1]
|
|
nr_zam = row[2]
|
|
|
|
if not link:
|
|
continue
|
|
|
|
print(f"\n🔗 Sprawdzam: {link}")
|
|
try:
|
|
info = mayo.get_order_info(link)
|
|
order_number = info["order_number"]
|
|
model = info["model"]
|
|
|
|
print(f"Nr z arkusza: {nr_zam}")
|
|
print(f"Nr ze strony: {order_number}")
|
|
print(f"Model: {model}")
|
|
|
|
if normalize(order_number) == normalize(nr_zam):
|
|
print("✅ Numer się zgadza")
|
|
else:
|
|
print("⚠️ Numer NIE pasuje!")
|
|
|
|
row_data = [
|
|
counter,
|
|
link,
|
|
nr_zam,
|
|
model,
|
|
get_finish_type(info),
|
|
info.get("color_top"),
|
|
info.get("color_body"),
|
|
info.get("color_neck"),
|
|
info.get("color_head"),
|
|
info.get("finish"),
|
|
]
|
|
|
|
rows_to_process.append(row_data)
|
|
counter += 1
|
|
|
|
except Exception as e:
|
|
print(f"❌ Błąd podczas przetwarzania linku {link}: {e}")
|
|
|
|
# Po zakończeniu pętli, dodaj wszystkie zebrane wiersze za jednym razem
|
|
if rows_to_process:
|
|
print(f"\n\n--- Podsumowanie ---")
|
|
print(f"Zebrano {len(rows_to_process)} wierszy do przetworzenia.")
|
|
gsheet_api.batch_append_unique_rows(RESULT_DOC, sheet_name, rows_to_process)
|
|
else:
|
|
print("\nNie zebrano żadnych danych do przetworzenia.")
|
|
|
|
if __name__ == "__main__":
|
|
main() |