47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
import http.cookiejar
|
|
|
|
import undetected_chromedriver as uc
|
|
from selenium.common import InvalidSessionIdException
|
|
from selenium.webdriver.chrome.options import Options
|
|
from selenium.webdriver.chrome.webdriver import WebDriver
|
|
from webdriver_manager.chrome import ChromeDriverManager
|
|
|
|
driver: WebDriver = None
|
|
|
|
|
|
def create_driver(headless=False):
|
|
global driver
|
|
if driver:
|
|
try:
|
|
driver.close()
|
|
except InvalidSessionIdException:
|
|
pass
|
|
|
|
options = Options()
|
|
if headless:
|
|
options.add_argument('--headless')
|
|
options.add_argument("--window-size=1920,1200")
|
|
options.add_argument("--incognito")
|
|
driver = uc.Chrome(driver_executable_path=ChromeDriverManager().install(), options=options, use_subprocess=False)
|
|
return driver
|
|
|
|
|
|
def load_cookies(file):
|
|
cookie_jar = http.cookiejar.MozillaCookieJar()
|
|
cookie_jar.load(file, ignore_discard=True, ignore_expires=True)
|
|
for cookie in cookie_jar:
|
|
driver.add_cookie({"name": cookie.name, "value": cookie.value, 'domain': cookie.domain})
|
|
|
|
|
|
def save_cookies(driver, location):
|
|
with open(location, 'w') as file:
|
|
file.write('# Netscape HTTP Cookie File\n')
|
|
for cookie in driver.get_cookies():
|
|
file.write(f"{cookie['domain']}\t"
|
|
f"{'TRUE' if cookie['domain'].startswith('.') else 'FALSE'}\t"
|
|
f"{cookie['path']}\t"
|
|
f"{'TRUE' if 'secure' in cookie else 'FALSE'}\t"
|
|
f"{str(int(cookie['expiry'])) if 'expiry' in cookie else '0'}\t"
|
|
f"{cookie['name']}\t"
|
|
f"{cookie['value']}\n")
|