Skip to content

Commit

Permalink
ADD: parameters for cookies and credential file
Browse files Browse the repository at this point in the history
  • Loading branch information
peteh authored and marzzzello committed Apr 1, 2024
1 parent be5ddf2 commit 13d13c7
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions pytr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class TradeRepublicApi:
_subscription_id_counter = 1
_previous_responses = {}
subscriptions = {}

_credentials_file = CREDENTIALS_FILE
_cookies_file = COOKIES_FILE

@property
def session_token(self):
Expand All @@ -79,21 +82,27 @@ def session_token(self, val):
self._session_token_expires_at = time.time() + 290
self._session_token = val

def __init__(self, phone_no=None, pin=None, keyfile=None, locale='de', save_cookies=False):
def __init__(self, phone_no=None, pin=None, keyfile=None, locale='de', save_cookies=False, credentials_file = None, cookies_file = None):
self.log = get_logger(__name__)
self._locale = locale
self._save_cookies = save_cookies

self._credentials_file = pathlib.Path(credentials_file) if credentials_file else CREDENTIALS_FILE
self._cookies_file = pathlib.Path(cookies_file) if cookies_file else COOKIES_FILE

if not (phone_no and pin):
try:
with open(CREDENTIALS_FILE, 'r') as f:
with open(self._credentials_file, 'r') as f:
lines = f.readlines()
self.phone_no = lines[0].strip()
self.pin = lines[1].strip()
except FileNotFoundError:
raise ValueError(f'phone_no and pin must be specified explicitly or via {CREDENTIALS_FILE}')
raise ValueError(f'phone_no and pin must be specified explicitly or via {self._credentials_file}')
else:
self.phone_no = phone_no
self.pin = pin



self.keyfile = keyfile if keyfile else KEY_FILE
try:
Expand All @@ -105,7 +114,7 @@ def __init__(self, phone_no=None, pin=None, keyfile=None, locale='de', save_cook
self._websession = requests.Session()
self._websession.headers = self._default_headers_web
if self._save_cookies:
self._websession.cookies = MozillaCookieJar(COOKIES_FILE)
self._websession.cookies = MozillaCookieJar(self._cookies_file)

def initiate_device_reset(self):
self.sk = SigningKey.generate(curve=NIST256p, hashfunc=hashlib.sha512)
Expand Down Expand Up @@ -224,7 +233,7 @@ def resume_websession(self):
return False

# Only attempt to load if the cookie file exists.
if COOKIES_FILE.exists():
if self._cookies_file.exists():
# Loads session cookies too (expirydate=0).
self._websession.cookies.load(ignore_discard=True, ignore_expires=True)
self._weblogin = True
Expand Down

0 comments on commit 13d13c7

Please sign in to comment.