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_suffix(color): """Extracts the finish suffix (e.g., 'G', 'S', 'M') from a color string.""" if not color: return None return color.strip().split('-')[-1].upper() def get_finish_type(row_data): """Determines the finish type (GLOSS, SATIN, MAT, MIX) based on color suffixes.""" try: top_suffix = get_finish_suffix(row_data.get("color_top")) body_suffix = get_finish_suffix(row_data.get("color_body")) if not top_suffix and not body_suffix: return None # If one suffix is missing, assume it's the same as the other. top_suffix = top_suffix or body_suffix body_suffix = body_suffix or top_suffix suffix_to_category = { 'G': 'gloss', 'S': 'satin', 'M': 'mat', 'R': 'mat', 'MAT': 'mat', 'RAW': 'mat', } top_category = suffix_to_category.get(top_suffix) body_category = suffix_to_category.get(body_suffix) if not top_category or not body_category: return None # Suffix not in our map if top_category == body_category: return top_category.upper() if 'mat' in {top_category, body_category}: return 'MIX' except (KeyError, AttributeError): # This will catch if row_data is not a dict or keys are missing return None return None def process_row(row, mayo, counter): """Processes a single row from the sheet.""" if len(row) < 3: return None # Skip rows with insufficient columns link = row[1] nr_zam = row[2] if not link: return None 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_kc"), info.get("finish_s"), ] print(f"raw_data: {row_data}") return row_data except Exception as e: print(f"❌ Błąd podczas przetwarzania linku {link}: {e}") 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:]: processed_row = process_row(row, mayo, counter) if processed_row: rows_to_process.append(processed_row) counter += 1 # 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()