@@ -13,8 +13,38 @@ def get_token_from_cookie(cookie, token):
1313 if el ['name' ] == token :
1414 return el
1515
16+ def get_token_from_file ():
17+ try :
18+ with open (TOKEN_FILE_PATH , 'r' ) as f :
19+ data = f .read ()
20+ except Exception as e :
21+ print (e )
22+ if (not data or data == "" ):
23+ raise RuntimeError ("Token not found in file" )
24+ else :
25+ return data
26+
27+ def get_cookies_from_login ():
28+ """
29+ Provides the user browser window to login to Notion
30+ Returns the user's cookies which can be used to
31+ access and transfer content to user's Notion account
32+ """
33+ driver = get_browser_driver ()
34+ try :
35+ driver .get (LOGIN_PATH )
36+ WebDriverWait (driver , 300 ).until (
37+ EC .presence_of_element_located ((By .CLASS_NAME ,
38+ "notion-sidebar-container" )))
39+ return driver .get_cookies ()
40+ except Exception as e :
41+ print (e )
42+ finally :
43+ driver .quit ()
44+
1645class NotionClient ():
1746 """
47+
1848 Implements Login and token retrieval
1949
2050 Handles the entire procedure of connecting to User's Notion account,
@@ -30,48 +60,19 @@ def __init__(self):
3060 self .tokenv2_cookie = None
3161 self .tokenv2_key = 'TOKENV2'
3262
33- def get_token_from_file (self ):
34- try :
35- with open (TOKEN_FILE_PATH , 'r' ) as f :
36- data = f .read ()
37- except Exception as e :
38- print (e )
39- if (not data or data == "" ):
40- raise RuntimeError ("Token not found in file" )
41- else :
42- return data
43-
4463 def save_token_file (self ):
4564 if (self .tokenv2_cookie ):
4665 with open (TOKEN_FILE_PATH , 'w' ) as f :
4766 f .write (str (self .tokenv2_cookie ))
4867
49- def get_cookies_from_login (self ):
50- """
51- Provides the user browser window to login to Notion
52- Returns the user's cookies which can be used to
53- access and transfer content to user's Notion account
54- """
55- driver = get_browser_driver ()
56- try :
57- driver .get (LOGIN_PATH )
58- WebDriverWait (driver , 300 ).until (
59- EC .presence_of_element_located ((By .CLASS_NAME ,
60- "notion-sidebar-container" )))
61- return driver .get_cookies ()
62- except Exception as e :
63- print (e )
64- finally :
65- driver .quit ()
66-
6768 def get_tokenv2_cookie (self ):
6869 # Sets 'tokenv2_cookie equal to the particular cookie containing token_v2
6970 if not self .tokenv2_cookie :
7071 try :
71- self .tokenv2_cookie = self . get_token_from_file ()
72+ self .tokenv2_cookie = get_token_from_file ()
7273 except Exception :
7374 try :
74- cookies = self . get_cookies_from_login ()
75+ cookies = get_cookies_from_login ()
7576 self .tokenv2_cookie = get_token_from_cookie (cookies , 'token_v2' )
7677 except Exception as e :
7778 print (e )
0 commit comments