Merge branch 'ScraperTest'
This commit is contained in:
@@ -122,6 +122,8 @@ class baza():
|
||||
zoltych_kartek_sum: Mapped[ int] = mapped_column()
|
||||
czerwonych_kartek_sum: Mapped[ int] = mapped_column()
|
||||
wygranych_sum: Mapped[ int] = mapped_column()
|
||||
przegranych_sum: Mapped[ int] = mapped_column()
|
||||
remisow_sum: Mapped[ int] = mapped_column()
|
||||
wynik_sum: Mapped[ int] = mapped_column()
|
||||
meczow_do_wynikow_sum: Mapped[ int] = mapped_column()
|
||||
|
||||
@@ -158,6 +160,23 @@ class baza():
|
||||
def __repr__(self):
|
||||
return f"<Mecz #{self.id_meczu} ({self.zewnetrzne_id_meczu}, {self.gospodarze.skrocona_nazwa} vs. {self.goscie.skrocona_nazwa})>"
|
||||
|
||||
def jsonify(self):
|
||||
return {
|
||||
"id_meczu": self.id_meczu,
|
||||
"zewnetrzne_id_meczu": self.zewnetrzne_id_meczu,
|
||||
"data": self.data.strftime("%Y-%m-%d"),
|
||||
"gospodarze_id": self.gospodarze_id,
|
||||
"gospodarze": self.gospodarze.skrocona_nazwa,
|
||||
"goscie_id": self.goscie_id,
|
||||
"goscie": self.goscie.skrocona_nazwa,
|
||||
"gosp_wynik": self.gosp_wynik,
|
||||
"gosc_wynik": self.gosc_wynik,
|
||||
"sezon": self.sezon,
|
||||
"nazwa_turnieju": self.nazwa_turnieju,
|
||||
"skrocona_nazwa_turnieju": self.skrocona_nazwa_turnieju,
|
||||
"flaga": self.flaga
|
||||
}
|
||||
|
||||
self.entities = {
|
||||
'sportowcy': sportowcy,
|
||||
'trofea': trofea,
|
||||
@@ -229,7 +248,7 @@ class baza():
|
||||
|
||||
query_params, special_args = self.extract_special_args(kwargs)
|
||||
|
||||
print(f"[{round(time.time())}] SELECT")
|
||||
print(f"[{round(time.time())}] SELECT {entity_type}")
|
||||
|
||||
results = (
|
||||
self.session.
|
||||
@@ -318,7 +337,7 @@ class baza():
|
||||
f"Aby naprawić dodawanie z autoinkrementującym kluczem zobacz {c.WARNING}https://stackoverflow.com/a/8745101{c.ENDC}\n"
|
||||
f"Zostałeś ostrzeżony!")
|
||||
|
||||
print(f"[{round(time.time())}] INSERT")
|
||||
print(f"[{round(time.time())}] INSERT {entity_type}")
|
||||
|
||||
obj = self.entities[entity_type](**kwargs)
|
||||
#with Session(self.db.engine) as session:
|
||||
@@ -342,6 +361,131 @@ class baza():
|
||||
return 0
|
||||
#return 1
|
||||
|
||||
@exit_gracefully
|
||||
def increment_fields(self, entity_type, record_id, **increments):
|
||||
"""
|
||||
Dodaje wartości do istniejących pól (np. goli_sum += 2).
|
||||
|
||||
Użycie:
|
||||
increment_fields(ldb.statystyki_sportowcow, 123, goli_sum=2, asyst_sum=1)
|
||||
"""
|
||||
|
||||
if not isinstance(entity_type, str):
|
||||
entity_type = entity_type.__name__
|
||||
|
||||
if entity_type not in self.entities:
|
||||
print(f"Nieznany typ encji: {entity_type}")
|
||||
return -1
|
||||
|
||||
model_class = self.entities[entity_type]
|
||||
|
||||
print(f"[{round(time.time())}] INCREMENT {entity_type}")
|
||||
|
||||
record = self.session.get(model_class, record_id)
|
||||
if not record:
|
||||
print(f"Rekord z ID {record_id} nie istnieje w tabeli {entity_type}")
|
||||
return -1
|
||||
|
||||
for key, increment_value in increments.items():
|
||||
if hasattr(record, key):
|
||||
current_value = getattr(record, key, 0)
|
||||
setattr(record, key, current_value + increment_value)
|
||||
else:
|
||||
print(f"⚠️ Pole '{key}' nie istnieje w modelu '{entity_type}' - pomijam.")
|
||||
|
||||
self.session.commit()
|
||||
return 0
|
||||
|
||||
@exit_gracefully
|
||||
def get_id_meczu_by_zewnetrzne_id(self, external_id: str) -> int | None:
|
||||
"""
|
||||
Zwraca id_meczu na podstawie zewnetrzne_id_meczu.
|
||||
|
||||
:param external_id: zewnętrzne ID meczu
|
||||
:return: id_meczu lub None jeśli nie znaleziono
|
||||
"""
|
||||
stmt = select(mecze.id_meczu).where(mecze.zewnetrzne_id_meczu == external_id)
|
||||
result = self.session.execute(stmt).scalar_one_or_none()
|
||||
return result
|
||||
|
||||
@exit_gracefully
|
||||
def get_id_zawodnika_by_zewnetrzne_id(self, external_id: str) -> int | None:
|
||||
"""
|
||||
Zwraca id_zawodnika na podstawie zewnetrzne_id_zawodnika.
|
||||
|
||||
:param external_id: zewnętrzne ID meczu
|
||||
:return: id_zawodnika lub None jeśli nie znaleziono
|
||||
"""
|
||||
stmt = select(sportowcy.id_zawodnika).where(sportowcy.zewnetrzne_id_zawodnika == external_id)
|
||||
result = self.session.execute(stmt).scalar_one_or_none()
|
||||
return result
|
||||
|
||||
@exit_gracefully
|
||||
def simple_update_one(self, entity_type, record_id, **kwargs):
|
||||
"""
|
||||
Użycie:
|
||||
simple_update_one(ldb.kluby, "polska", pelna_nazwa="Nowa Nazwa", skrocona_nazwa="NN")
|
||||
|
||||
Aktualizuje pojedynczy rekord w bazie danych na podstawie ID.
|
||||
"""
|
||||
if not isinstance(entity_type, str):
|
||||
entity_type = entity_type.__name__
|
||||
|
||||
if entity_type not in self.entities:
|
||||
print(f"Nieznany typ encji: {entity_type}")
|
||||
return -1
|
||||
|
||||
model_class = self.entities[entity_type]
|
||||
|
||||
print(f"[{round(time.time())}] UPDATE {entity_type}")
|
||||
|
||||
record = self.session.get(model_class, record_id)
|
||||
if not record:
|
||||
print(f"Rekord z ID {record_id} nie istnieje w tabeli {entity_type}")
|
||||
return -1
|
||||
|
||||
for key, value in kwargs.items():
|
||||
if hasattr(record, key):
|
||||
setattr(record, key, value)
|
||||
else:
|
||||
print(f"⚠️ Pole '{key}' nie istnieje w modelu '{entity_type}' - pomijam.")
|
||||
|
||||
self.session.commit()
|
||||
return 0
|
||||
|
||||
@exit_gracefully
|
||||
def dodaj_sportowca_w_meczu(self, entity_type, record_id, **kwargs):
|
||||
"""
|
||||
Użycie:
|
||||
dodaj_sportowca_w_meczu(ldb.sportowcy_w_meczu, ...)
|
||||
|
||||
Dodaje pojedynczy rekord w bazie danych.
|
||||
"""
|
||||
if not isinstance(entity_type, str):
|
||||
entity_type = entity_type.__name__
|
||||
|
||||
if entity_type not in self.entities:
|
||||
print(f"Nieznany typ encji: {entity_type}")
|
||||
return -1
|
||||
|
||||
model_class = self.entities[entity_type]
|
||||
|
||||
print(f"[{round(time.time())}] INSERT {entity_type}")
|
||||
|
||||
record = self.session.get(model_class, record_id)
|
||||
if not record:
|
||||
print(f"Rekord z ID {record_id} nie istnieje w tabeli {entity_type}")
|
||||
return -1
|
||||
|
||||
for key, value in kwargs.items():
|
||||
if hasattr(record, key):
|
||||
setattr(record, key, value)
|
||||
else:
|
||||
print(f"⚠️ Pole '{key}' nie istnieje w modelu '{entity_type}' - pomijam.")
|
||||
|
||||
self.session.commit()
|
||||
return 0
|
||||
|
||||
@exit_gracefully
|
||||
def sample_data_init(self, override_safety_check=False):
|
||||
"""
|
||||
@@ -375,7 +519,7 @@ class baza():
|
||||
goscie_id="undefined",
|
||||
gosp_wynik=0,
|
||||
gosc_wynik=0,
|
||||
sezon="1970/1970",
|
||||
sezon="1970-1970",
|
||||
nazwa_turnieju="Nieznany turniej",
|
||||
skrocona_nazwa_turnieju="N/A",
|
||||
flaga=0)
|
||||
@@ -391,6 +535,8 @@ class baza():
|
||||
zoltych_kartek_sum=0,
|
||||
czerwonych_kartek_sum=0,
|
||||
wygranych_sum=0,
|
||||
przegranych_sum=0,
|
||||
remisow_sum=0,
|
||||
wynik_sum=0,
|
||||
meczow_do_wynikow_sum=0)
|
||||
|
||||
@@ -409,7 +555,7 @@ class baza():
|
||||
wycena=64_940_000)
|
||||
trofeum = trofea(
|
||||
nazwa="Nieznane trofeum",
|
||||
sezon="0000/0000",
|
||||
sezon="0000-0000",
|
||||
rok=1970)
|
||||
|
||||
session.add(sportowiec)
|
||||
@@ -421,6 +567,40 @@ class baza():
|
||||
trofeum.zawodnik = sportowiec
|
||||
sportowiec.ostatnie_trofeum = trofeum
|
||||
|
||||
# MIEJSCE NA DANE KOLEJNYCH SPORTOWCÓW
|
||||
# TRZEBA ZROBIĆ TO RĘCZNIE, ZGODNIE ZE SCHEMATEM:
|
||||
# self.simple_insert_one(statystyki_sportowcow,
|
||||
# ostatni_mecz=1,
|
||||
# ilosc_wystapien=0,
|
||||
# minut_gry=0,
|
||||
# gier_sum=0,
|
||||
# goli_sum=0,
|
||||
# asyst_sum=0,
|
||||
# interwencji_sum=0,
|
||||
# nieobronionych_interwencji_sum=0,
|
||||
# zoltych_kartek_sum=0,
|
||||
# czerwonych_kartek_sum=0,
|
||||
# wygranych_sum=0,
|
||||
# przegranych_sum=0,
|
||||
# remisow_sum=0,
|
||||
# wynik_sum=0,
|
||||
# meczow_do_wynikow_sum=0)
|
||||
|
||||
# self.simple_insert_one(sportowcy,
|
||||
# zewnetrzne_id_zawodnika="...",
|
||||
# imie="...",
|
||||
# nazwisko="...",
|
||||
# data_urodzenia="...",
|
||||
# czy_aktywny=True, # NIE WSZYSCY SĄ AKTYWNI!
|
||||
# klub_id="undefined",
|
||||
# narodowosc="...",
|
||||
# ilosc_trofeow=0,
|
||||
# pierwszy_mecz_id=1,
|
||||
# ostatni_gol_dla_id="undefined",
|
||||
# statystyki_id=2, # itd...
|
||||
# wycena=...) # w złotówkach
|
||||
|
||||
session.commit()
|
||||
return 0
|
||||
return 1
|
||||
|
||||
return 1
|
||||
|
||||
Reference in New Issue
Block a user