Refactor process_row function to streamline row processing and error handling

This commit is contained in:
2025-10-29 21:30:04 +01:00
parent 2eb1b38f25
commit fd75bc88dc

73
main.py
View File

@@ -65,43 +65,16 @@ def get_finish_type(row_data):
return None return None
def main(): def process_row(row, mayo, counter):
# Inicjalizuj API raz na początku """Processes a single row from the sheet."""
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: if len(row) < 3:
continue # Pomiń wiersze, które nie mają wystarczającej liczby kolumn return None # Skip rows with insufficient columns
link = row[1] link = row[1]
nr_zam = row[2] nr_zam = row[2]
if not link: if not link:
continue return None
print(f"\n🔗 Sprawdzam: {link}") print(f"\n🔗 Sprawdzam: {link}")
try: try:
@@ -132,11 +105,45 @@ def main():
info.get("finish_s"), info.get("finish_s"),
] ]
print(f"raw_data: {row_data}") print(f"raw_data: {row_data}")
rows_to_process.append(row_data) return row_data
counter += 1
except Exception as e: except Exception as e:
print(f"❌ Błąd podczas przetwarzania linku {link}: {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 # Po zakończeniu pętli, dodaj wszystkie zebrane wiersze za jednym razem
if rows_to_process: if rows_to_process: