This commit is contained in:
Denis Merigoux 2022-09-22 15:33:20 +02:00
parent 365037f70c
commit 4df71d8bae
No known key found for this signature in database
GPG Key ID: EE99DCFA365C3EE3
2 changed files with 172 additions and 0 deletions

View File

@ -0,0 +1,121 @@
from abc import ABC
from enum import Enum
from typing import Any, Optional
class Logement(ABC):
def residence(self) -> str:
pass
def typ(self) -> Optional[str]:
pass
def meublee(self) -> Optional[bool]:
pass
class AppartementOuMaisonType(Enum):
Location = "LOCATION"
Colocation = "COLOCATION"
class AppartementOuMaison(Logement):
def __init__(self, typ: AppartementOuMaisonType, meuble: bool):
self.residence_v = "APPARTEMENT_OU_MAISON"
self.typ_v = typ
self.meuble_v = meuble
def residence(self) -> str:
return self.residence_v
def typ(self) -> Optional[str]:
return self.typ_v.value
def meublee(self) -> Optional[bool]:
return self.meuble_v
class LogementCrousType(Enum):
Chambre = 'CHAMBRE'
Chambre_rehabilitee = 'CHAMBRE_REHABILITEE'
Studio = 'STUDIO'
class LogementCrous(Logement):
def __init__(self, typ: LogementCrousType):
self.residence_v = 'LOGEMENT_CROUS'
self.typ_v = typ
def residence(self) -> str:
return self.residence_v
def typ(self) -> Optional[str]:
return self.typ_v.value
def meublee(self) -> Optional[bool]:
return None
class LogementFoyer(Logement):
def __init__(self):
self.residence_v = 'FOYER'
def residence(self) -> str:
return self.residence_v
def typ(self) -> Optional[str]:
return None
def meublee(self) -> Optional[bool]:
return None
class LogementResidenceSocialeFJT(Logement):
def __init__(self):
self.residence_v = 'RESIDENCE_SOCIALE_FJT'
def residence(self) -> str:
return self.residence_v
def typ(self) -> Optional[str]:
return None
def meublee(self) -> Optional[bool]:
return None
class LogementMaisonRetraite(Logement):
def __init__(self):
self.residence_v = 'MAISON_RETRAITE_EHPAD'
def residence(self) -> str:
return self.residence_v
def typ(self) -> Optional[str]:
return None
def meublee(self) -> Optional[bool]:
return None
class LogementChambre(Logement):
def __init__(self, meuble: bool):
self.residence_v = 'CHAMBRE'
self.meuble_v = meuble
def residence(self) -> str:
return self.residence_v
def typ(self) -> Optional[str]:
return None
def meublee(self) -> Optional[bool]:
return self.meuble_v
class CnafSimulatorInput():
def __init__(self,
code_postal: str,
logement: Logement):
self.code_postal = code_postal
self.logement = logement

View File

@ -0,0 +1,51 @@
# This code is ported from https://github.com/jboillot/apl-fetcher
from playwright.sync_api import sync_playwright
from input import AppartementOuMaison, AppartementOuMaisonType, CnafSimulatorInput
HOME_PAGE = 'https://wwwd.caf.fr/wps/portal/caffr/aidesetservices/lesservicesenligne/estimervosdroits/lelogement'
def run_simulator(input: CnafSimulatorInput):
with sync_playwright() as p:
browser = p.firefox.launch(headless=False, slow_mo=10)
page = browser.new_page()
# Go to the CNAF simulator
page.goto(HOME_PAGE)
# Click on 'Commencer'
commencer_button = page.wait_for_selector('button[id="btn-commencer"]')
if commencer_button is None:
raise RuntimeError
commencer_button.click()
# Code Postal
code_postal_input = page.wait_for_selector('input[id="codePostal"]')
if code_postal_input is None:
raise RuntimeError
code_postal_input.fill(input.code_postal)
# Select first match
page.wait_for_selector('li[class="uib-typeahead-match"]')
page.keyboard.press('Enter')
page.wait_for_timeout(1000)
# Select residence type
type_residence_button = page.wait_for_selector(
"label[for=\"nature_{}\"]".format(input.logement.residence()))
if type_residence_button is None:
raise RuntimeError
type_residence_button.click()
page.wait_for_timeout(1000)
browser.close()
run_simulator(
CnafSimulatorInput(
code_postal="69001",
logement=AppartementOuMaison(AppartementOuMaisonType.Location)
))