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 # pobierz część po ostatnim myślniku top_suffix = color_top.split('-')[-1].upper() body_suffix = color_body.split('-')[-1].upper() if (top_suffix == 'G' or top_suffix == None) and body_suffix == 'G': return "GLOSS" elif (top_suffix == 'S' or top_suffix == None) and body_suffix == 'S': return "SATIN" elif top_suffix in ('G', 'S') and body_suffix == 'M': return "MIX" elif top_suffix in ('M', 'R', 'MAT', 'RAW') and body_suffix in ('M', 'R', 'MAT'): return "MAT" elif top_suffix in ('M', 'R', 'MAT', 'RAW') and body_suffix in ('G', 'S'): return "MIX" elif top_suffix is None and body_suffix in ('M', 'R', 'MAT'): 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_kc"), info.get("finish_s"), ] print(f"raw_data: {row_data}") 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()