Add configuration, processing, and workflow modules for GoogleSheetBot

This commit is contained in:
2025-10-29 21:37:53 +01:00
parent 00739cec56
commit 3f1fd7e942
4 changed files with 159 additions and 156 deletions

10
config.py Normal file
View File

@@ -0,0 +1,10 @@
import os
from dotenv import load_dotenv
load_dotenv()
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"

160
main.py
View File

@@ -1,160 +1,8 @@
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 select_sheet(gsheet_api):
"""Lists available sheets and prompts the user to select one."""
print("📄 Pobieram listę arkuszy...")
try:
sheets = gsheet_api.list_sheets(DOC_NAME)
for i, name in enumerate(sheets):
print(f"{i+1}. {name}")
sheet_name = input("\nWybierz arkusz do przetworzenia: ")
return sheet_name
except Exception as e:
print(f"❌ Błąd podczas pobierania listy arkuszy: {e}")
return None
def get_sheet_data(gsheet_api, sheet_name):
"""Fetches all data from a given sheet."""
print(f"📋 Pobieram dane z arkusza: {sheet_name}")
try:
return gsheet_api.get_sheet_data(DOC_NAME, sheet_name)
except Exception as e:
print(f"❌ Błąd podczas pobierania danych z arkusza: {e}")
return None
def process_all_rows(rows, mayo):
"""Processes all rows from the sheet."""
rows_to_process = []
counter = 1
# Skip header row by starting from index 1
for row in rows[1:]:
processed_row = process_row(row, mayo, counter)
if processed_row:
rows_to_process.append(processed_row)
counter += 1
return rows_to_process
def save_results(gsheet_api, sheet_name, processed_rows):
"""Saves the processed rows to the spreadsheet."""
if processed_rows:
print(f"\n\n--- Podsumowanie ---")
print(f"Zebrano {len(processed_rows)} wierszy do przetworzenia.")
gsheet_api.batch_append_unique_rows(RESULT_DOC, sheet_name, processed_rows)
else:
print("\nNie zebrano żadnych danych do przetworzenia.")
from config import MAYO_URL, LOGIN, PASSWORD
from workflow import select_sheet, get_sheet_data, save_results
from processing import process_all_rows
def main():
gsheet_api = GSheetAPI()
@@ -175,4 +23,4 @@ def main():
save_results(gsheet_api, sheet_name, processed_rows)
if __name__ == "__main__":
main()
main()

111
processing.py Normal file
View File

@@ -0,0 +1,111 @@
import re
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 process_all_rows(rows, mayo):
"""Processes all rows from the sheet."""
rows_to_process = []
counter = 1
# Skip header row by starting from index 1
for row in rows[1:]:
processed_row = process_row(row, mayo, counter)
if processed_row:
rows_to_process.append(processed_row)
counter += 1
return rows_to_process

34
workflow.py Normal file
View File

@@ -0,0 +1,34 @@
from config import DOC_NAME, RESULT_DOC
def select_sheet(gsheet_api):
"""Lists available sheets and prompts the user to select one."""
print("📄 Pobieram listę arkuszy...")
try:
sheets = gsheet_api.list_sheets(DOC_NAME)
for i, name in enumerate(sheets):
print(f"{i+1}. {name}")
sheet_name = input("\nWybierz arkusz do przetworzenia: ")
return sheet_name
except Exception as e:
print(f"❌ Błąd podczas pobierania listy arkuszy: {e}")
return None
def get_sheet_data(gsheet_api, sheet_name):
"""Fetches all data from a given sheet."""
print(f"📋 Pobieram dane z arkusza: {sheet_name}")
try:
return gsheet_api.get_sheet_data(DOC_NAME, sheet_name)
except Exception as e:
print(f"❌ Błąd podczas pobierania danych z arkusza: {e}")
return None
def save_results(gsheet_api, sheet_name, processed_rows):
"""Saves the processed rows to the spreadsheet."""
if processed_rows:
print(f"\n\n--- Podsumowanie ---")
print(f"Zebrano {len(processed_rows)} wierszy do przetworzenia.")
gsheet_api.batch_append_unique_rows(RESULT_DOC, sheet_name, processed_rows)
else:
print("\nNie zebrano żadnych danych do przetworzenia.")