|
| 1 | +import os |
| 2 | + |
| 3 | +from .utility import get_browser_driver |
| 4 | +from .error import LoginError |
| 5 | +from .settings import LOGIN_PATH |
| 6 | +from .settings import TOKEN_FILE_PATH |
| 7 | + |
| 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 | +def get_token_from_cookie(cookie, token): |
| 13 | + for el in cookie: |
| 14 | + if el['name'] == token: |
| 15 | + return el |
| 16 | + |
| 17 | +def get_token_from_file(): |
| 18 | + try: |
| 19 | + with open(TOKEN_FILE_PATH, 'r') as f: |
| 20 | + data = f.read() |
| 21 | + except Exception as e: |
| 22 | + print(e) |
| 23 | + if(not data or data==""): |
| 24 | + raise RuntimeError("Token not found in file") |
| 25 | + else: |
| 26 | + return data |
| 27 | + |
| 28 | +def get_cookies_from_login(): |
| 29 | + """Capture browser cookies for authentication.""" |
| 30 | + driver = get_browser_driver() |
| 31 | + try: |
| 32 | + driver.get(LOGIN_PATH) |
| 33 | + WebDriverWait(driver, 300).until( |
| 34 | + EC.presence_of_element_located((By.CLASS_NAME, |
| 35 | + "notion-sidebar-container"))) |
| 36 | + return driver.get_cookies() |
| 37 | + except Exception as e: |
| 38 | + print(e) |
| 39 | + finally: |
| 40 | + driver.quit() |
| 41 | + |
| 42 | +class NotionClient(): |
| 43 | + |
| 44 | + """ |
| 45 | + Implements Login and token retrieval. |
| 46 | +
|
| 47 | + Handles the entire procedure of connecting to User's Notion account, |
| 48 | + generating Notion's tokenv2_cookie, storing it locally and uploading |
| 49 | + content to User's space |
| 50 | + """ |
| 51 | + def __init__(self): |
| 52 | + """ |
| 53 | + No input parameters required for instatiating object. |
| 54 | +
|
| 55 | + :tokenv2_cookie: stores the cookie containing user's tokenv2 |
| 56 | + :tokenv2_key: used to create environment variable |
| 57 | + """ |
| 58 | + self.tokenv2_cookie = None |
| 59 | + self.tokenv2_key = 'TOKENV2' |
| 60 | + |
| 61 | + def save_token_file(self): |
| 62 | + if(self.tokenv2_cookie): |
| 63 | + with open(TOKEN_FILE_PATH, 'w') as f: |
| 64 | + f.write(str(self.tokenv2_cookie)) |
| 65 | + |
| 66 | + def get_tokenv2_cookie(self): |
| 67 | + message_success = "Successfully logged into Notion \U0001F389" |
| 68 | + message_failure = "Login unsuccessful. Please try again \U0001F615" |
| 69 | + # Sets 'tokenv2_cookie equal to the particular cookie containing token_v2 |
| 70 | + if not self.tokenv2_cookie: |
| 71 | + try: |
| 72 | + self.tokenv2_cookie = get_token_from_file() |
| 73 | + LoginError(message_success, success=True) |
| 74 | + except Exception: |
| 75 | + try: |
| 76 | + cookies = get_cookies_from_login() |
| 77 | + self.tokenv2_cookie = get_token_from_cookie(cookies, 'token_v2') |
| 78 | + except Exception: |
| 79 | + self.tokenv2_cookie = None |
| 80 | + finally: |
| 81 | + if self.tokenv2_cookie: |
| 82 | + os.environ[self.tokenv2_key] = str(self.tokenv2_cookie) |
| 83 | + LoginError(message_success, success=True) |
| 84 | + self.save_token_file() |
| 85 | + else: |
| 86 | + LoginError(message_failure, success=False) |
| 87 | + return self.get_tokenv2_cookie |
0 commit comments