diff --git a/lidlplus/__main__.py b/lidlplus/__main__.py index 5c7e5df..36c2d9f 100755 --- a/lidlplus/__main__.py +++ b/lidlplus/__main__.py @@ -13,7 +13,7 @@ sys.path.insert(0, str(Path(__file__).parent.parent)) # pylint: disable=wrong-import-position from lidlplus import LidlPlusApi -from lidlplus.exceptions import WebBrowserException, LoginError +from lidlplus.exceptions import WebBrowserException, LoginError, LegalTermsException def get_arguments(): @@ -26,6 +26,8 @@ def get_arguments(): parser.add_argument("--2fa", choices=["phone", "email"], default="phone", help="set 2fa method") parser.add_argument("-r", "--refresh-token", help="refresh token to authenticate") parser.add_argument("--skip-verify", help="skip ssl verification", action="store_true") + parser.add_argument("--not-accept-legal-terms", help="Deny legal terms updates", action="store_true") + parser.add_argument("-d", "--debug", help="debug mode", action="store_true") subparser = parser.add_subparsers(title="commands", metavar="command", required=True) auth = subparser.add_parser("auth", help="authenticate and get refresh_token") auth.add_argument("auth", help="authenticate and get refresh_token", action="store_true") @@ -63,18 +65,28 @@ def lidl_plus_login(args): country = args.get("country") or input("Enter your country (de, at, ...): ") if args.get("refresh_token"): return LidlPlusApi(language, country, args.get("refresh_token")) - username = args.get("username") or input("Enter your lidl plus username (phone number): ") + username = args.get("user") or input("Enter your lidl plus username (phone number): ") password = args.get("password") or getpass("Enter your lidl plus password: ") lidl_plus = LidlPlusApi(language, country) try: text = f"Enter the verify code you received via {args['2fa']}: " - lidl_plus.login(username, password, verify_token_func=lambda: input(text), verify_mode=args["2fa"]) + lidl_plus.login( + username, + password, + verify_token_func=lambda: input(text), + verify_mode=args["2fa"], + headless=not args.get("debug"), + accept_legal_terms=not args.get("not_accept_legal_terms"), + ) except WebBrowserException: print("Can't connect to web browser. Please install Chrome, Chromium or Firefox") sys.exit(101) except LoginError as error: print(f"Login failed - {error}") sys.exit(102) + except LegalTermsException as error: + print(f"Legal terms not accepted - {error}") + sys.exit(103) return lidl_plus diff --git a/lidlplus/api.py b/lidlplus/api.py index 219498f..0da2901 100644 --- a/lidlplus/api.py +++ b/lidlplus/api.py @@ -9,7 +9,7 @@ import requests -from lidlplus.exceptions import WebBrowserException, LoginError +from lidlplus.exceptions import WebBrowserException, LoginError, LegalTermsException try: from getuseragent import UserAgent @@ -143,11 +143,22 @@ def _register_link(self): params = "&".join([f"{key}={value}" for key, value in args.items()]) return f"{self._register_oauth_client()}&{params}" - def _parse_code(self, all_request): - for request in reversed(all_request): + @staticmethod + def _accept_legal_terms(browser, wait, accept=True): + wait.until(expected_conditions.visibility_of_element_located((By.ID, "checkbox_Accepted"))).click() + if not accept: + title = browser.find_element(By.TAG_NAME, "h2").text + raise LegalTermsException(title) + browser.find_element(By.TAG_NAME, "button").click() + + def _parse_code(self, browser, wait, accept_legal_terms=True): + for request in reversed(browser.requests): if f"{self._AUTH_API}/connect" not in request.url: continue location = request.response.headers.get("Location", "") + if "legalTerms" in location: + self._accept_legal_terms(browser, wait, accept=accept_legal_terms) + return self._parse_code(browser, wait, False) if code := re.findall("code=([0-9A-F]+)", location): return code[0] return "" @@ -198,7 +209,7 @@ def login(self, phone, password, **kwargs): self._check_login_error(browser) self._check_2fa_auth(browser, wait, kwargs.get("verify_mode", "phone"), kwargs.get("verify_token_func")) browser.wait_for_request(f"{self._AUTH_API}/connect.*") - code = self._parse_code(browser.requests) + code = self._parse_code(browser, wait, accept_legal_terms=kwargs.get("accept_legal_terms", True)) self._authorization_code(code) def _default_headers(self): diff --git a/lidlplus/exceptions.py b/lidlplus/exceptions.py index 5696b92..7b44e50 100644 --- a/lidlplus/exceptions.py +++ b/lidlplus/exceptions.py @@ -9,3 +9,7 @@ class WebBrowserException(Exception): class LoginError(Exception): """Login failed""" + + +class LegalTermsException(Exception): + """Not accepted legal terms""" diff --git a/setup.py b/setup.py index efedba2..bef1db4 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="lidl-plus", - version="0.2.3", + version="0.2.4", author="Andre Basche", description="Fetch receipts and more from Lidl Plus", long_description=long_description,