feat: add a method to dynamically fetch available collection type ids

This commit is contained in:
Xavier Morel
2025-03-21 17:17:21 +01:00
parent b37cc89cf2
commit 1c6c04dd33
2 changed files with 24 additions and 0 deletions

View File

@@ -12,6 +12,7 @@ from bs4 import BeautifulSoup as bs # noqa: N813
from .const import ( from .const import (
ECOCITO_COLLECTION_ENDPOINT, ECOCITO_COLLECTION_ENDPOINT,
ECOCITO_COLLECTION_TYPE_ENDPOINT,
ECOCITO_DEFAULT_COLLECTION_TYPE, ECOCITO_DEFAULT_COLLECTION_TYPE,
ECOCITO_GARBAGE_COLLECTION_TYPE, ECOCITO_GARBAGE_COLLECTION_TYPE,
ECOCITO_LOGIN_ENDPOINT, ECOCITO_LOGIN_ENDPOINT,
@@ -79,6 +80,28 @@ class EcocitoClient:
except aiohttp.ClientError as e: except aiohttp.ClientError as e:
raise EcocitoError(f"Authentication error: {e}") from e raise EcocitoError(f"Authentication error: {e}") from e
async def get_collection_types(self) -> dict:
"""Return the mapping of collection type ID with their label."""
async with aiohttp.ClientSession(cookie_jar=self._cookies) as session:
try:
async with session.get(
ECOCITO_COLLECTION_TYPE_ENDPOINT.format(self._domain),
raise_for_status=True,
) as response:
content = await response.text()
html = bs(content, "html.parser")
try:
select = html.find("select", {"id": "Filtres_IdMatiere"})
return {
item.attrs["value"]: item.text
for item in select.find_all("option")
if "value" in item.attrs
}
except Exception as e: # noqa: BLE001
await self._handle_error(content, e)
except aiohttp.ClientError as e:
raise EcocitoError(f"Unable to get collection types: {e}") from e
async def get_collection_events( async def get_collection_events(
self, event_type: str, year: int self, event_type: str, year: int
) -> list[CollectionEvent]: ) -> list[CollectionEvent]:

View File

@@ -32,6 +32,7 @@ ECOCITO_RECYCLING_COLLECTION_TYPE = 16
# Ecocito - Collection endpoint # Ecocito - Collection endpoint
ECOCITO_COLLECTION_ENDPOINT = f"https://{ECOCITO_DOMAIN}/Usager/Collecte/GetCollecte" ECOCITO_COLLECTION_ENDPOINT = f"https://{ECOCITO_DOMAIN}/Usager/Collecte/GetCollecte"
ECOCITO_COLLECTION_TYPE_ENDPOINT = f"https://{ECOCITO_DOMAIN}/Usager/Collecte"
# Ecocito - Waste deposit visits # Ecocito - Waste deposit visits
ECOCITO_WASTE_DEPOSIT_ENDPOINT = f"https://{ECOCITO_DOMAIN}/Usager/Apport/GetApport" ECOCITO_WASTE_DEPOSIT_ENDPOINT = f"https://{ECOCITO_DOMAIN}/Usager/Apport/GetApport"