# -*- coding:utf-8 -*-
import json
import os
import ssl
import sys
import time

import undetected_chromedriver as uc
from selenium import webdriver

path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(path)


class Papunika(object):
    def __init__(self):
        self.main()

    def main(self):
        # 浏览器选项
        chrome_options = webdriver.ChromeOptions()
        # 使用headless无界面浏览器模式
        # chrome_options.add_argument('--headless')
        # 解决DevToolsActivePort文件不存在的报错
        chrome_options.add_argument('--no-sandbox')
        # 谷歌文档提到需要加上这个属性来规避bug
        chrome_options.add_argument('--disable-gpu')
        # 设置默认编码为utf-8
        chrome_options.add_argument('--lang=zh-CN')
        chrome_options.add_argument('--disable-javascript')
        chrome_options.add_argument('--disable-java')

        # 隐藏滚动条, 应对一些特殊页面
        chrome_options.add_argument('--hide-scrollbars')
        # 禁止加载图片
        chrome_options.add_argument('blink-settings=imagesEnabled=false')
        # 指定浏览器分辨率
        chrome_options.add_argument('--start-maximized')
        ssl._create_default_https_context = ssl._create_unverified_context
        uc.TARGET_VERSION = 105

        driver = uc.Chrome(options=chrome_options)
        driver.get('https://compass.jinritemai.com/shop?btm_ppre=a0.b0.c0.d0&btm_pre=a2427.b19885.c4158.header_new_menu_3&btm_show_id=16db90e0-98cf-4c9f-850e-5aec5c50a738')
        with open('cookie.json', 'r') as cookief:
            cookieslist = json.load(cookief)
            for cookie in cookieslist:
                driver.add_cookie(cookie)
        driver.get('https://compass.jinritemai.com/shop?btm_ppre=a0.b0.c0.d0&btm_pre=a2427.b19885.c4158.header_new_menu_3&btm_show_id=16db90e0-98cf-4c9f-850e-5aec5c50a738')
        time.sleep(2)
        cookies = driver.get_cookies()
        jsonCookies = json.dumps(cookies)
        with open("cookie.json", 'w') as f:
            f.write(jsonCookies)
        time.sleep(2000000)


if __name__ == "__main__":
    Papunika()