feat: wrap the db object around baza, which will provide helper methods

also adds the list of tracked sportsmen
This commit is contained in:
2025-05-27 12:28:51 +02:00
parent b6ae33861e
commit 65ec7ff73d
5 changed files with 153 additions and 93 deletions

View File

@@ -10,3 +10,22 @@ api_key = ""
[scraper]
user-agent = "" # Leave empty for default (Firefox ESR).
[sportsmen]
tracked_ids = [
"MVC8zHZD", # Robert Lewandowski
"WGOY4FSt", # Cristiano Ronaldo
"vgOOdZbd", # Lionel Messi
"Wn6E2SED", # Kylian Mbappe
"AiH2zDve", # Zlatan Ibrahimovic
"dUShzrBp", # Luis Suarez
"UmV9iQmE", # Erling Haaland
"tpV0VX0S", # Karim Benzema
"vw8ZV7HC", # Sergio Aguero
"Qgx2trzH", # Edinson Cavani
"2oMimkAU", # Radamel Falcao
"WfXv1DCa", # Wayne Rooney
"0vgcq6un", # Robin van Persie
"v5HSlEAa", # Harry Kane
"4S9fNUYh" # Ciro Immobile
]

View File

@@ -1,5 +1,7 @@
import requests
from lewy_db import baza as ldb
import json
import lewy_globals
import requests
class scraper:
@@ -7,7 +9,10 @@ class scraper:
'x-fsign': 'SW9D1eZo'
}
db = None
def __init__(self):
db = lewy_globals.getDb()
pass
def pobierzDaneNajlepszegoSportowcaNaSwiecie(self) -> dict:
@@ -19,3 +24,11 @@ class scraper:
raise ValueError("Zewnętrzne ID sportowca powinno być długości 8!")
response = requests.get(f'https://3.flashscore.ninja/3/x/feed/plm_{zewnetrzne_id_sportowca}_{nr_strony}', headers=self.headers)
return json.loads(response.text)
def czy_mecz_istnieje(self, zewnetrzne_id_meczu: str):
mecz = db.simple_select_all(ldb.mecze, zewnetrzne_id_meczu=zewnetrzne_id_meczu).first
if mecz is not None:
return mecz
else:
return False

View File

@@ -6,7 +6,7 @@ from flask_sqlalchemy import SQLAlchemy
from functools import wraps
from lewy_globals import getDb, colors as c
import flask, json, time
import lewy_db
import lewy_db as ldb
import lewy_globals
def require_authentication(func):

View File

@@ -1,13 +1,22 @@
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase, relationship
from sqlalchemy import ForeignKey, select
from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase, Session, relationship
from typing import List
import toml
global db
def initDB(app, config):
class baza():
# global sportowcy, trofea, sportowcy_w_meczach, statystyki_sportowcow, kluby, mecze
db = None
def __init__(self, app, config):
self.db = self.initDB(app, config)
def initDB(self, app, config):
global sportowcy, trofea, sportowcy_w_meczach, statystyki_sportowcow, kluby, mecze
tnp = config['general']['db_prefix'] + "_lewangoalski_"
@@ -104,3 +113,22 @@ def initDB(app, config):
flaga: Mapped[ int] = mapped_column()
return db
def create_all(self):
self.db.create_all()
def simple_select_all(self, type, **kwargs):
"""
Użycie:
simple_select_all(ldb.sportowcy, zewnetrzne_id_zawodnika="").fetchall()
https://stackoverflow.com/a/75316945
Did they make it harder to query dynamically on purpose? ~Frank 19.11.2023
"""
with self.db.engine.begin() as conn:
return conn.execute(
select(type).
filter_by(**kwargs)
).scalars()

View File

@@ -84,7 +84,7 @@ def getConfig(configfile):
def setupDb(app, config):
global db
db = lewy_db.initDB(app, config)
db = lewy_db.baza(app, config)
return db
def getDb():