Ver Fonte

MOD:落地页创建加速

cxyu há 3 anos atrás
pai
commit
ed1c644ac5
1 ficheiros alterados com 88 adições e 23 exclusões
  1. 88 23
      wechat_action/create_ad_layout.py

+ 88 - 23
wechat_action/create_ad_layout.py

@@ -35,7 +35,9 @@ class CreateAd:
                 WebDriverWait(self.driver, 10).until(
                     lambda driver: create_element.is_displayed() and create_element.is_enabled())
                 create_element.click()
+                break
             except Exception as e:
+                logging.info(str(e))
                 pass
             if i == 10:
                 raise ValueError('点击新建推广页 出错')
@@ -96,8 +98,9 @@ class CreateAd:
             file_name = re.split('\/', info['content']['url'])[-1]
             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)
+                lambda x: len([_ for _ in self.driver.find_elements_by_xpath(
+                    '//*[@class="base-1H7btNX4v9 primary-2diSJcTI_7 small-ZbOBQ_8kVy iconBtn-NYPRTZ5yJB webuploader-container"]')
+                               if _.is_displayed() and _.is_enabled()]) > 0)
 
             turn_page_buttons = self.driver.find_elements_by_class_name('paginationIcon-1EfoH0sNRF')
             can_use_button = []
@@ -107,7 +110,11 @@ class CreateAd:
             # 不断翻页获取到元素为止
             chose_over = False
             while True:
+                WebDriverWait(self.driver, 10).until(
+                    lambda driver: self.driver.find_elements_by_class_name('title-29sncpKgTl'))
+                #TODO:翻页后,有时没有加载出来
                 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:
@@ -287,13 +294,17 @@ class CreateAd:
             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)
+            lambda x: len([_ for _ in self.driver.find_elements_by_xpath(
+                '//*[@class="base-1H7btNX4v9 primary-2diSJcTI_7 small-ZbOBQ_8kVy iconBtn-NYPRTZ5yJB webuploader-container"]')
+                           if _.is_displayed() and _.is_enabled()]) > 0)
 
         # 不断翻页获取到元素为止
         chose_over = False
         logging.info('翻页开始')
         while True:
+            WebDriverWait(self.driver, 10).until(
+                lambda driver: self.driver.find_elements_by_class_name('title-29sncpKgTl'))
+            # TODO:翻页后,有时没有加载出来
             page_elements = self.driver.find_elements_by_class_name('title-29sncpKgTl')
             for _ in page_elements:
                 if _.is_displayed() and _.is_enabled():
@@ -353,7 +364,8 @@ class CreateAd:
         logging.info('是否截图结束')
         # 设置文本边距
         if config_info['marginTop'] != 0 or config_info['marginBottom'] != 0:
-            distance_buttons = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
+            distance_buttons = self.driver.find_elements_by_xpath(
+                '//div[@class="adui-numeric-input adui-input-wrapper adui-input-small adui-input-normal"]/input')
             distance_buttons_can_use = []
             for _ in distance_buttons:
                 if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '0':
@@ -404,7 +416,7 @@ class CreateAd:
         for _ in input_elements:
             if _.is_enabled() and _.is_displayed():
                 _.send_keys(Keys.CONTROL, 'v')
-
+        logging.info('文本内容设置完毕')
         # 设置颜色
         if config_info['color'] != '#595959':
             color_buttons = self.driver.find_elements_by_class_name('adui-cp-picker')
@@ -415,7 +427,8 @@ class CreateAd:
             c_buttons_can_use[1].click()
             time.sleep(random.uniform(0.2, 0.3))
 
-            input_elements = self.driver.find_elements_by_tag_name('input')
+            input_elements = self.driver.find_elements_by_xpath(
+                '//div[@class="adui-cp-input adui-input-wrapper adui-input-small adui-input-light adui-input-normal"]/input')
             for _ in input_elements:
                 if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '595959':
                     _.click()
@@ -423,7 +436,8 @@ class CreateAd:
 
         # 设置是否加粗
         if config_info['fontWeight'] != 'normal':
