Files
GoogleSheetBot/mayo.py
2025-10-15 11:36:01 +02:00

112 lines
4.0 KiB
Python

import requests
from bs4 import BeautifulSoup
import re
class MayoSession:
def __init__(self, base_url, login, password, db="1"):
"""
base_url: np. 'http://192.168.0.152/mayo2'
login, password: dane logowania
db: numer bazy (np. "1" = Mayones 2)
"""
self.session = requests.Session()
self.base_url = base_url
self.login_url = f"{self.base_url}/login.php"
self.credentials = {
"login": login,
"pass": password,
"baza": db
}
def login(self):
"""Loguje się do systemu lokalnego."""
r = self.session.post(self.login_url, data=self.credentials)
if "Zaloguj się" in r.text or "login" in r.url:
raise Exception("Nie udało się zalogować do Mayo.")
print("✅ Zalogowano poprawnie do systemu Mayo.")
# def get_order_info(self, url):
# """
# Pobiera dane z podanej strony zamówienia:
# - numer zamówienia
# - model gitary
# """
# r = self.session.get(url)
# r.encoding = "utf-8"
# soup = BeautifulSoup(r.text, "html.parser")
# # nr zamówienia
# order_span = soup.find("span", class_="czarnobiale")
# order_number = order_span.get_text(strip=True) if order_span else None
# # model gitary
# input_tag = soup.find("input", {"name": "s_nr_kat"})
# model = input_tag.get("value").strip() if input_tag else None
# return {"order_number": order_number, "model": model}
def get_order_info(self, url):
"""
Pobiera dane z podanej strony zamówienia:
- numer zamówienia
- model
- kolory (Top, Korpus, Szyjka, Główka)
- wykończenie
"""
r = self.session.get(url)
r.encoding = "utf-8"
soup = BeautifulSoup(r.text, "html.parser")
# --- nr zamówienia ---
order_span = soup.find("span", class_="czarnobiale")
order_number = order_span.get_text(strip=True) if order_span else None
# --- model gitary ---
input_tag = soup.find("input", {"name": "s_nr_kat"})
model = input_tag.get("value").strip() if input_tag else None
# --- kolory i wykończenie ---
color_sections = {}
# Znajdź wszystkie <p> zawierające <a> z tekstem "KOLOR"
for p in soup.find_all("p"):
a_tag = p.find("a")
if not a_tag:
continue
title = a_tag.get_text(strip=True)
if title.startswith("KOLOR"):
# np. "KOLOR - Top"
label = title.replace("KOLOR - ", "").strip()
# znajdź <td> z wartością koloru (niedaleko tego <p>)
td = p.find_parent("td")
if td:
# przejdź do następnego <td>, tam jest <span> z kolorem
next_td = td.find_next_sibling("td")
if next_td:
span = next_td.find("span")
if span:
text = span.get_text(" ", strip=True)
# usuń ewentualne znaki nadmiarowe
text = re.sub(r"\s+", " ", text)
color_match = re.search(r"^\s*-\s*([A-Z0-9-]+)", text)
if color_match:
text = color_match.group(1)
color_sections[label] = text
# --- normalizacja nazewnictwa ---
color_top = color_sections.get("Top")
color_body = color_sections.get("Korpus")
color_neck = color_sections.get("Szyjka")
color_head = color_sections.get("Główka")
finish = color_sections.get("Wykończenie [K/C]")
return {
"order_number": order_number,
"model": model,
"color_top": color_top,
"color_body": color_body,
"color_neck": color_neck,
"color_head": color_head,
"finish": finish,
}