# AUTOGENERATED! DO NOT EDIT! File to edit: 01_Selenium.ipynb (unless otherwise specified).

__all__ = ['save_authentification_session', 'set_auth_session_with_selenium', 'add_auth_session_web_driver',
           'load_json']

# Cell
import json

def save_authentification_session(auth_file='auth.json'):
    global driver
    authentification_session={
        'cookies': driver.get_cookies(),
        'start_url': driver.current_url,
        'user_agent': driver.execute_script('return navigator.userAgent')
    }
    json.dump(authentification_session,open(auth_file,"w"))
    return(authentification_session)

# Cell
import time
import os

def set_auth_session_with_selenium(log_in_url,auth_file='auth.json'):
    global driver

    # Import Chrome
    import undetected_chromedriver as uc
    uc.TARGET_VERSION = 86

    def driver_():
        global driver
        uc.install()
        from selenium import webdriver

        # Instanciate the web driver
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--disable-extensions')
        chrome_options.add_argument('--profile-directory=Default')
        chrome_options.add_argument("--disable-plugins-discovery")
        driver = webdriver.Chrome(chrome_options=chrome_options)

    try :
        driver_()
    except :
        os.remove('./chromedriver')
        driver_()


    print('You have 2 minutes to login & define start_url you want !')
    driver.get(log_in_url)

    time.sleep(2*60)

    auth=save_authentification_session(auth_file)
    driver.quit()

    return auth

# Cell

load_json= lambda file='auth.json' : json.load(open(file,'r'))

def add_auth_session_web_driver(auth_file='auth.json'):
    global driver

    auth=load_json(auth_file)

    # Request a first time the start url to append the right cookies
    driver.get(auth['start_url'])
    # Set the cookies to the right values
    _=[driver.add_cookie(c) for c in auth['cookies']]

    driver.get(auth['start_url'])