Skip to content

Commit 28ef452

Browse files
committed
Add feature to get token_v2 from login
1 parent c808c86 commit 28ef452

File tree

4 files changed

+49
-40
lines changed

4 files changed

+49
-40
lines changed

src/arguments/notion.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33

44
from .utility import Utility
55
from .error import SearchError
6+
from .settings import LOGIN_PATH
7+
from .settings import TOKEN_FILE_PATH
68

79
from selenium.webdriver.common.by import By
810
from selenium.webdriver.support.ui import WebDriverWait
911
from selenium.webdriver.support import expected_conditions as EC
1012

11-
12-
from webdriver_manager.chrome import ChromeDriverManager
13-
from webdriver_manager.firefox import GeckoDriverManager
14-
from webdriver_manager.microsoft import EdgeChromiumDriverManager
15-
1613
def get_token_from_cookie(cookie, token):
1714
for el in cookie:
1815
if el['name'] == token:
@@ -27,21 +24,23 @@ class NotionClient():
2724
"""
2825
def __init__(self):
2926
self.tokenv2_cookie = None
30-
self.base_url = "https://www.notion.so/"
31-
self.login_path = "login/"
32-
self.linux_path = "/home/{}/Documents/dynamic".format(os.getenv('USER'))
33-
self.mac_path = "/Users/{}/Documents/dynamic".format(os.getenv('USER'))
34-
self.file_name = 'tokenv2_cookie.key'
27+
self.tokenv2_key = 'TOKENV2'
3528

3629
def get_token_from_file(self):
37-
raise FileNotFoundError("File not found")
38-
return None
39-
40-
def get_login_path(self):
41-
return self.base_url + self.login_path
30+
try:
31+
with open(TOKEN_FILE_PATH, 'r') as f:
32+
data = f.read()
33+
except Exception as e:
34+
raise e
35+
if(not data or data==""):
36+
raise Exception("Token not found in file")
37+
else:
38+
return data
4239

4340
def save_token_file(self):
44-
pass
41+
if(self.tokenv2_cookie):
42+
with open(TOKEN_FILE_PATH, 'w') as f:
43+
f.write(str(self.tokenv2_cookie))
4544

4645
def get_cookies_from_login(self):
4746
"""
@@ -51,7 +50,7 @@ def get_cookies_from_login(self):
5150
"""
5251
driver = Utility().get_browser_driver()
5352
try:
54-
driver.get(self.get_login_path())
53+
driver.get(LOGIN_PATH)
5554
WebDriverWait(driver, 300).until(
5655
EC.presence_of_element_located((By.CLASS_NAME,
5756
"notion-sidebar-container")))
@@ -61,7 +60,7 @@ def get_cookies_from_login(self):
6160
finally:
6261
driver.quit()
6362

64-
def set_tokenv2_cookie(self):
63+
def get_tokenv2_cookie(self):
6564
# Sets 'tokenv2_cookie equal to the particular cookie containing token_v2
6665
if not self.tokenv2_cookie:
6766
try:
@@ -74,6 +73,9 @@ def set_tokenv2_cookie(self):
7473
print(e)
7574
self.tokenv2_cookie = None
7675
finally:
77-
os.environ['tokenv2_cookie'] = self.tokenv2_cookie
7876
if self.tokenv2_cookie:
79-
self.save_token_file()
77+
os.environ[self.tokenv2_key] = str(self.tokenv2_cookie)
78+
self.save_token_file()
79+
return self.get_tokenv2_cookie
80+
else:
81+
raise Exception("Cookie unreachable")

src/arguments/search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def search_args(self):
5656
elif self.arguments.GET:
5757
self.api_test_object.get_request()
5858
elif self.arguments.notion:
59-
NotionClient().print_token_v2()
59+
NotionClient().get_tokenv2_cookie()
6060

6161
def search_for_results(self, save=False):
6262
queries = ["What do you want to search", "Tags"]

src/arguments/settings.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
"DYNAMIC_DATA_DIR", str(Path(os.path.expanduser("~")).joinpath(".dynamic"))
88
)
99
PLAYBOOK_FILE = str(Path(DATA_DIR).joinpath("playbook.json"))
10-
TOKEN_COOKIE_FILE = str(Path(DATA_DIR).joinpath("tokenv2_cookie"))
10+
TOKEN_FILE_PATH = str(Path(DATA_DIR).joinpath("tokenv2_cookie"))
1111

1212
try:
1313
os.makedirs(DATA_DIR)
1414
except FileExistsError:
15-
pass
15+
pass
16+
17+
if(not os.path.isfile(TOKEN_FILE_PATH)):
18+
open(TOKEN_FILE_PATH, 'w').close()

src/arguments/utility.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .error import SearchError
1616
from .save import SaveSearchResults
1717
from .markdown import MarkdownRenderer
18-
from .settings import PLAYBOOK_DIR
18+
from .settings import PLAYBOOK_FILE
1919

2020
# Required for OAuth
2121
import json
@@ -47,7 +47,7 @@ def playbook_path(self):
4747
"""
4848
if not os.getenv(self.key):
4949
if(sys.platform=='linux'):
50-
os.environ[self.key] = PLAYBOOK_DIR
50+
os.environ[self.key] = PLAYBOOK_FILE
5151
return os.getenv(self.key)
5252

5353
@property
@@ -351,6 +351,24 @@ def get_ans(self, questions_list):
351351
stackoverflow_panel.display_panel(questions_list)
352352
# Support for reddit searching can also be implemented from here
353353

354+
def get_browser_driver(self):
355+
# Try to install web drivers for one of these browsers
356+
# Chrome, Firefox, Edge (One of them must be installed)
357+
try:
358+
return webdriver.Chrome(ChromeDriverManager().install())
359+
except ValueError:
360+
try:
361+
return webdriver.Firefox(executable_path=
362+
GeckoDriverManager().install())
363+
except ValueError:
364+
try:
365+
return webdriver.Edge(EdgeChromiumDriverManager().\
366+
install())
367+
except ValueError:
368+
print("You do not have one of these supported browsers:" +
369+
"Chrome, Firefox, Edge")
370+
pass
371+
354372
# Get an access token and extract to a JSON file "access_token.json"
355373
@classmethod
356374
def setCustomKey(self):
@@ -375,21 +393,7 @@ def setCustomKey(self):
375393
scope=scopes, redirect_uri=redirect_uri)
376394
auth_url, state = stackApps.authorization_url(authorization_url)
377395

378-
# Try to install web drivers for one of these browsers
379-
# Chrome, Firefox, Edge (One of them must be installed)
380-
try:
381-
driver = webdriver.Chrome(ChromeDriverManager().install())
382-
except ValueError:
383-
try:
384-
driver = webdriver.Firefox(executable_path=
385-
GeckoDriverManager().install())
386-
except ValueError:
387-
try:
388-
driver = webdriver.Edge(EdgeChromiumDriverManager().\
389-
install())
390-
except ValueError:
391-
print("You do not have one of these supported browsers:" +
392-
"Chrome, Firefox, Edge")
396+
driver = self.get_browser_driver()
393397

394398
# Open auth_url in one of the supported browsers
395399
driver.get(auth_url)

0 commit comments

Comments
 (0)