-            big_elements = self.driver.find_elements_by_class_name('adui-button-content')
+            big_elements = self.driver.find_elements_by_xpath(
+                '//button[@class="adui-button-base adui-button-normal adui-button-mini"]/span')
             for _ in big_elements:
                 if _.text == '加粗' and _.is_displayed() and _.is_enabled():
                     _.click()
@@ -431,15 +445,15 @@ class CreateAd:
         # 设置文本位置
         if config_info['textAlign'] != 'left':
             loc_buttons = self.driver.find_elements_by_xpath(
-                "//div[contains(@class, 'adui-button-group_banner')]//button")
+                '//button[@class="adui-button-base adui-button-normal adui-button-mini adui-button-hasLeftIcon adui-button-hasRightIcon"]')
             loc_buttons_can_use = []
             for _ in loc_buttons:
                 if _.is_enabled() and _.is_displayed():
                     loc_buttons_can_use.append(_)
             if config_info['textAlign'] == 'center':
-                loc_buttons_can_use[1].click()
+                loc_buttons_can_use[0].click()
             if config_info['textAlign'] == 'right':
-                loc_buttons_can_use[2].click()
+                loc_buttons_can_use[1].click()
 
         # 设置文本大小
         if config_info['fontSize'] != 15:
@@ -465,7 +479,8 @@ class CreateAd:
 
         # 设置文本边距
         if config_info['marginTop'] != 22 or config_info['marginBottom'] != 22:
-            distance_buttons = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
+            distance_buttons = self.driver.find_elements_by_xpath(
+                '//div[@class="adui-numeric-input adui-input-wrapper adui-input-small adui-input-normal"]/input')
             distance_buttons_can_use = []
             for _ in distance_buttons:
                 if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '22':
@@ -480,8 +495,7 @@ class CreateAd:
             distance_buttons_can_use[1].send_keys(config_info['marginBottom'])
 
     def set_follow_button(self, config_info):
-        # TODO:是否一键关注
-
+        logging.info('设置一键关注')
         # 设置关注按钮
         # 0.是否一键关注 1.设置button文本内容 2.是否加粗 3.设置字体颜色,边框颜色,填充色 4.设置边距
         self.driver.find_element_by_xpath('//*[@id="stage-sidebar"]/section/div[4]/div[2]/div/div').click()
@@ -498,7 +512,8 @@ class CreateAd:
                         _.click()
 
         # 文本设置
-        input_elements = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
+        input_elements = self.driver.find_elements_by_xpath(
+            '//div[@class="adui-input-wrapper adui-input-mini adui-input-limited adui-input-normal"]/input')
         for _ in input_elements:
             if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '关注公众号':
                 _.click()
@@ -510,13 +525,15 @@ class CreateAd:
 
         # 文本是否加粗
         if config_info['fontWeight'] != 'normal':
