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 )
0 commit comments