diff --git a/french_law/python/cnaf_cross_tester/input.py b/french_law/python/cnaf_cross_tester/input.py new file mode 100644 index 00000000..d5c9fc78 --- /dev/null +++ b/french_law/python/cnaf_cross_tester/input.py @@ -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 diff --git a/french_law/python/cnaf_cross_tester/pupeteer.py b/french_law/python/cnaf_cross_tester/pupeteer.py new file mode 100644 index 00000000..f68399c0 --- /dev/null +++ b/french_law/python/cnaf_cross_tester/pupeteer.py @@ -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) + ))