import logging from actions import open_page from classes.application_result import ApplicationResult from language import _ from providers._provider import ApplyContext, Provider logger = logging.getLogger("flat-apply") class Degewo(Provider): @property def domain(self) -> str: return "degewo.de" async def apply_for_flat(self, url: str, ctx: ApplyContext) -> ApplicationResult: r = ctx.recorder p = ctx.profile async with open_page(url, recorder=r) as page: r.step("cookies.check") cookie_accept_btn = page.locator("#cookie-consent-submit-all") if await cookie_accept_btn.is_visible(): await cookie_accept_btn.click() r.step("cookies.accepted") r.step("page.404_check") if page.url == "https://www.degewo.de/immosuche/404": return ApplicationResult(False, message=_("ad_offline")) r.step("ad.deactivated_check") if await page.locator("span", has_text="Inserat deaktiviert").is_visible(): return ApplicationResult(False, message=_("ad_deactivated")) r.step("page.moved_check") if await page.locator("h1", has_text="Diese Seite ist umgezogen!").is_visible(): return ApplicationResult(False, message=_("ad_offline")) r.step("form.open"); await page.get_by_role("link", name="Kontakt").click() r.step("iframe.locate") form_frame = page.frame_locator("iframe[src*='wohnungshelden']") r.step("form.fill_personal") await form_frame.locator("#salutation").fill(p.salutation) await form_frame.get_by_role("option", name=p.salutation, exact=True).click() await form_frame.locator("#firstName").fill(p.firstname) await form_frame.locator("#lastName").fill(p.lastname) await form_frame.locator("#email").fill(p.email) await form_frame.locator("input[title='Telefonnummer']").fill(p.telephone) await form_frame.locator("input[title='Anzahl einziehende Personen']").fill(str(p.person_count)) await page.wait_for_timeout(1000) r.step("form.wbs_section") wbs_question = form_frame.locator("input[id*='wbs_available'][id$='Ja']") if await wbs_question.is_visible(): if not p.is_possessing_wbs: r.step("wbs_required_abort", "warn") return ApplicationResult(False, _("wbs_required")) await wbs_question.click() await form_frame.locator("input[title='WBS gültig bis']").fill(p.wbs_valid_till_dt().strftime("%d.%m.%Y")) await page.wait_for_timeout(1000) wbs_rooms_select = form_frame.locator("ng-select[id*='wbs_max_number_rooms']") await wbs_rooms_select.click() await page.wait_for_timeout(1000) await form_frame.get_by_role("option", name=str(p.wbs_rooms), exact=True).click() await page.wait_for_timeout(1000) await form_frame.locator("ng-select[id*='fuer_wen_ist_wohnungsanfrage']").click() await page.wait_for_timeout(1000) await form_frame.get_by_role("option", name="Für mich selbst").click() if not ctx.submit_forms: r.step("submit.dry_run") return ApplicationResult(success=True, message=_("application_success_dry")) r.step("submit.click"); await form_frame.locator("button[data-cy*='btn-submit']").click() await page.wait_for_timeout(3000) r.step("success.check") if await page.locator("h4", has_text="Vielen Dank für das Übermitteln Ihrer Informationen. Sie können dieses Fenster jetzt schließen.").is_visible(): return ApplicationResult(success=True) elif await self._missing_fields_warning(page): r.step("missing_fields_detected", "warn") return ApplicationResult(success=False, message=_("missing_fields")) elif await self._already_applied_warning(page): r.step("already_applied_detected", "warn") return ApplicationResult(success=False, message=_("already_applied")) r.step("success.not_found", "warn") return ApplicationResult(success=False, message=_("submit_conformation_msg_not_found")) async def _already_applied_warning(self, page) -> bool: await page.wait_for_timeout(1000) f = page.frame_locator("iframe[src*='wohnungshelden']") msg = f.locator("span.ant-alert-message", has_text="Es existiert bereits eine Anfrage mit dieser E-Mail Adresse") return await msg.first.is_visible() async def _missing_fields_warning(self, page) -> bool: await page.wait_for_timeout(1000) f = page.frame_locator("iframe[src*='wohnungshelden']") msg = f.locator("span.ant-alert-message", has_text="Es wurden nicht alle Felder korrekt befüllt. Bitte prüfen Sie ihre Eingaben") return await msg.first.is_visible()