import json
import time

import requests
from pymysql.converters import escape_string
from selenium import webdriver
from bs4 import BeautifulSoup
from base.BaseCore import BaseCore

baseCore = BaseCore()
log = baseCore.getLogger()
cnx_ = baseCore.cnx
cursor_ = baseCore.cursor


def flushAndGetToken(browser):
    log.info('======刷新浏览器=====')
    browser.refresh()
    cookie_list = browser.get_cookies()
    cur_url = browser.current_url
    token = cur_url.split('token=')[1]
    log.info(f'===========当前token为：{token}============')
    cookies = {}
    for cookie in cookie_list:
        cookies[cookie['name']] = cookie['value']
    browser.get(cur_url)
    info = browser.page_source
    # res_2 = requests.get(year_url, proxies=ip)
    soup = BeautifulSoup(info, 'html.parser')
    user_name = soup.find('div', class_='weui-desktop_name').text

    return token, cookies, user_name


if __name__ == "__main__":
    while True:
        requests.DEFAULT_RETRIES = 5
        time_start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        log.info(f'开始时间为：{time_start}')

        requests.adapters.DEFAULT_RETRIES = 3
        headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
        }

        opt = webdriver.ChromeOptions()
        opt.add_argument(
            'user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36')

        opt.add_argument("--ignore-certificate-errors")
        opt.add_argument("--ignore-ssl-errors")
        opt.add_experimental_option("excludeSwitches", ["enable-automation"])
        opt.add_experimental_option('excludeSwitches', ['enable-logging'])
        opt.add_experimental_option('useAutomationExtension', False)
        opt.binary_location = r'F:\spider\Google\Chrome\Application\chrome.exe'
        chromedriver = r'F:\spider\cmd100\chromedriver.exe'
        browser = webdriver.Chrome(chrome_options=opt, executable_path=chromedriver)
        url = "https://mp.weixin.qq.com/"
        browser.get(url)
        # 可改动
        while True:
            if browser.current_url != url:
                break
            else:
                time.sleep(10)

        s = requests.session()
        # 获取到token和cookies
        token, cookies, user_name = flushAndGetToken(browser)
        print(token, cookies)
        cookies = json.dumps(cookies)

        # loadinfo = [token,cookies]
        # 保存到数据库中
        insert = f"insert into weixin_tokenCookies (token,cookies,create_time,fenghao_time,user_name,update_time) values ('{token}','{escape_string(cookies)}',now(),DATE_SUB(NOW(), INTERVAL 1 DAY),'{user_name}',now())"
        cursor_.execute(insert)
        cnx_.commit()
        browser.close()
        flg = input('是否继续登录')
        if flg == 'n' or flg == 'N':
            break
    baseCore.close()
    # s.cookies.update(cookies)
    # s.keep_alive = False
