feat: wrap the db object around baza, which will provide helper methods
also adds the list of tracked sportsmen
This commit is contained in:
@@ -10,3 +10,22 @@ api_key = ""
|
|||||||
|
|
||||||
[scraper]
|
[scraper]
|
||||||
user-agent = "" # Leave empty for default (Firefox ESR).
|
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
|
||||||
|
]
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
import requests
|
from lewy_db import baza as ldb
|
||||||
import json
|
import json
|
||||||
|
import lewy_globals
|
||||||
|
import requests
|
||||||
|
|
||||||
class scraper:
|
class scraper:
|
||||||
|
|
||||||
@@ -7,7 +9,10 @@ class scraper:
|
|||||||
'x-fsign': 'SW9D1eZo'
|
'x-fsign': 'SW9D1eZo'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db = None
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
db = lewy_globals.getDb()
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def pobierzDaneNajlepszegoSportowcaNaSwiecie(self) -> dict:
|
def pobierzDaneNajlepszegoSportowcaNaSwiecie(self) -> dict:
|
||||||
@@ -19,3 +24,11 @@ class scraper:
|
|||||||
raise ValueError("Zewnętrzne ID sportowca powinno być długości 8!")
|
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)
|
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)
|
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
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from flask_sqlalchemy import SQLAlchemy
|
|||||||
from functools import wraps
|
from functools import wraps
|
||||||
from lewy_globals import getDb, colors as c
|
from lewy_globals import getDb, colors as c
|
||||||
import flask, json, time
|
import flask, json, time
|
||||||
import lewy_db
|
import lewy_db as ldb
|
||||||
import lewy_globals
|
import lewy_globals
|
||||||
|
|
||||||
def require_authentication(func):
|
def require_authentication(func):
|
||||||
|
|||||||
@@ -1,13 +1,22 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from sqlalchemy import ForeignKey
|
from sqlalchemy import ForeignKey, select
|
||||||
from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase, relationship
|
from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase, Session, relationship
|
||||||
from typing import List
|
from typing import List
|
||||||
import toml
|
import toml
|
||||||
|
|
||||||
global db
|
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
|
global sportowcy, trofea, sportowcy_w_meczach, statystyki_sportowcow, kluby, mecze
|
||||||
tnp = config['general']['db_prefix'] + "_lewangoalski_"
|
tnp = config['general']['db_prefix'] + "_lewangoalski_"
|
||||||
|
|
||||||
@@ -104,3 +113,22 @@ def initDB(app, config):
|
|||||||
flaga: Mapped[ int] = mapped_column()
|
flaga: Mapped[ int] = mapped_column()
|
||||||
|
|
||||||
return db
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ def getConfig(configfile):
|
|||||||
|
|
||||||
def setupDb(app, config):
|
def setupDb(app, config):
|
||||||
global db
|
global db
|
||||||
db = lewy_db.initDB(app, config)
|
db = lewy_db.baza(app, config)
|
||||||
return db
|
return db
|
||||||
|
|
||||||
def getDb():
|
def getDb():
|
||||||
|
|||||||
Reference in New Issue
Block a user