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 = 'ISO-8859-2' # Poprawione kodowanie na podstawie tagu meta w HTML 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 = {} # Szukamy linków (a) wewnątrz pogrubienia (b), które zawierają "KOLOR -" for a_tag in soup.select('b > a'): title = a_tag.get_text(strip=True) if title.startswith("KOLOR -"): label = title.replace("KOLOR - ", "").strip() parent_td = a_tag.find_parent('td') if parent_td: value_td = parent_td.find_next_sibling('td') if value_td: text = value_td.get_text(" ", strip=True) value = None # Wartość jest zazwyczaj pomiędzy myślnikiem a ukośnikiem match = re.search(r'-\s*([^/]+)', text) # print(f"label: {label}, match: {match}, text: {text}") if match: value = match.group(1).strip() color_sections[label] = value return { "order_number": order_number, "model": model, "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_kc": color_sections.get("Wykończenie [K/C]"), "finish_s": color_sections.get("Wykończenie [S]"), }