"""模拟扫码登录"""
import json
import time

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from base.BaseCore import BaseCore
from pymysql.converters import escape_string
baseCore = BaseCore()
log = baseCore.getLogger()
cnx_ = baseCore.cnx
cursor_ = baseCore.cursor
def createDriver():
    chrome_driver = r'D:\cmd100\chromedriver.exe'
    path = Service(chrome_driver)
    chrome_options = webdriver.ChromeOptions()
    chrome_options.binary_location = r'D:\Google\Chrome\Application\chrome.exe'
    # 设置代理
    # proxy = "127.0.0.1:8080"  # 代理地址和端口
    # chrome_options.add_argument('--proxy-server=http://' + proxy)
    driver = webdriver.Chrome(service=path, chrome_options=chrome_options)
    return driver

def flushAndGetToken():
    log.info('======刷新浏览器=====')
    browser.refresh()
    cookie_list = browser.get_cookies()
    cookies = {}
    print(cookie_list)
    # for cookie in cookie_list:
    #     cookies[cookie['name']] = cookie['value']
    # print(cookies)
    # return cookies
    print(type(cookie_list))
    return cookie_list

if __name__ == "__main__":
    urlqcc = 'https://www.qcc.com/'
    browser = createDriver()
    browser.get(urlqcc)
    wait = WebDriverWait(browser, 10)
    wait.until(EC.presence_of_element_located((By.CLASS_NAME, "nav-item")))
    # page_source = browser.page_source
    # soup = BeautifulSoup(page_source,'html.parser')
    # print(soup)
    browser.find_element(By.CLASS_NAME, 'nav-item').click()
    time.sleep(20)
    cookies = flushAndGetToken()
    # print(cookies)
    # cookies = json.dumps(cookies)
    cookies = str(cookies)
    # print(cookies)
    insert = f"insert into QCC_token (cookies,create_time,fenghao_time,update_time) values ('{escape_string(cookies)}',now(),DATE_SUB(NOW(), INTERVAL 1 DAY),now())"
    cursor_.execute(insert)
    cnx_.commit()

    baseCore.close()





