Skip to content

Commit

Permalink
Auto accept legal terms updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre0512 committed Jan 16, 2023
1 parent c2391c6 commit 3f8d3aa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
18 changes: 15 additions & 3 deletions lidlplus/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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")
Expand Down Expand Up @@ -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


Expand Down
19 changes: 15 additions & 4 deletions lidlplus/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import requests

from lidlplus.exceptions import WebBrowserException, LoginError
from lidlplus.exceptions import WebBrowserException, LoginError, LegalTermsException

try:
from getuseragent import UserAgent
Expand Down Expand Up @@ -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 ""
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions lidlplus/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ class WebBrowserException(Exception):

class LoginError(Exception):
"""Login failed"""


class LegalTermsException(Exception):
"""Not accepted legal terms"""
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3f8d3aa

Please sign in to comment.