diff --git a/bot_z/operator.py b/bot_z/operator.py index 8885b65..2e4efcd 100644 --- a/bot_z/operator.py +++ b/bot_z/operator.py @@ -149,7 +149,6 @@ class Operator(wd.Firefox): self.logger.setLevel(logging.DEBUG) self.logger.debug("Debug level") self._logged_in = False - self._checked_in = False self.username = None # Clean preceding session self.delete_all_cookies() @@ -269,12 +268,42 @@ class Operator(wd.Firefox): _cookies = all(c in cookies for c in ("spcookie", "JSESSIONID", "ipclientid")) return _cookies + def _switch_to_container(self) -> None: + try: + iframe = self.find_element_by_xpath( + '//iframe[contains(@id, "gsmd_container.jsp")]' + ) + self.switch_to.frame(iframe) + except NoSuchElementException: + pass + + def get_movements(self) -> T.List[T.Tuple[T.Text]]: + self._switch_to_container() + try: + result = [] # type: T.List[T.Tuple[T.Text]] + movements_table = self.find_elements_by_xpath( + '//div[contains(@class, "ushp_wterminale_container")]//div[@ps-name="Grid2"]//table/tbody/tr' + ) + for row in movements_table: + data = row.text.strip().split("\n") # type: T.List[T.Text] + result.append(tuple(i.strip() for i in data if i.strip())) # noqa + except NoSuchElementException: + return [] + return result + @property def checked_in(self) -> bool: """ Check if the user is checked in already. """ - return self._checked_in + if not self.logged_in: + return False + dates = self.get_movements() + if not dates: + return False + if dates[-1][0] == "Entrata": + return True + return False @safely(RETRIES) def check_in(self, force: bool = False) -> None: @@ -284,19 +313,13 @@ class Operator(wd.Firefox): if not force and not self.logged_in: self.logger.warning("Not logged in!") return - if self._checked_in: + if self.checked_in: self.logger.warn("Already checked in!") if not force: return - iframe = self.find_element_by_xpath( - '//iframe[contains(@id, "gsmd_container.jsp")]' - ) - self.switch_to.frame(iframe) + self._switch_to_container() enter_butt = self.find_element_by_xpath('//input[@value="Entrata"]') enter_butt.click() - # Click the check in button and change - # self._checked_in state in case of success - self._checked_in = True @safely(RETRIES) def check_out(self, force: bool = False) -> None: @@ -306,19 +329,13 @@ class Operator(wd.Firefox): if not force and not self.logged_in: self.logger.warning("Not logged in!") return - if not self._checked_in: + if not self.checked_in: self.logger.warn("Not yet checked in!") if not force: return - iframe = self.find_element_by_xpath( - '//iframe[contains(@id, "gsmd_container.jsp")]' - ) - self.switch_to.frame(iframe) + self._switch_to_container() exit_butt = self.find_element_by_xpath('//input[@value="Uscita"]') exit_butt.click() - # Click the check in button and change - # self._checked_in state in case of success - self._checked_in = False def __del__(self) -> None: self.quit()