platform login is working
This commit is contained in:
parent
44a04f451e
commit
93bd81ecea
8 changed files with 174 additions and 32 deletions
|
@ -1,7 +1,7 @@
|
|||
import asyncio
|
||||
import logging
|
||||
|
||||
from aiohttp import ClientResponse
|
||||
from aiohttp import ClientSession
|
||||
|
||||
from resa_padel import config
|
||||
from resa_padel.gestion_sports_connector import GestionSportsConnector
|
||||
|
@ -9,13 +9,17 @@ from resa_padel.gestion_sports_connector import GestionSportsConnector
|
|||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def connect(url: str) -> ClientResponse:
|
||||
booking_platform = GestionSportsConnector(url)
|
||||
return await booking_platform.connect()
|
||||
async def book(url: str, user: str, password: str, club_id: str) -> ClientSession:
|
||||
async with ClientSession() as session:
|
||||
platform = GestionSportsConnector(session, url)
|
||||
await platform.connect()
|
||||
await platform.login(user, password, club_id)
|
||||
return session
|
||||
|
||||
|
||||
def main() -> ClientResponse:
|
||||
def main() -> None:
|
||||
LOGGER.info("Starting booking padel court")
|
||||
response = asyncio.run(connect(config.GESTION_SPORTS_URL))
|
||||
asyncio.run(
|
||||
book(config.GESTION_SPORTS_URL, config.USER, config.PASSWORD, config.CLUB_ID)
|
||||
)
|
||||
LOGGER.info("Finished booking padel court")
|
||||
return response
|
||||
|
|
|
@ -14,3 +14,6 @@ def init_log_config():
|
|||
|
||||
|
||||
GESTION_SPORTS_URL = "https://toulousepadelclub.gestion-sports.com"
|
||||
USER = os.environ.get("USER")
|
||||
PASSWORD = os.environ.get("PASSWORD")
|
||||
CLUB_ID = os.environ.get("CLUB_ID")
|
||||
|
|
2
resa_padel/exceptions.py
Normal file
2
resa_padel/exceptions.py
Normal file
|
@ -0,0 +1,2 @@
|
|||
class ArgumentMissing(Exception):
|
||||
pass
|
|
@ -2,21 +2,53 @@ import logging
|
|||
|
||||
from aiohttp import ClientSession, ClientResponse
|
||||
|
||||
from resa_padel.gestion_sports_payload_builder import GestionSportsPayloadBuilder
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
HEADERS = {
|
||||
"Connection": "keep-alive",
|
||||
"Accept-Language": "en-US,en;q=0.5",
|
||||
"Accept-Encoding": "gzip, deflate, br",
|
||||
"DNT": "1",
|
||||
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
||||
"Accept": "application/json, text/javascript, */*; q=0.01",
|
||||
"X-Requested-With": "XMLHttpRequest",
|
||||
"Sec-Fetch-Dest": "empty",
|
||||
"Sec-Fetch-Mode": "cors",
|
||||
"Sec-Fetch-Site": "same-origin",
|
||||
}
|
||||
|
||||
|
||||
class GestionSportsConnector:
|
||||
|
||||
def __init__(self, url: str):
|
||||
def __init__(self, session: ClientSession, url: str):
|
||||
LOGGER.info("Initializing connection to GestionSports API")
|
||||
self.url = url
|
||||
self.session = ClientSession()
|
||||
self.session = session
|
||||
self.payload_builder = GestionSportsPayloadBuilder()
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
self.session.close()
|
||||
|
||||
async def connect(self) -> ClientResponse:
|
||||
LOGGER.info("Connecting to GestionSports API")
|
||||
async with self.session.get(self.url) as response:
|
||||
connection_url = self.url + "/connexion.php?"
|
||||
async with self.session.get(connection_url) as response:
|
||||
await response.text()
|
||||
return response
|
||||
|
||||
async def login(self, user: str, password: str, club_id: str) -> ClientResponse:
|
||||
payload = (
|
||||
self.payload_builder.login(user)
|
||||
.password(password)
|
||||
.club_id(club_id)
|
||||
.build_login_payload()
|
||||
)
|
||||
|
||||
login_url = f"{self.url}/connexion.php?"
|
||||
|
||||
async with self.session.post(
|
||||
login_url, data=payload, headers=HEADERS
|
||||
) as response:
|
||||
await response.text()
|
||||
return response
|
||||
|
|
33
resa_padel/gestion_sports_payload_builder.py
Normal file
33
resa_padel/gestion_sports_payload_builder.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
from resa_padel.exceptions import ArgumentMissing
|
||||
|
||||
|
||||
class GestionSportsPayloadBuilder:
|
||||
def __init__(self):
|
||||
self._login = None
|
||||
self._password = None
|
||||
self._club_id = None
|
||||
|
||||
def login(self, login):
|
||||
self._login = login
|
||||
return self
|
||||
|
||||
def password(self, password):
|
||||
self._password = password
|
||||
return self
|
||||
|
||||
def club_id(self, club_id):
|
||||
self._club_id = club_id
|
||||
return self
|
||||
|
||||
def build_login_payload(self):
|
||||
if self._login is None:
|
||||
raise ArgumentMissing("Login not provided")
|
||||
if self.password is None:
|
||||
raise ArgumentMissing("Password not provided")
|
||||
if self.club_id is None:
|
||||
raise ArgumentMissing("Club ID not provided")
|
||||
|
||||
return (
|
||||
f"ajax=connexionUser&id_club={self._club_id}&email={self._login}&form_ajax=1&pass={self._password}&compte"
|
||||
f"=user&playeridonesignal=0&identifiant=identifiant&externCo=true"
|
||||
).encode("utf-8")
|
Loading…
Add table
Add a link
Reference in a new issue