Skip to content

Commit

Permalink
Improve handling of multiple credentials (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
RealCLanger authored Jan 29, 2025
1 parent 87831a2 commit e41470a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
12 changes: 4 additions & 8 deletions pytr/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def get_settings(tr):
return formatted_json


def login(phone_no=None, pin=None, web=True):
def login(phone_no=None, pin=None, web=True, store_credentials=False):
"""
If web is true, use web login method as else simulate app login.
Check if credentials file exists else create it.
If web is true, use web login method, else simulate app login.
Handle credentials parameters and store to credentials file if requested.
If no parameters are set but are needed then ask for input
"""
log = get_logger(__name__)
Expand Down Expand Up @@ -52,17 +52,13 @@ def login(phone_no=None, pin=None, web=True):
print("Please enter your TradeRepublic pin:")
pin = getpass(prompt="Pin (Input is hidden):")

print('Save credentials? Type "y" to save credentials:')
save = input()
if save == "y":
if store_credentials:
with open(CREDENTIALS_FILE, "w") as f:
f.writelines([phone_no + "\n", pin + "\n"])

log.info(f"Saved credentials in {CREDENTIALS_FILE}")

else:
save_cookies = False
log.info("Credentials not saved")

tr = TradeRepublicApi(phone_no=phone_no, pin=pin, save_cookies=save_cookies)

Expand Down
9 changes: 6 additions & 3 deletions pytr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ def __init__(
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:
Expand All @@ -117,6 +114,12 @@ def __init__(
self.phone_no = phone_no
self.pin = pin

self._cookies_file = (
pathlib.Path(cookies_file)
if cookies_file
else BASE_DIR / f"cookies.{self.phone_no}.txt"
)

self.keyfile = keyfile if keyfile else KEY_FILE
try:
with open(self.keyfile, "rb") as f:
Expand Down
43 changes: 38 additions & 5 deletions pytr/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def formatter(prog):
"-n", "--phone_no", help="TradeRepublic phone number (international format)"
)
parser_login_args.add_argument("-p", "--pin", help="TradeRepublic pin")
parser_login_args.add_argument(
"--store_credentials",
help="Store credentials (Phone number, pin, cookies) for next usage",
action="store_true",
default=False,
)

# sort
parser_sort_export = argparse.ArgumentParser(add_help=False)
Expand Down Expand Up @@ -245,7 +251,12 @@ def main():
log.debug("logging is set to debug")

if args.command == "login":
login(phone_no=args.phone_no, pin=args.pin, web=not args.applogin)
login(
phone_no=args.phone_no,
pin=args.pin,
web=not args.applogin,
store_credentials=args.store_credentials,
)

elif args.command == "dl_docs":
if args.last_days == 0:
Expand All @@ -255,7 +266,12 @@ def main():
datetime.now().astimezone() - timedelta(days=args.last_days)
).timestamp()
dl = DL(
login(phone_no=args.phone_no, pin=args.pin, web=not args.applogin),
login(
phone_no=args.phone_no,
pin=args.pin,
web=not args.applogin,
store_credentials=args.store_credentials,
),
args.output,
args.format,
since_timestamp=since_timestamp,
Expand All @@ -268,15 +284,32 @@ def main():
# TODO
print("Not implemented yet")
elif args.command == "get_price_alarms":
Alarms(login(phone_no=args.phone_no, pin=args.pin, web=not args.applogin)).get()
Alarms(
login(
phone_no=args.phone_no,
pin=args.pin,
web=not args.applogin,
store_credentials=args.store_credentials,
)
).get()
elif args.command == "details":
Details(
login(phone_no=args.phone_no, pin=args.pin, web=not args.applogin),
login(
phone_no=args.phone_no,
pin=args.pin,
web=not args.applogin,
store_credentials=args.store_credentials,
),
args.isin,
).get()
elif args.command == "portfolio":
p = Portfolio(
login(phone_no=args.phone_no, pin=args.pin, web=not args.applogin)
login(
phone_no=args.phone_no,
pin=args.pin,
web=not args.applogin,
store_credentials=args.store_credentials,
)
)
p.get()
if args.output is not None:
Expand Down

0 comments on commit e41470a

Please sign in to comment.