diff --git a/gsheet_api.py b/gsheet_api.py index b398fba..b11ee08 100644 --- a/gsheet_api.py +++ b/gsheet_api.py @@ -1,6 +1,8 @@ import logging +import time import gspread from google.oauth2.service_account import Credentials +from gspread.exceptions import APIError, WorksheetNotFound SCOPES = [ "https://www.googleapis.com/auth/spreadsheets", @@ -10,32 +12,69 @@ SCOPES = [ class GSheetAPI: def __init__(self, credentials_file="credentials.json"): """Inicjalizuje klienta API przy tworzeniu obiektu.""" - creds = Credentials.from_service_account_file(credentials_file, scopes=SCOPES) - self.client = gspread.authorize(creds) + def connect(): + creds = Credentials.from_service_account_file(credentials_file, scopes=SCOPES) + return gspread.authorize(creds) + + self.client = self._execute_with_retry(connect) logging.info("✅ Połączono z Google Sheets API.") + def _execute_with_retry(self, func, *args, **kwargs): + """Wykonuje funkcję z logiką ponawiania dla błędu API 503.""" + retries = 5 + delay = 60 + last_exception = None + for i in range(retries): + try: + return func(*args, **kwargs) + except APIError as e: + last_exception = e + # Sprawdzamy, czy błąd ma status code 503 (Service Unavailable) + if hasattr(e, 'response') and e.response.status_code == 503: + logging.warning( + f"Błąd API 503: Usługa niedostępna. Ponawiam za {delay}s... (Próba {i + 1}/{retries})" + ) + time.sleep(delay) + else: + # Inne błędy API rzucamy od razu dalej + raise + except Exception as e: + # Przechwytujemy inne potencjalne błędy sieciowe i rzucamy je dalej + logging.error(f"Wystąpił nieoczekiwany błąd podczas komunikacji z API: {e}") + raise + + logging.error(f"Operacja '{func.__name__}' nie powiodła się po {retries} próbach.") + if last_exception: + raise last_exception + # Fallback, gdyby pętla się zakończyła bez wyjątku + raise Exception(f"Operacja '{func.__name__}' nie powiodła się po {retries} próbach.") + + def list_sheets(self, doc_name): """Zwraca listę arkuszy w danym dokumencie.""" - spreadsheet = self.client.open(doc_name) - return [ws.title for ws in spreadsheet.worksheets()] + spreadsheet = self._execute_with_retry(self.client.open, doc_name) + worksheets = self._execute_with_retry(spreadsheet.worksheets) + return [ws.title for ws in worksheets] def get_sheet_data(self, doc_name, sheet_name): """Pobiera wszystkie dane z danego arkusza.""" - sheet = self.client.open(doc_name).worksheet(sheet_name) - return sheet.get_all_values() + spreadsheet = self._execute_with_retry(self.client.open, doc_name) + sheet = self._execute_with_retry(spreadsheet.worksheet, sheet_name) + return self._execute_with_retry(sheet.get_all_values) def ensure_worksheet(self, doc_name, sheet_name): """ Zwraca worksheet o danej nazwie. Tworzy nowy, jeśli nie istnieje. """ - spreadsheet = self.client.open(doc_name) + spreadsheet = self._execute_with_retry(self.client.open, doc_name) try: - ws = spreadsheet.worksheet(sheet_name) - except gspread.exceptions.WorksheetNotFound: + ws = self._execute_with_retry(spreadsheet.worksheet, sheet_name) + except WorksheetNotFound: logging.info(f"➕ Tworzę nowy arkusz: {sheet_name}") - ws = spreadsheet.add_worksheet(title=sheet_name, rows=100, cols=10) - ws.append_row(["#", "Link", "Nr zamówienia", "Model", "Wykończenie", "Kolor Top", "Kolor Body", "Kolor Neck", "Kolor Head", "Finish K/C", "Finish S"]) + ws = self._execute_with_retry(spreadsheet.add_worksheet, title=sheet_name, rows=100, cols=10) + header = ["#", "Link", "Nr zamówienia", "Model", "Wykończenie", "Kolor Top", "Kolor Body", "Kolor Neck", "Kolor Head", "Finish K/C", "Finish S"] + self._execute_with_retry(ws.append_row, header, value_input_option="USER_ENTERED") return ws def batch_append_unique_rows(self, doc_name, sheet_name, rows_data): @@ -49,30 +88,26 @@ class GSheetAPI: ws = self.ensure_worksheet(doc_name, sheet_name) - # 1. Pobierz wszystkie istniejące numery zamówień w JEDNYM zapytaniu logging.info("🔍 Sprawdzam istniejące numery zamówień w arkuszu docelowym...") - # existing_orders = set(ws.col_values(3)) - existing_orders = {str(x).strip() for x in ws.col_values(3)} + existing_orders_values = self._execute_with_retry(ws.col_values, 3) + existing_orders = {str(x).strip() for x in existing_orders_values} logging.debug(f"Znaleziono {len(existing_orders)} istniejących numerów.\n existing_orders: {existing_orders}") - # 2. Filtruj nowe wiersze, aby znaleźć tylko te unikalne unique_rows_to_add = [] for row in rows_data: order_number = str(row[2]).strip() if order_number not in existing_orders: unique_rows_to_add.append(row) - # Dodaj nowo dodany numer do seta, aby uniknąć duplikatów w ramach jednej paczki existing_orders.add(order_number) - # 3. Dodaj wszystkie unikalne wiersze w JEDNYM zapytaniu if unique_rows_to_add: logging.info(f"📝 Dodaję {len(unique_rows_to_add)} nowych unikalnych wierszy do arkusza {sheet_name}...") - ws.append_rows(unique_rows_to_add, value_input_option="USER_ENTERED") # type: ignore + self._execute_with_retry(ws.append_rows, unique_rows_to_add, value_input_option="USER_ENTERED") logging.info("✅ Zakończono dodawanie.") else: logging.info("ℹ️ Nie znaleziono żadnych nowych wierszy do dodania.") skipped_count = len(rows_data) - len(unique_rows_to_add) if skipped_count > 0: - logging.info(f"⏭️ Pominięto {skipped_count} wierszy, które już istniały w arkuszu.") \ No newline at end of file + logging.info(f"⏭️ Pominięto {skipped_count} wierszy, które już istniały w arkuszu.") diff --git a/processing.py b/processing.py index 8e45f5b..86ffe12 100644 --- a/processing.py +++ b/processing.py @@ -1,6 +1,16 @@ import re import logging +SUFFIX_TO_CATEGORY = { + 'G': 'gloss', + 'S': 'satin', + 'M': 'mat', + 'R': 'mat', + 'MAT': 'mat', + 'RAW': 'mat', + 'AG': 'aged', +} + def normalize(text): if not text: return "" @@ -11,7 +21,13 @@ 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() + known_suffixes = list(SUFFIX_TO_CATEGORY.keys()) + pattern = r'\b(' + '|'.join(known_suffixes) + r')\b' + matches = re.findall(pattern, color.upper()) + if matches: + return matches[-1] + return None + def get_finish_type(row_data): """Determines the finish type (GLOSS, SATIN, MAT, MIX) based on color suffixes.""" @@ -26,31 +42,25 @@ def get_finish_type(row_data): 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) + 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 + return None # Suffix not in our map if top_category == body_category: return top_category.upper() - if 'mat' in {top_category, body_category}: + # If categories are different, and one of them is 'mat' or 'aged', it's a MIX. + mix_triggers = {'mat', 'aged'} + categories = {top_category, body_category} + if categories.intersection(mix_triggers): 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):