-            big_elements = self.driver.find_elements_by_xpath('//span[@class="adui-button-content"]')
+            big_elements = self.driver.find_elements_by_xpath(
+                '//button[@class="adui-button-base adui-button-normal adui-button-mini"]/span')
             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 = self.driver.find_elements_by_xpath(
+            '//div[@class="adui-cp-input adui-input-wrapper adui-input-mini adui-input-normal"]/input')
         input_elements_can_use = []
         for _ in input_elements:
             if _.is_enabled() and _.is_displayed() and (
@@ -536,7 +553,8 @@ class CreateAd:
         time.sleep(random.uniform(0.1, 0.5))
 
         # 间隙设置
-        size_buttons = self.driver.find_elements_by_xpath('//input[@class="adui-input-base"]')
+        size_buttons = self.driver.find_elements_by_xpath(
+            '//div[@class="adui-numeric-input adui-input-wrapper adui-input-small adui-input-normal"]/input')
         size_buttons_can_use = []
         for _ in size_buttons:
             if _.is_enabled() and _.is_displayed() and _.get_attribute("value") == '28':
@@ -549,6 +567,7 @@ class CreateAd:
         for i in range(4):
             size_buttons_can_use[1].send_keys(Keys.BACKSPACE)
         size_buttons_can_use[1].send_keys(config_info['marginBottom'])
+        logging.info('设置一键关注,结束')
 
     def set_text_button(self):
         # 设置图文按钮
@@ -647,9 +666,49 @@ class CreateAd:
                 self.send_file(file_link, err_num=err_num + 1)
 
     def send_file_alone(self, layout):
-        def check_file():
-            # TODO:检查是否需要上传
-            pass
+        def check_file(file_set, err_num=0):
+            try:
+                logging.info('start check file')
+                token = re.findall('token=(\d+)', self.driver.current_url)
+                token = str(token[0])
+                cookies = self.driver.get_cookies()
+                cookie_dict = {}
+                for _ in cookies:
+                    cookie_dict[_['name']] = _['value']
+
+                wechat_url = 'https://mp.weixin.qq.com/promotion/material/material_library'
+                headers = {
+                    'content-type': 'application/x-www-form-urlencoded',
+                    'referer': 'https://mp.weixin.qq.com/promotion/frame?t=ad_system/common_simple_frame&t1=rdcanvas/canvas_edit_v2&token={}'.format(
+                        token),
+                    'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36'}
+                data = 'action=get_material_data&args={"condition_list":[{"min_width":0,"min_height":0}],"type":1,"page":1,"page_size":8}&token=' + token + '&appid=&spid=&_=' + str(
+                    int(time.time() * 1000))
+                rsp = requests.post(wechat_url, data=data, headers=headers, cookies=cookie_dict, timeout=1)
+                total_page = rsp.json()['data']['page_info']['total_page']
+                import copy
+                tmp_file_set = copy.deepcopy(file_set)
+                for file_url in tmp_file_set:
+                    link_pra = re.split('\/', file_url)[-1]
+                    for page_info in rsp.json()['data']['list']:
+                        if link_pra == page_info['name']:
+                            if file_url in file_set:
+                                file_set.remove(file_url)
+
+                for i in range(2, total_page + 1):
+                    data = 'action=get_material_data&args={"condition_list":[{"min_width":0,"min_height":0}],"type":1,"page":' + str(
+                        i) + ',"page_size":8}&token=' + token + '&appid=&spid=&_=' + str(
+                        int(time.time() * 1000))
+                    rsp = requests.post(wechat_url, data=data, headers=headers, cookies=cookie_dict, timeout=1)
+                    tmp_file_set = copy.deepcopy(file_set)
+                    for file_url in tmp_file_set:
+                        link_pra = re.split('\/', file_url)[-1]
+                        for page_info in rsp.json()['data']['list']:
+                            if link_pra == page_info['name']:
+                                file_set.remove(file_url)
+            except Exception as e:
+                if err_num < 10:
+                    check_file(file_set, err_num + 1)
 
         def get_url(v_info):
             if 'url' in v_info.keys():
@@ -665,6 +724,13 @@ class CreateAd:
         # 上传文件,单线程
         # 上传文件,文件上传与整体流程切割开
         logging.info('开始传送文件')
+        file_set = set()
+        get_url(layout)
+        check_file(file_set)
+        if not file_set:
+            logging.info('文件都已传送,无需重新上传')
+            return
+
         WebDriverWait(self.driver, 10).until(
             lambda x: len(re.findall('token=(\d+)', self.driver.current_url)))
         token = re.findall('token=(\d+)', self.driver.current_url)
@@ -674,7 +740,6 @@ class CreateAd:
         self.driver.execute_script(js)
         time.sleep(random.uniform(1, 2))
         self.driver.switch_to.window(self.driver.window_handles[-1])
-        file_set = set()
         # TODO:之后还有multi_page 和 movie
         # for v in layout.values():
         #     if isinstance(v, dict):
@@ -686,7 +751,6 @@ class CreateAd:
         #             else:
         #                 file_set.add(v[v_name])
 
-        get_url(layout)
         for _ in file_set:
             self.send_file(_)
             err_num = 0
@@ -783,6 +847,7 @@ class CreateAd:
                     self.get_into_create_page()
                     return self.create_layout(layout, sql_session, err_num=err_num + 1)
         except Exception as e:
+            # raise
             logging.error(e)
             self.log_ad.driver.save_screenshot(
                 'user_id:{}_time_{}_layout_name:{}_layout_error.png'.format(self.log_ad.user_id,