nowy sufix i retry

This commit is contained in:
2026-05-07 15:13:48 +02:00
parent 0c9055122b
commit 1df295b2ed
2 changed files with 79 additions and 34 deletions

View File

@@ -1,6 +1,8 @@
import logging import logging
import time
import gspread import gspread
from google.oauth2.service_account import Credentials from google.oauth2.service_account import Credentials
from gspread.exceptions import APIError, WorksheetNotFound
SCOPES = [ SCOPES = [
"https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/spreadsheets",
@@ -10,32 +12,69 @@ SCOPES = [
class GSheetAPI: class GSheetAPI:
def __init__(self, credentials_file="credentials.json"): def __init__(self, credentials_file="credentials.json"):
"""Inicjalizuje klienta API przy tworzeniu obiektu.""" """Inicjalizuje klienta API przy tworzeniu obiektu."""
creds = Credentials.from_service_account_file(credentials_file, scopes=SCOPES) def connect():
self.client = gspread.authorize(creds) 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.") 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): def list_sheets(self, doc_name):
"""Zwraca listę arkuszy w danym dokumencie.""" """Zwraca listę arkuszy w danym dokumencie."""
spreadsheet = self.client.open(doc_name) spreadsheet = self._execute_with_retry(self.client.open, doc_name)
return [ws.title for ws in spreadsheet.worksheets()] worksheets = self._execute_with_retry(spreadsheet.worksheets)
return [ws.title for ws in worksheets]
def get_sheet_data(self, doc_name, sheet_name): def get_sheet_data(self, doc_name, sheet_name):
"""Pobiera wszystkie dane z danego arkusza.""" """Pobiera wszystkie dane z danego arkusza."""
sheet = self.client.open(doc_name).worksheet(sheet_name) spreadsheet = self._execute_with_retry(self.client.open, doc_name)
return sheet.get_all_values() 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): def ensure_worksheet(self, doc_name, sheet_name):
""" """
Zwraca worksheet o danej nazwie. Zwraca worksheet o danej nazwie.
Tworzy nowy, jeśli nie istnieje. Tworzy nowy, jeśli nie istnieje.
""" """
spreadsheet = self.client.open(doc_name) spreadsheet = self._execute_with_retry(self.client.open, doc_name)
try: try:
ws = spreadsheet.worksheet(sheet_name) ws = self._execute_with_retry(spreadsheet.worksheet, sheet_name)
except gspread.exceptions.WorksheetNotFound: except WorksheetNotFound:
logging.info(f" Tworzę nowy arkusz: {sheet_name}") logging.info(f" Tworzę nowy arkusz: {sheet_name}")
ws = spreadsheet.add_worksheet(title=sheet_name, rows=100, cols=10) ws = self._execute_with_retry(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"]) 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 return ws
def batch_append_unique_rows(self, doc_name, sheet_name, rows_data): 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) 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...") logging.info("🔍 Sprawdzam istniejące numery zamówień w arkuszu docelowym...")
# existing_orders = set(ws.col_values(3)) existing_orders_values = self._execute_with_retry(ws.col_values, 3)
existing_orders = {str(x).strip() for x in 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}") 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 = [] unique_rows_to_add = []
for row in rows_data: for row in rows_data:
order_number = str(row[2]).strip() order_number = str(row[2]).strip()
if order_number not in existing_orders: if order_number not in existing_orders:
unique_rows_to_add.append(row) 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) existing_orders.add(order_number)
# 3. Dodaj wszystkie unikalne wiersze w JEDNYM zapytaniu
if unique_rows_to_add: if unique_rows_to_add:
logging.info(f"📝 Dodaję {len(unique_rows_to_add)} nowych unikalnych wierszy do arkusza {sheet_name}...") 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.") logging.info("✅ Zakończono dodawanie.")
else: else:
logging.info(" Nie znaleziono żadnych nowych wierszy do dodania.") logging.info(" Nie znaleziono żadnych nowych wierszy do dodania.")
skipped_count = len(rows_data) - len(unique_rows_to_add) skipped_count = len(rows_data) - len(unique_rows_to_add)
if skipped_count > 0: if skipped_count > 0:
logging.info(f"⏭️ Pominięto {skipped_count} wierszy, które już istniały w arkuszu.") logging.info(f"⏭️ Pominięto {skipped_count} wierszy, które już istniały w arkuszu.")

View File

@@ -1,6 +1,16 @@
import re import re
import logging import logging
SUFFIX_TO_CATEGORY = {
'G': 'gloss',
'S': 'satin',
'M': 'mat',
'R': 'mat',
'MAT': 'mat',
'RAW': 'mat',
'AG': 'aged',
}
def normalize(text): def normalize(text):
if not text: if not text:
return "" return ""
@@ -11,7 +21,13 @@ def get_finish_suffix(color):
"""Extracts the finish suffix (e.g., 'G', 'S', 'M') from a color string.""" """Extracts the finish suffix (e.g., 'G', 'S', 'M') from a color string."""
if not color: if not color:
return None 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): def get_finish_type(row_data):
"""Determines the finish type (GLOSS, SATIN, MAT, MIX) based on color suffixes.""" """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 top_suffix = top_suffix or body_suffix
body_suffix = body_suffix or top_suffix body_suffix = body_suffix or top_suffix
suffix_to_category = { top_category = SUFFIX_TO_CATEGORY.get(top_suffix)
'G': 'gloss', body_category = SUFFIX_TO_CATEGORY.get(body_suffix)
'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: 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: if top_category == body_category:
return top_category.upper() 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' return 'MIX'
except (KeyError, AttributeError): except (KeyError, AttributeError):
# This will catch if row_data is not a dict or keys are missing # This will catch if row_data is not a dict or keys are missing
return None return None
return None return None
def process_row(row, mayo, counter): def process_row(row, mayo, counter):