From ee513727ca437481cb28e861327bb248e10676da Mon Sep 17 00:00:00 2001 From: matthew55 <78285385+matthew55@users.noreply.github.com> Date: Tue, 27 Aug 2024 16:27:17 -0400 Subject: [PATCH 1/6] Add helper functions for handling cookies --- helperAPI.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/helperAPI.py b/helperAPI.py index 9ac46e2a..6a9e090d 100644 --- a/helperAPI.py +++ b/helperAPI.py @@ -4,6 +4,7 @@ import asyncio import os +import pickle import subprocess import sys import textwrap @@ -706,3 +707,36 @@ def printHoldings(brokerObj: Brokerage, loop=None, mask=True): EMBED["fields"].append(field) printAndDiscord(EMBED, loop, True) print("==============================") + + +def save_cookies(driver, filename, path=None): + filename = filename + if path is not None: + filename = os.path.join(path, filename) + if path is not None and not os.path.exists(path): + os.makedirs(path) + with open(filename, "wb") as f: + pickle.dump(driver.get_cookies(), f) + + +def load_cookies(driver, filename, path=None): + if path is not None: + filename = os.path.join(path, filename) + if not os.path.exists(filename): + return False + try: + with open(filename, "rb") as f: + cookies = pickle.load(f) + for cookie in cookies: + driver.add_cookie(cookie) + except Exception: + return False + return True + + +def clear_cookies(driver, filename, path=None): + if path is not None: + filename = os.path.join(path, filename) + if os.path.exists(filename): + os.remove(filename) + driver.delete_all_cookies() From 8c5fabb73973b2cc87f37524eaa5e30c8ca552f5 Mon Sep 17 00:00:00 2001 From: matthew55 <78285385+matthew55@users.noreply.github.com> Date: Thu, 29 Aug 2024 17:01:41 -0400 Subject: [PATCH 2/6] Add Fidelity cookie support --- fidelityAPI.py | 13 +++++++++++-- helperAPI.py | 3 +-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/fidelityAPI.py b/fidelityAPI.py index ca74d074..9b9040b4 100644 --- a/fidelityAPI.py +++ b/fidelityAPI.py @@ -28,6 +28,9 @@ printHoldings, stockOrder, type_slowly, + save_cookies, + load_cookies, + clear_cookies, ) @@ -75,6 +78,7 @@ def fidelity_init(FIDELITY_EXTERNAL=None, DOCKER=False, botObj=None, loop=None): if driver is None: raise Exception("Error: Unable to get driver") # Log in to Fidelity account + load_cookies(driver, filename=f"fidelity{index}.pkl", path="./creds/") driver.get( "https://digital.fidelity.com/prgw/digital/login/full-page?AuthRedUrl=digital.fidelity.com/ftgw/digital/portfolio/summary" ) @@ -150,6 +154,8 @@ def fidelity_init(FIDELITY_EXTERNAL=None, DOCKER=False, botObj=None, loop=None): (By.XPATH, text_me_button) ), ).click() + # Clear cookies if 2fa page loads + clear_cookies(driver, filename=f"fidelity{index}.pkl", path="./creds/") # Make sure the next page loads fully code_field = "#dom-otp-code-input" WebDriverWait(driver, 10).until( @@ -171,6 +177,8 @@ def fidelity_init(FIDELITY_EXTERNAL=None, DOCKER=False, botObj=None, loop=None): code_field = driver.find_element(by=By.CSS_SELECTOR, value=code_field) code_field.send_keys(str(sms_code)) + remember_device_checkbox = "#dom-trust-device-checkbox + label" + driver.find_element(By.CSS_SELECTOR, remember_device_checkbox).click() continue_btn_selector = "#dom-otp-code-submit-button" driver.find_element(By.CSS_SELECTOR, continue_btn_selector).click() except TimeoutException: @@ -214,6 +222,7 @@ def fidelity_init(FIDELITY_EXTERNAL=None, DOCKER=False, botObj=None, loop=None): fidelity_obj.set_account_totals( name, acct, account_dict[acct]["balance"] ) + save_cookies(driver, filename=f"fidelity{index}.pkl", path="./creds/") print(f"Logged in to {name}!") except Exception as e: fidelity_error(driver, e) @@ -242,8 +251,8 @@ def fidelity_account_info(driver: webdriver) -> dict | None: account_types = javascript_get_classname(driver, "acct-selector__acct-name") # Make sure all lists are the same length if not ( - len(account_numbers) == len(account_values) - and len(account_numbers) == len(account_types) + len(account_numbers) == len(account_values) + and len(account_numbers) == len(account_types) ): shortest = min( len(account_numbers), len(account_values), len(account_types) diff --git a/helperAPI.py b/helperAPI.py index 6a9e090d..03ffbef3 100644 --- a/helperAPI.py +++ b/helperAPI.py @@ -710,7 +710,6 @@ def printHoldings(brokerObj: Brokerage, loop=None, mask=True): def save_cookies(driver, filename, path=None): - filename = filename if path is not None: filename = os.path.join(path, filename) if path is not None and not os.path.exists(path): @@ -729,9 +728,9 @@ def load_cookies(driver, filename, path=None): cookies = pickle.load(f) for cookie in cookies: driver.add_cookie(cookie) + return True except Exception: return False - return True def clear_cookies(driver, filename, path=None): From d70990832569c742bc58b39e2d788094f6207344 Mon Sep 17 00:00:00 2001 From: Nelson Dane <47427072+NelsonDane@users.noreply.github.com> Date: Tue, 10 Sep 2024 08:27:59 -0400 Subject: [PATCH 3/6] fix indents --- fidelityAPI.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fidelityAPI.py b/fidelityAPI.py index 97b7f750..91a67167 100644 --- a/fidelityAPI.py +++ b/fidelityAPI.py @@ -266,8 +266,8 @@ def fidelity_account_info(driver: webdriver) -> dict | None: account_types = javascript_get_classname(driver, "acct-selector__acct-name") # Make sure all lists are the same length if not ( - len(account_numbers) == len(account_values) - and len(account_numbers) == len(account_types) + len(account_numbers) == len(account_values) + and len(account_numbers) == len(account_types) ): shortest = min( len(account_numbers), len(account_values), len(account_types) From 16484598d862660e00f982847f5f40c02a6e1677 Mon Sep 17 00:00:00 2001 From: matthew55 <78285385+matthew55@users.noreply.github.com> Date: Mon, 16 Sep 2024 18:20:59 -0400 Subject: [PATCH 4/6] Remove clearing cookie function --- fidelityAPI.py | 3 --- helperAPI.py | 8 -------- 2 files changed, 11 deletions(-) diff --git a/fidelityAPI.py b/fidelityAPI.py index 91a67167..2e5c69e4 100644 --- a/fidelityAPI.py +++ b/fidelityAPI.py @@ -29,7 +29,6 @@ type_slowly, save_cookies, load_cookies, - clear_cookies, ) @@ -153,8 +152,6 @@ def fidelity_init(FIDELITY_EXTERNAL=None, DOCKER=False, botObj=None, loop=None): (By.XPATH, text_me_button) ), ).click() - # Clear cookies if 2fa page loads - clear_cookies(driver, filename=f"fidelity{index}.pkl", path="./creds/") # Make sure the next page loads fully code_field = "#dom-otp-code-input" WebDriverWait(driver, 10).until( diff --git a/helperAPI.py b/helperAPI.py index b6cc2c02..faf7d0a0 100644 --- a/helperAPI.py +++ b/helperAPI.py @@ -785,11 +785,3 @@ def load_cookies(driver, filename, path=None): return True except Exception: return False - - -def clear_cookies(driver, filename, path=None): - if path is not None: - filename = os.path.join(path, filename) - if os.path.exists(filename): - os.remove(filename) - driver.delete_all_cookies() From 22fa62a2e8f26b135155dd0d56dbae1fc011f0af Mon Sep 17 00:00:00 2001 From: Nelson Dane <47427072+NelsonDane@users.noreply.github.com> Date: Thu, 19 Sep 2024 12:22:19 -0400 Subject: [PATCH 5/6] add clear and improve load --- helperAPI.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/helperAPI.py b/helperAPI.py index faf7d0a0..3977e05e 100644 --- a/helperAPI.py +++ b/helperAPI.py @@ -777,11 +777,18 @@ def load_cookies(driver, filename, path=None): filename = os.path.join(path, filename) if not os.path.exists(filename): return False - try: - with open(filename, "rb") as f: - cookies = pickle.load(f) - for cookie in cookies: + with open(filename, "rb") as f: + cookies = pickle.load(f) + for cookie in cookies: + try: driver.add_cookie(cookie) - return True - except Exception: - return False + except Exception: + continue + return True + +def clear_cookies(driver, filename, path=None): + if path is not None: + filename = os.path.join(path, filename) + if os.path.exists(filename): + os.remove(filename) + driver.delete_all_cookies() From 11f032d09d803c0c6e476c4b5c2ae3b95899d4f8 Mon Sep 17 00:00:00 2001 From: Nelson Dane <47427072+NelsonDane@users.noreply.github.com> Date: Thu, 19 Sep 2024 12:24:20 -0400 Subject: [PATCH 6/6] add return --- helperAPI.py | 1 + 1 file changed, 1 insertion(+) diff --git a/helperAPI.py b/helperAPI.py index 3977e05e..7de3cca5 100644 --- a/helperAPI.py +++ b/helperAPI.py @@ -786,6 +786,7 @@ def load_cookies(driver, filename, path=None): continue return True + def clear_cookies(driver, filename, path=None): if path is not None: filename = os.path.join(path, filename)