Skip to content

Commit c659c42

Browse files
committed
✨ Add Notion.so login feature
1 parent 51d4fe3 commit c659c42

File tree

5 files changed

+113
-17
lines changed

5 files changed

+113
-17
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@ cython_debug/
3131

3232
# User files related to API testing and authentication
3333
access_token.json
34-
response_data.json
34+
response_data.json
35+
36+
# Selenium data
37+
geckodriver.log

main.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,24 @@
5555
help="View and organise the playbook",
5656
action='store_true')
5757

58+
parser.add_argument("-no",
59+
"--notion",
60+
help="Login to your Notion account to save playbook",
61+
action='store_true')
62+
5863
ARGV = parser.parse_args()
5964

6065
search_flag = Search(ARGV)
6166

6267
if __name__ == "__main__":
6368
if ARGV.start:
6469
print('''
65-
\U0001F604 Hello and Welcome to Dynamic CLI
66-
\U0001F917 Use the following commands to get started
67-
\U0001F50E Search on StackOverflow with '-s'
68-
\U0001F4C4 Open browser to create new Stack Overflow question with '-n [title(optional)]'
70+
\U0001F604 Hello and Welcome to Dynamic CLI
71+
\U0001F917 Use the following commands to get started
72+
\U0001F50E Search on StackOverflow with '-s'
73+
\U0001F4C4 Open browser to create new Stack Overflow question with '-n [title(optional)]'
6974
\U0001F4C2 Save answer to a file with '-file'
70-
\U00002728 Know the version of Dynamic CLI with '-V'
75+
\U00002728 Know the version of Dynamic CLI with '-V'
7176
\U0001F609 See this message again with '-st'
7277
\U00002755 Get help with '-h'
7378
''')

src/arguments/notion.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import os
2+
import json
3+
4+
from .utility import Utility
5+
from .error import SearchError
6+
7+
from selenium import webdriver
8+
from selenium.webdriver.common.by import By
9+
from selenium.webdriver.support.ui import WebDriverWait
10+
from selenium.webdriver.support import expected_conditions as EC
11+
12+
13+
from webdriver_manager.chrome import ChromeDriverManager
14+
from webdriver_manager.firefox import GeckoDriverManager
15+
from webdriver_manager.microsoft import EdgeChromiumDriverManager
16+
17+
def get_token_from_cookie(cookie, token):
18+
for el in cookie:
19+
if el['name'] == token:
20+
return el
21+
pass
22+
23+
class NotionClient():
24+
"""
25+
Handles the entire procedure of connecting to User's Notion account,
26+
generating Notion's tokenv2_cookie, storing it locally and uploading content
27+
to User's space
28+
"""
29+
def __init__(self):
30+
self.tokenv2_cookie = None
31+
self.base_url = "https://www.notion.so/"
32+
self.login_path = "login/"
33+
self.linux_path = "/home/{}/Documents/dynamic".format(os.getenv('USER'))
34+
self.mac_path = "/Users/{}/Documents/dynamic".format(os.getenv('USER'))
35+
self.file_name = 'tokenv2_cookie.key'
36+
37+
def get_token_from_file(self):
38+
raise FileNotFoundError("File not found")
39+
return None
40+
41+
def get_login_path(self):
42+
return self.base_url + self.login_path
43+
44+
def save_token_file(self):
45+
pass
46+
47+
def get_cookies_from_login(self):
48+
"""
49+
Provides the user browser window to login to Notion
50+
Returns the user's cookies which can be used to
51+
access and transfer content to user's Notion account
52+
"""
53+
driver = Utility().get_browser_driver()
54+
try:
55+
driver.get(self.get_login_path())
56+
WebDriverWait(driver, 300).until(
57+
EC.presence_of_element_located((By.CLASS_NAME,
58+
"notion-sidebar-container")))
59+
return driver.get_cookies()
60+
except Exception as e:
61+
print(e)
62+
finally:
63+
driver.quit()
64+
65+
def set_tokenv2_cookie(self):
66+
# Sets 'tokenv2_cookie equal to the particular cookie containing token_v2
67+
if not self.tokenv2_cookie:
68+
try:
69+
self.tokenv2_cookie = self.get_token_from_file()
70+
except:
71+
try:
72+
cookies = self.get_cookies_from_login()
73+
self.tokenv2_cookie = get_token_from_cookie(cookies, 'token_v2')
74+
except Exception as e:
75+
print(e)
76+
77+
def print_token_v2(self):
78+
self.set_tokenv2_cookie()
79+
print(self.tokenv2_cookie)

src/arguments/search.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .save import SaveSearchResults
1111
from .update import UpdateApplication
1212
from .api_test import ApiTesting
13+
from .notion import NotionClient
1314

1415
version = "0.1.0"
1516
class Prompt():
@@ -53,6 +54,8 @@ def search_args(self):
5354
update.check_for_updates()
5455
elif self.arguments.GET:
5556
self.api_test_object.get_request()
57+
elif self.arguments.notion:
58+
NotionClient().print_token_v2()
5659

5760
def search_for_results(self, save=False):
5861
queries = ["What do you want to search", "Tags"]
@@ -78,12 +81,12 @@ def search_for_results(self, save=False):
7881
else:
7982
data = self.utility_object.get_ans(questions)
8083
print('''
81-
\U0001F604 Hopefully you found what you were looking for!
84+
\U0001F604 Hopefully you found what you were looking for!
8285
\U0001F4C2 You can save an answer to a file with '-file'
8386
8487
Not found what you were looking for \U00002754
8588
\U0001F4C4 Open browser and post your question on StackOverflow with '-n [title (optional)]'
86-
89+
8790
\U0001F50E To search more use '-s'
8891
''')
8992

src/arguments/utility.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,18 @@ def get_ans(self, questions_list):
310310
stackoverflow_panel.display_panel(questions_list)
311311
# Support for reddit searching can also be implemented from here
312312

313+
def get_browser_driver(self):
314+
try:
315+
return webdriver.Chrome(ChromeDriverManager().install())
316+
except ValueError:
317+
try:
318+
return webdriver.Firefox(executable_path=GeckoDriverManager().install())
319+
except ValueError:
320+
try:
321+
return webdriver.Edge(EdgeChromiumDriverManager().install())
322+
except ValueError:
323+
raise Exception("Browser/Browser driver now found.")
324+
313325
# Get an access token and extract to a JSON file "access_token.json"
314326
@classmethod
315327
def setCustomKey(self):
@@ -332,15 +344,9 @@ def setCustomKey(self):
332344
# Try to install web drivers for one of these browsers
333345
# Chrome, Firefox, Edge (One of them must be installed)
334346
try:
335-
driver = webdriver.Chrome(ChromeDriverManager().install())
336-
except ValueError:
337-
try:
338-
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
339-
except ValueError:
340-
try:
341-
driver = webdriver.Edge(EdgeChromiumDriverManager().install())
342-
except ValueError:
343-
print("You do not have one of these supported browsers: Chrome, Firefox, Edge")
347+
driver = Utility().get_browser_driver()
348+
except Exception as e:
349+
print(e.message)
344350

345351
# Open auth_url in one of the supported browsers
346352
driver.get(auth_url)

0 commit comments

Comments
 (0)