Skip to content

Commit a2a7521

Browse files
authored
Merge pull request #342 from matthew55/generic-cookie-functions
Add helper functions for handling cookies
2 parents fbf4d90 + 11f032d commit a2a7521

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

fidelityAPI.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
printHoldings,
2828
stockOrder,
2929
type_slowly,
30+
save_cookies,
31+
load_cookies,
3032
)
3133

3234

@@ -74,6 +76,7 @@ def fidelity_init(FIDELITY_EXTERNAL=None, DOCKER=False, botObj=None, loop=None):
7476
if driver is None:
7577
raise Exception("Error: Unable to get driver")
7678
# Log in to Fidelity account
79+
load_cookies(driver, filename=f"fidelity{index}.pkl", path="./creds/")
7780
driver.get(
7881
"https://digital.fidelity.com/prgw/digital/login/full-page?AuthRedUrl=digital.fidelity.com/ftgw/digital/portfolio/summary"
7982
)
@@ -186,6 +189,8 @@ def fidelity_init(FIDELITY_EXTERNAL=None, DOCKER=False, botObj=None, loop=None):
186189

187190
code_field = driver.find_element(by=By.CSS_SELECTOR, value=code_field)
188191
code_field.send_keys(str(sms_code))
192+
remember_device_checkbox = "#dom-trust-device-checkbox + label"
193+
driver.find_element(By.CSS_SELECTOR, remember_device_checkbox).click()
189194
continue_btn_selector = "#dom-otp-code-submit-button"
190195
driver.find_element(By.CSS_SELECTOR, continue_btn_selector).click()
191196
except TimeoutException:
@@ -229,6 +234,7 @@ def fidelity_init(FIDELITY_EXTERNAL=None, DOCKER=False, botObj=None, loop=None):
229234
fidelity_obj.set_account_totals(
230235
name, acct, account_dict[acct]["balance"]
231236
)
237+
save_cookies(driver, filename=f"fidelity{index}.pkl", path="./creds/")
232238
print(f"Logged in to {name}!")
233239
except Exception as e:
234240
fidelity_error(driver, e)

helperAPI.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import asyncio
66
import os
7+
import pickle
78
import subprocess
89
import sys
910
import textwrap
@@ -760,3 +761,35 @@ def printHoldings(brokerObj: Brokerage, loop=None, mask=True):
760761
EMBED["fields"].append(field)
761762
printAndDiscord(EMBED, loop, True)
762763
print("==============================")
764+
765+
766+
def save_cookies(driver, filename, path=None):
767+
if path is not None:
768+
filename = os.path.join(path, filename)
769+
if path is not None and not os.path.exists(path):
770+
os.makedirs(path)
771+
with open(filename, "wb") as f:
772+
pickle.dump(driver.get_cookies(), f)
773+
774+
775+
def load_cookies(driver, filename, path=None):
776+
if path is not None:
777+
filename = os.path.join(path, filename)
778+
if not os.path.exists(filename):
779+
return False
780+
with open(filename, "rb") as f:
781+
cookies = pickle.load(f)
782+
for cookie in cookies:
783+
try:
784+
driver.add_cookie(cookie)
785+
except Exception:
786+
continue
787+
return True
788+
789+
790+
def clear_cookies(driver, filename, path=None):
791+
if path is not None:
792+
filename = os.path.join(path, filename)
793+
if os.path.exists(filename):
794+
os.remove(filename)
795+
driver.delete_all_cookies()

0 commit comments

Comments
 (0)