Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of multiple credentials #144

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions pytr/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ 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=True):
RealCLanger marked this conversation as resolved.
Show resolved Hide resolved
"""
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__)
save_cookies = True

if phone_no is None and CREDENTIALS_FILE.is_file():
log.info("Found credentials file")
Expand All @@ -52,19 +51,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)
tr = TradeRepublicApi(phone_no=phone_no, pin=pin, save_cookies=True)
RealCLanger marked this conversation as resolved.
Show resolved Hide resolved

if web:
# Use same login as app.traderepublic.com
Expand Down
7 changes: 4 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,10 @@ 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
16 changes: 11 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 and pin) for next usage",
action="store_true",
default=True
RealCLanger marked this conversation as resolved.
Show resolved Hide resolved
)

# sort
parser_sort_export = argparse.ArgumentParser(add_help=False)
Expand Down Expand Up @@ -245,7 +251,7 @@ 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 +261,7 @@ 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 +274,15 @@ 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