from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from auth_data import username, password
import time
import random
import pickle

driver = webdriver.Chrome()

try:
    #    # cookies_load
    # -----------------------------------------------------------------------------------------
    driver.get('https://www.instagram.com')
    time.sleep(1)

    driver.find_element_by_css_selector('.bIiDR').click()
    time.sleep(1)

    username_input = driver.find_element_by_name('username')
    username_input.clear()
    username_input.send_keys(username)
    time.sleep(1)

    password_input = driver.find_element_by_name('password')
    password_input.clear()
    password_input.send_keys(password)

    password_input.send_keys(Keys.ENTER)
    time.sleep(3)

    pickle.dump(driver.get_cookies(), open(f"{username}_cookies", "wb"))

    driver.close()
    driver.quit()
    time.sleep(2)

    # --------------------------------------------------------------------------------
    # cookies_upload

    driver.get('https://www.instagram.com')
    time.sleep(1)

    driver.find_element_by_css_selector('.bIiDR').click()
    time.sleep(1)

    driver.delete_all_cookies()

    for cookie in pickle.load(open(f"{username}_cookies", "rb")):
        driver.add_cookie(cookie)
    time.sleep(3)

    driver.refresh()
    time.sleep(10)

except Exception as ex:
    print(ex)
finally:
    driver.close()
    driver.quit()
