from wechat_action.sql_tools import save_ad_layout_result
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from sqlalchemy import Table
from logging import handlers
import logging
import random
import time
import json
import re
import os


class CreateAd:
    def __init__(self, login_ad, layout_db, user_id):
        self.user_id = user_id
        self.service_name = login_ad.service_name
        self.wechat_name = login_ad.wechat_name
        # self.driver = LogIn(service_name, wechat_name).get_driver_loged()
        self.driver = login_ad.get_driver_loged()
        self.get_into_create_page()
        self.db = layout_db
        self.metadata = self.db.metadata
        self.sql_session = self.db.DBSession()
        self.layout_table = Table('layout_record', self.metadata,
                                  autoload=True, autoload_with=self.db.engine)
        self.send_file_limit_num = 8

    def get_into_create_page(self):
        # 进入创建页面
        self.driver.find_element_by_id('material').click()
        WebDriverWait(self.driver, 10).until(lambda driver: driver.find_element_by_class_name('ui-fl-r'))
        time.sleep(random.uniform(0.1, 0.2))
        self.driver.find_element_by_class_name('ui-fl-r').click()
        WebDriverWait(self.driver, 5).until(lambda driver: driver.find_element_by_css_selector(
            '#wxadcontainer > div:nth-child(1) > div:nth-child(2) > div.dialog-1fj_N480ZT > div > div > div:nth-child(1) > div.dialogCardFooter-17KpBD1lgN > button'))
        self.driver.find_element_by_css_selector(
            '#wxadcontainer > div:nth-child(1) > div:nth-child(2) > div.dialog-1fj_N480ZT > div > div > div:nth-child(1) > div.dialogCardFooter-17KpBD1lgN > button').click()
        self.driver.switch_to.window(self.driver.window_handles[-1])
        WebDriverWait(self.driver, 100).until(lambda driver: driver.find_element_by_class_name('addContent-8pexaaAGYy'))
        self.driver.find_element_by_class_name('addContent-8pexaaAGYy').click()
        WebDriverWait(self.driver, 100).until(lambda driver: driver.find_element_by_class_name('topArea-qOwEAeNuIn'))

    def get_layout(self):
        # 数据库获取数据,然后进行编排
        # 创建编排
        pass

    def set_advertisement_sign(self):
        # 设置广告标记
        now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        self.driver.find_element_by_xpath('//*[@class="icon-edQB0KK2VG"]').click()
        input_element = self.driver.find_element_by_xpath('//input[@class="input-2lFnByGCRh"]')
        input_element.send_keys(Keys.BACKSPACE)
        input_element.send_keys(now_time)
        return now_time

    def set_background_color(self):
        color_buttons = self.driver.find_elements_by_class_name('adui-cp-picker')
        c_buttons_can_use = []
        for _ in color_buttons:
            if _.is_displayed() and _.is_enabled():
                c_buttons_can_use.append(_)
        c_buttons_can_use[0].click()
        time.sleep(random.uniform(0.2, 0.3))
        input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
        for _ in input_elements:
            if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == 'FFFFFF':
                _.click()
                _.send_keys('F7F7F7')

    def set_head_assemb(self, info):
        def single_page_set():

            self.driver.find_element_by_xpath('//*[@id="stage-sidebar"]/div[1]/div/div[1]/div/div').click()
            time.sleep(random.uniform(0.1, 0.2))

            file_name = os.path.basename(info_v)
            self.driver.find_element_by_class_name('upload-img-item-inner-2gsg7NjaZ8').click()
            WebDriverWait(self.driver, 10).until(
                lambda x: len([_ for _ in self.driver.find_elements_by_class_name('title-29sncpKgTl') if
                               _.is_displayed() and _.is_enabled()]) > 0)

            turn_page_buttons = self.driver.find_elements_by_class_name('paginationIcon-1EfoH0sNRF')
            can_use_button = []
            for _ in turn_page_buttons:
                if _.is_enabled() and _.is_displayed():
                    can_use_button.append(_)
            # 不断翻页获取到元素为止
            chose_over = False
            while True:
                page_elements = self.driver.find_elements_by_class_name('title-29sncpKgTl')
                for _ in page_elements:
                    if _.is_displayed() and _.is_enabled():
                        if file_name in _.text:
                            _.click()
                            chose_over = True
                            break
                if chose_over or len(can_use_button) == 0:
                    break
                # 翻到最后一页时停止
                page_text = self.driver.find_element_by_class_name('count_small-37CcvfzoTl').text
                page_nums = re.findall('\d+', page_text)
                if int(page_nums[0].strip()) == int(page_nums[1].strip()):
                    break
                # 翻页
                can_use_button[2].click()
                # 确保翻页成功
                WebDriverWait(self.driver, 10).until(
                    lambda x: page_text != self.driver.find_element_by_class_name('count_small-37CcvfzoTl').text)

            self.driver.find_element_by_xpath('//*[@id="test_material_container_confirm"]').click()
            try:
                WebDriverWait(self.driver, 4).until(
                    lambda driver: driver.find_element_by_class_name(
                        'btnFist-uueBS6DQFa'))
                _ = self.driver.find_element_by_class_name(
                    'btnFist-uueBS6DQFa')
                WebDriverWait(self.driver, 4).until(
                    lambda x: (_.is_displayed() and _.is_enabled()))
                time.sleep(1)
                _.click()

                WebDriverWait(self.driver, 100).until(
                    lambda driver: driver.find_element_by_class_name(
                        'btn-3E823IXt3m'))
                _ = self.driver.find_element_by_class_name(
                    'btn-3E823IXt3m')
                WebDriverWait(self.driver, 100).until(
                    lambda x: (_.is_displayed() and _.is_enabled()))
                time.sleep(1)
                _.click()


            except Exception as e:
                pass
            logging.info('头版首页单图上传结束')

        def multi_page_set():
            self.driver.find_element_by_css_selector(
                '#stage-sidebar > div.topArea-qOwEAeNuIn > div > div:nth-child(2)').click()
            time.sleep(random.uniform(0.1, 0.2))
            page_size = len(info_v)
            if page_size == 3:
                pass
            if page_size == 4:
                self.driver.find_element_by_css_selector(
                    '#stage-settings > div:nth-child(1) > div > div > div:nth-child(3) > div.adui-form-item > div.adui-form-control > div > button:nth-child(2)').click()

            if page_size == 6:
                self.driver.find_element_by_css_selector(
                    '#stage-settings > div:nth-child(1) > div > div > div:nth-child(3) > div.adui-form-item > div.adui-form-control > div > button:nth-child(3)').click()

            time.sleep(random.uniform(0.1, 0.2))
            self.driver.find_element_by_class_name('imageUploadItem-tA9JX0RWua').click()
            WebDriverWait(self.driver, 1000).until(
                lambda driver: self.driver.find_element_by_class_name('adui-tabs-tab')
            )
            input_elements = self.driver.find_elements_by_tag_name('input')
            input_find_element = None
            for _ in input_elements:
                if '输入关键词搜索素材' in _.get_attribute('placeholder'):
                    input_find_element = _

            for _ in info_v:
                file_name = os.path.basename(_)
                logging.info(file_name)
                input_find_element.send_keys(file_name)
                input_find_element.send_keys(Keys.RETURN)
                WebDriverWait(self.driver, 1000).until(
                    lambda driver: driver.find_element_by_class_name('img-2HvhMmpnzP'))
                file_element = self.driver.find_element_by_class_name('img-2HvhMmpnzP')
                WebDriverWait(self.driver, 1000).until(
                    lambda x: (file_element.is_displayed() and file_element.is_enabled()))
                ActionChains(self.driver).move_to_element(file_element).perform()
                time.sleep(random.uniform(0.5, 1))
                file_element.click()

                for i in range(len(file_name) + 10):
                    input_find_element.send_keys(Keys.BACKSPACE)
                time.sleep(random.uniform(0.5, 1))
            self.driver.find_element_by_xpath('/html/body/div[12]/div/div/div[2]/div/div[3]/button[2]').click()

            # 切图操作如果有的话进行对应操作.
            for i in range(len(info_v) + 1):
                try:
                    WebDriverWait(self.driver, 6).until(
                        lambda driver: driver.find_element_by_xpath(
                            '/html/body/div[13]/div/div/div[2]/div/div[3]/button[2]'))
                    self.driver.find_element_by_xpath('/html/body/div[13]/div/div/div[2]/div/div[3]/button[2]').click()
                except:
                    pass

            logging.info('头版多图选择 结束')

        def movie_set():
            file_name = os.path.basename(info_v)
            self.driver.find_element_by_xpath('//*[@id="stage-sidebar"]/div[1]/div/div[3]/div/div').click()
            self.driver.find_element_by_xpath('//*[@class="comptEditButton-2JsnAFdOGZ"]').click()

            WebDriverWait(self.driver, 100).until(
                lambda x: len([_ for _ in self.driver.find_elements_by_class_name('title-29sncpKgTl') if
                               _.is_displayed() and _.is_enabled()]) > 0)
            #
            turn_page_buttons = self.driver.find_elements_by_class_name('paginationIcon-1EfoH0sNRF')
            can_use_button = []
            for _ in turn_page_buttons:
                if _.is_enabled() and _.is_displayed():
                    can_use_button.append(_)
            # 不断翻页获取到元素为止
            chose_over = False
            while True:
                page_elements = self.driver.find_elements_by_class_name('title-29sncpKgTl')
                for _ in page_elements:
                    if _.is_displayed() and _.is_enabled():
                        if file_name in _.text:
                            _.click()
                            chose_over = True
                            break
                if chose_over or len(can_use_button) == 0:
                    # can_use_button 数量说明是否可以翻页,如果不可以翻页,则直接停止
                    break
                # 翻到最后一页时停止
                page_text_elements = self.driver.find_elements_by_class_name('count_small-37CcvfzoTl')
                for _ in page_text_elements:
                    if _.is_enabled() and _.is_displayed():
                        page_text = _.text
                        page_text_element = _

                page_nums = re.findall('\d+', page_text)
                if int(page_nums[0].strip()) == int(page_nums[1].strip()):
                    break
                # 翻页
                can_use_button[2].click()
                # 确保翻页成功
                WebDriverWait(self.driver, 10).until(
                    lambda x: page_text != page_text_element.text)
            sc_buttons = self.driver.find_elements_by_id('test_material_container_confirm')
            for _ in sc_buttons:
                if _.is_enabled() and _.is_displayed():
                    _.click()

        info_key = list(info.keys())[0]
        info_v = info[info_key]

        if info_key == 'page':
            single_page_set()
        if info_key == 'multi_page':
            multi_page_set()
        if info_key == 'movie':
            movie_set()

        # 设置头部组件
        pass

    def set_page(self, page_path):
        # 设置图片模块
        self.driver.find_element_by_xpath('//*[@id="stage-sidebar"]/section/div[2]/div[1]/div/div').click()
        time.sleep(random.uniform(0.1, 0.2))

        file_name = os.path.basename(page_path)
        upload_elements = self.driver.find_elements_by_class_name('upload-img-item-inner-2gsg7NjaZ8')
        for _ in upload_elements:
            if _.is_displayed() and _.is_enabled():
                _.click()
        WebDriverWait(self.driver, 10).until(
            lambda x: len([_ for _ in self.driver.find_elements_by_class_name('title-29sncpKgTl') if
                           _.is_displayed() and _.is_enabled()]) > 0)

        turn_page_buttons = self.driver.find_elements_by_class_name('paginationIcon-1EfoH0sNRF')
        can_use_button = []
        for _ in turn_page_buttons:
            if _.is_enabled() and _.is_displayed():
                can_use_button.append(_)
        # 不断翻页获取到元素为止
        chose_over = False
        while True:
            page_elements = self.driver.find_elements_by_class_name('title-29sncpKgTl')
            for _ in page_elements:
                if _.is_displayed() and _.is_enabled():
                    if file_name in _.text:
                        _.click()
                        chose_over = True
                        break
            if chose_over or len(can_use_button) == 0:
                # can_use_button 数量说明是否可以翻页,如果不可以翻页,则直接停止
                break
            # 翻到最后一页时停止
            page_text_elements = self.driver.find_elements_by_class_name('count_small-37CcvfzoTl')
            for _ in page_text_elements:
                if _.is_enabled() and _.is_displayed():
                    page_text = _.text
                    page_text_element = _

            page_nums = re.findall('\d+', page_text)
            if int(page_nums[0].strip()) == int(page_nums[1].strip()):
                break
            # 翻页
            can_use_button[2].click()
            # 确保翻页成功
            WebDriverWait(self.driver, 10).until(
                lambda x: page_text != page_text_element.text)

        sc_buttons = self.driver.find_elements_by_id('test_material_container_confirm')
        for _ in sc_buttons:
            if _.is_enabled() and _.is_displayed():
                _.click()
        try:
            WebDriverWait(self.driver, 4).until(
                lambda driver: driver.find_element_by_class_name(
                    'btnFist-uueBS6DQFa'))
            _ = self.driver.find_element_by_class_name(
                'btnFist-uueBS6DQFa')
            WebDriverWait(self.driver, 4).until(
                lambda x: (_.is_displayed() and _.is_enabled()))
            # 已经出现但任需要等待
            time.sleep(1)
            _.click()

            WebDriverWait(self.driver, 100).until(
                lambda x: len([_ for _ in self.driver.find_elements_by_class_name('btn-3E823IXt3m') if
                               _.is_displayed() and _.is_enabled()]) > 0)
            for _ in self.driver.find_elements_by_class_name('btn-3E823IXt3m'):
                if _.is_displayed() and _.is_enabled():
                    _.click()
        except Exception as e:
            logging.info(e)
        logging.info('图片模块设置结束')

    def set_content(self, config_info):
        # 设置文本模块
        # 1.文本内容2.文本颜色3.是否加粗4.文本位置设置5.文本大小

        # 设置文本内容
        self.driver.find_element_by_xpath('//*[@id="stage-sidebar"]/section/div[2]/div[5]/div/div').click()
        time.sleep(random.uniform(0.2, 0.3))
        input_elements = self.driver.find_elements_by_xpath('//textarea[@class="adui-input-base"]')
        for _ in input_elements:
            if _.is_enabled() and _.is_displayed():
                _.send_keys(config_info['content'])

        # 设置颜色
        if config_info['color'] != '595959':
            color_buttons = self.driver.find_elements_by_class_name('adui-cp-picker')
            c_buttons_can_use = []
            for _ in color_buttons:
                if _.is_displayed() and _.is_enabled():
                    c_buttons_can_use.append(_)
            c_buttons_can_use[1].click()
            time.sleep(random.uniform(0.2, 0.3))

            input_elements = self.driver.find_elements_by_tag_name('input')
            for _ in input_elements:
                if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '595959':
                    _.click()
                    _.send_keys(config_info['color'])

        # 设置是否加粗
        if config_info['is_bold']:
            big_elements = self.driver.find_elements_by_class_name('adui-button-content')
            for _ in big_elements:
                if _.text == '加粗' and _.is_displayed() and _.is_enabled():
                    _.click()

        # 设置文本位置
        if config_info['loc'] != 'left':
            loc_buttons = self.driver.find_elements_by_xpath(
                "//div[contains(@class, 'adui-button-group_banner')]//button")
            loc_buttons_can_use = []
            for _ in loc_buttons:
                if _.is_enabled() and _.is_displayed():
                    loc_buttons_can_use.append(_)
            if config_info['loc'] == 'middle':
                loc_buttons_can_use[1].click()
            if config_info['loc'] == 'right':
                loc_buttons_can_use[2].click()

        # 设置文本大小
        if config_info['str_num'] != 15:
            str_buttons = self.driver.find_elements_by_xpath("//*[@class='adui-select-selection__rendered']")
            str_buttons_can_use = []
            for _ in str_buttons:
                if _.is_displayed() and _.is_enabled():
                    str_buttons_can_use.append(_)
            str_buttons_can_use[1].click()
            time.sleep(random.uniform(0.5, 1))

            str_num_buttons = self.driver.find_elements_by_xpath("//*[@class='adui-select-dropdown-menu-item']")
            str_num_can_use = []
            for _ in str_num_buttons:
                if _.is_displayed() and _.is_enabled():
                    str_num_can_use.append(_)
            str_num_map = {14: 0, 16: 1, 18: 2, 20: 3, 24: 4, 36: 5}
            str_num_can_use[str_num_map[config_info['str_num']]].click()

        # 设置文本边距
        if config_info['str_distance'][0] != 22 or config_info['str_distance'][1] != 22:
            distance_buttons = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
            distance_buttons_can_use = []
            for _ in distance_buttons:
                if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '22':
                    distance_buttons_can_use.append(_)
            distance_buttons_can_use[0].click()
            for i in range(4):
                distance_buttons_can_use[0].send_keys(Keys.BACKSPACE)
            distance_buttons_can_use[0].send_keys(config_info['str_distance'][0])
            distance_buttons_can_use[1].click()
            for i in range(4):
                distance_buttons_can_use[1].send_keys(Keys.BACKSPACE)
            distance_buttons_can_use[1].send_keys(config_info['str_distance'][1])

    def set_follow_button(self, button_type):
        # 设置关注按钮
        # 1.设置button文本内容2.是否加粗3.设置字体颜色,边框颜色,填充色4.设置边距

        self.driver.find_element_by_xpath('//*[@id="stage-sidebar"]/section/div[4]/div[2]/div/div').click()
        if button_type == 1:
            input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
            for _ in input_elements:
                if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '关注公众号':
                    _.click()
                    _.clear()
                    for i in range(10):
                        _.send_keys(Keys.BACKSPACE)
                    _.send_keys('保存阅读记录')
            big_elements = self.driver.find_elements_by_xpath('//span[@class="adui-button-content"]')
            for _ in big_elements:
                if _.text == '加粗' and _.is_displayed() and _.is_enabled():
                    _.click()
            input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
            for _ in input_elements:
                if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '07C160':
                    _.click()
                    _.send_keys('DE2821')
        if button_type == 2:
            input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
            for _ in input_elements:
                if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '关注公众号':
                    _.click()
                    _.clear()
                    for i in range(10):
                        _.send_keys(Keys.BACKSPACE)
                    _.send_keys('点击此处,继续阅读')
            big_elements = self.driver.find_elements_by_xpath('//span[@class="adui-button-content"]')
            for _ in big_elements:
                if _.text == '加粗' and _.is_displayed() and _.is_enabled():
                    _.click()
            input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
            for _ in input_elements:
                if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '07C160':
                    _.click()
                    _.send_keys('DE2821')
            size_buttons = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
            size_buttons_can_use = []
            for _ in size_buttons:
                if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '28':
                    size_buttons_can_use.append(_)
            size_buttons_can_use[0].click()
            for i in range(4):
                size_buttons_can_use[0].send_keys(Keys.BACKSPACE)
            size_buttons_can_use[0].send_keys('0')
            size_buttons_can_use[1].click()
            for i in range(4):
                size_buttons_can_use[1].send_keys(Keys.BACKSPACE)
            size_buttons_can_use[1].send_keys('0')

        if button_type == 3:
            input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
            for _ in input_elements:
                if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '关注公众号':
                    _.click()
                    _.clear()
                    for i in range(10):
                        _.send_keys(Keys.BACKSPACE)
                    _.send_keys('点我,继续阅读')
            big_elements = self.driver.find_elements_by_xpath('//span[@class="adui-button-content"]')
            for _ in big_elements:
                if _.text == '加粗' and _.is_displayed() and _.is_enabled():
                    _.click()
            input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
            for _ in input_elements:
                if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '07C160':
                    _.click()
                    _.send_keys('FFFFFF')
            input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
            for _ in input_elements:
                if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == 'FFFFFF':
                    _.click()
                    _.send_keys('DE2821')
                    break
            size_buttons = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
            size_buttons_can_use = []
            for _ in size_buttons:
                if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '28':
                    size_buttons_can_use.append(_)
            size_buttons_can_use[0].click()
            for i in range(4):
                size_buttons_can_use[0].send_keys(Keys.BACKSPACE)
            size_buttons_can_use[0].send_keys('0')
            size_buttons_can_use[1].click()
            for i in range(4):
                size_buttons_can_use[1].send_keys(Keys.BACKSPACE)
            size_buttons_can_use[1].send_keys('100')

    def set_follow_button_(self, config_info):
        # 设置关注按钮
        # 1.设置button文本内容 2.是否加粗 3.设置字体颜色,边框颜色,填充色 4.设置边距

        self.driver.find_element_by_xpath('//*[@id="stage-sidebar"]/section/div[4]/div[2]/div/div').click()
        # 文本设置
        input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
        for _ in input_elements:
            if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '关注公众号':
                _.click()
                _.clear()
                for i in range(10):
                    _.send_keys(Keys.BACKSPACE)
                _.send_keys(config_info['content'])

        # 文本是否加粗
        if config_info['is_bold']:
            big_elements = self.driver.find_elements_by_xpath('//span[@class="adui-button-content"]')
            for _ in big_elements:
                if _.text == '加粗' and _.is_displayed() and _.is_enabled():
                    _.click()

        # 颜色设置
        input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
        input_elements_can_use = []
        for _ in input_elements:
            if _.is_enabled() and _.is_displayed() and (
                    _.get_attribute("value") == '07C160' or _.get_attribute("value") == 'FFFFFF'):
                input_elements_can_use.append(_)

        input_elements_can_use[0].click()
        input_elements_can_use[0].send_keys(config_info['color1'])
        time.sleep(random.uniform(0.1, 0.5))
        input_elements_can_use[1].click()
        input_elements_can_use[1].send_keys(config_info['color2'])
        time.sleep(random.uniform(0.1, 0.5))
        input_elements_can_use[2].click()
        input_elements_can_use[2].send_keys(config_info['color3'])
        time.sleep(random.uniform(0.1, 0.5))

        # 间隙设置
        size_buttons = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
        size_buttons_can_use = []
        for _ in size_buttons:
            if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '28':
                size_buttons_can_use.append(_)
        size_buttons_can_use[0].click()
        for i in range(4):
            size_buttons_can_use[0].send_keys(Keys.BACKSPACE)
        size_buttons_can_use[0].send_keys(config_info['str_distance'][0])
        size_buttons_can_use[1].click()
        for i in range(4):
            size_buttons_can_use[1].send_keys(Keys.BACKSPACE)
        size_buttons_can_use[1].send_keys(config_info['str_distance'][1])

    def set_text_button(self):
        # 设置图文按钮
        pass

    def send_file_multi(self, layout, err_num=0):
        # 有问题,暂时不使用
        # 上传文件,文件上传与整体流程切割开
        token = re.findall('token=(\d+)', self.driver.current_url)
        url_ = 'https://mp.weixin.qq.com/promotion/frame?t=ad_system/common_frame&t1=material_std/material_library&token={}'.format(
            token[0])
        js = "window.open('{}')".format(url_)
        self.driver.execute_script(js)
        time.sleep(random.uniform(1, 2))
        self.driver.switch_to.window(self.driver.window_handles[-1])
        file_set = set()
        for v in layout.values():
            if isinstance(v, dict):
                if 'multi_page' in v.keys() or 'page' in v.keys() or 'movie' in v.keys():
                    v_name = list(v.keys())[0]
                    if isinstance(v[v_name], list):
                        for _ in v[v_name]:
                            file_set.add(_)
                    else:
                        file_set.add(v[v_name])
        file_list = list(file_set)
        # print(file_list)
        # print('file list size', len(file_list))
        send_file_sign = self.send_file_limit_num
        for _ in file_list:
            # print(_)
            self.driver.find_element_by_xpath('//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/input').send_keys(_)

        time.sleep(100)
        while True:
            # 失败,失败直接退出重跑
            # 正在上传数量,如果是正在上传小于限定数量,就不断输入
            # 上传完毕,小于限定数量,不断输入,.....已经上传好了,就关闭
            time.sleep(1)
            send_reslut_content = self.driver.find_element_by_xpath(
                '//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/div/strong').text
            if '失败' in send_reslut_content:
                if err_num < 3:
                    self.send_file(layout, err_num=err_num + 1)
                else:
                    exit()
            if '正在上传' in send_reslut_content:
                if send_file_sign < len(file_list):
                    send_num = re.findall('(\d+)', send_reslut_content)[0]
                    if int(send_num) < self.send_file_limit_num:
                        # 还可以继续上传
                        x = send_file_sign
                        can_send_sign = send_file_sign + (self.send_file_limit_num - send_num)
                        y = can_send_sign if can_send_sign < len(file_list) else len(file_list)
                        for i in range(x, y):
                            send_file_sign = send_file_sign + 1
                            self.driver.find_element_by_xpath(
                                '//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/input').send_keys(file_list[i])

                else:
                    pass
            if '上传成功' in send_reslut_content:
                if send_file_sign < len(file_list):
                    send_num = re.findall('(\d+)', send_reslut_content)[0]
                    if int(send_num) < self.send_file_limit_num:
                        # 还可以继续上传
                        x = send_file_sign
                        can_send_sign = send_file_sign + (self.send_file_limit_num - send_num)
                        y = can_send_sign if can_send_sign < len(file_list) else len(file_list)
                        for i in range(x, y):
                            send_file_sign = send_file_sign + 1
                            self.driver.find_element_by_xpath(
                                '//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/input').send_keys(file_list[i])
                else:
                    break

        time.sleep(1000)
        self.driver.execute_script('window.close();')

    def send_file(self, file_path, err_num=0):
        # 有问题,暂时不使用
        self.driver.execute_script('location.reload();')
        self.driver.find_element_by_xpath('//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/input').send_keys(file_path)

    def send_file_alone(self, layout):
        # 上传文件,单线程
        # 上传文件,文件上传与整体流程切割开
        token = re.findall('token=(\d+)', self.driver.current_url)
        url_ = 'https://mp.weixin.qq.com/promotion/frame?t=ad_system/common_frame&t1=material_std/material_library&token={}'.format(
            token[0])
        js = "window.open('{}')".format(url_)
        self.driver.execute_script(js)
        time.sleep(random.uniform(1, 2))
        self.driver.switch_to.window(self.driver.window_handles[-1])
        file_set = set()
        for v in layout.values():
            if isinstance(v, dict):
                if 'multi_page' in v.keys() or 'page' in v.keys() or 'movie' in v.keys():
                    v_name = list(v.keys())[0]
                    if isinstance(v[v_name], list):
                        for _ in v[v_name]:
                            file_set.add(_)
                    else:
                        file_set.add(v[v_name])
        for _ in file_set:
            self.send_file(_)
            err_num = 0
            while True:
                time.sleep(1)
                send_reslut_content = self.driver.find_element_by_xpath(
                    '//*[@id="wxadcontainer"]/div[1]/div[2]/div[4]/div/strong').text
                if '上传成功' in send_reslut_content:
                    break
                if '失败' in send_reslut_content:
                    err_num = err_num + 1
                    self.send_file(_)
                    if err_num > 3:
                        raise ValueError("图片上传失败")

        self.driver.execute_script('window.close();')
        self.driver.switch_to.window(self.driver.window_handles[-1])

    def set_share_content(self, share_info):
        title_content = share_info['share_info']['title']
        des_content = share_info['share_info']['des']
        self.driver.find_element_by_xpath('//*[@id="wxadcontainer"]/div[1]/div/section/header/div[2]/button[2]').click()
        WebDriverWait(self.driver, 50).until(lambda driver: driver.find_element_by_xpath(
            '//*[@id="wxadcontainer"]/div[1]/div/div/div/div[1]/section/section/div[2]/div[1]/div/div/input'))
        title_input = self.driver.find_element_by_xpath(
            '//*[@id="wxadcontainer"]/div[1]/div/div/div/div[1]/section/section/div[2]/div[1]/div/div/input')
        title_input.click()
        title_input.send_keys(title_content)
        des_input = self.driver.find_element_by_xpath(
            '//*[@id="wxadcontainer"]/div[1]/div/div/div/div[1]/section/section/div[2]/div[2]/div/div/input')
        des_input.click()
        des_input.send_keys(des_content)
        self.driver.find_element_by_xpath('//*[@id="wxadcontainer"]/div[1]/div/header/div[2]/button').click()
        WebDriverWait(self.driver, 10).until(
            lambda driver: driver.find_element_by_xpath('/html/body/div[6]/div/div/div[2]/div/div[3]/button[2]'))
        self.driver.find_element_by_xpath('/html/body/div[6]/div/div/div[2]/div/div[3]/button[2]').click()
        time.sleep(1)

    def create_layout(self, layout, err_num=0):
        #
        try:
            self.send_file_alone(layout)
            advertisement_sign = self.set_advertisement_sign()
            self.set_head_assemb(layout[-1])
            if -2 in layout.keys():
                self.set_background_color()

            for _ in range(max(layout.keys()) + 1):
                info = layout[_]
                info_key = list(info.keys())[0]
                info_v = info[info_key]
                if info_key == 'page':
                    self.set_page(info_v)
                if info_key == 'content':
                    self.set_content(info_v)
                if info_key == 'follow_button':
                    self.set_follow_button_(info_v)

            self.set_share_content(layout[-3])
            self.driver.execute_script('window.close();')
            # 切回到前一页
            self.driver.switch_to.window(self.driver.window_handles[-1])
            layout_info = {}
            layout_info['id'] = int(time.time() * 1000)
            layout_info['userid'] = self.user_id
            layout_info['result'] = 'sucess'
            layout_info['layout'] = json.dumps(layout, ensure_ascii=False)

            layout_insert = save_ad_layout_result(layout_info=layout_info, table_layout=self.layout_table)
            self.sql_session.execute(layout_insert)
            self.sql_session.commit()

            return {'sucess': True, 'result_info': advertisement_sign}
        except Exception as e:

            if err_num > 3:
                layout_num = int(time.time() * 1000)
                now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                try:
                    self.driver.save_screenshot('{layout_num}-{now_time}-{service_name}-{wechat_name}.png'.format(
                        layout_num=layout_num,
                        now_time=now_time,
                        service_name=self.service_name,
                        wechat_name=self.wechat_name))
                except:
                    pass
                layout_info = {}
                layout_info['id'] = layout_num
                layout_info['userid'] = self.user_id
                layout_info['result'] = str(e)
                layout_info['layout'] = json.dumps(layout)
                layout_insert = save_ad_layout_result(layout_info=layout_info, table_layout=self.layout_table)
                self.sql_session.execute(layout_insert)
                self.sql_session.commit()
                # 截图,传回错误信息
                return {'sucess': False, 'result_info': e}
            else:
                self.create_layout(layout, err_num=err_num + 1)


if __name__ == '__main__':
    logging.basicConfig(
        handlers=[
            logging.handlers.RotatingFileHandler('./create_ad.log',
                                                 maxBytes=10 * 1024 * 1024,
                                                 backupCount=5,
                                                 encoding='utf-8')
            , logging.StreamHandler()  # 供输出使用
        ],
        level=logging.INFO,
        format="%(asctime)s - %(levelname)s %(filename)s %(funcName)s %(lineno)s - %(message)s"
    )