|  | @@ -3,7 +3,9 @@ from selenium.webdriver.common.keys import Keys
 | 
	
		
			
				|  |  |  from selenium.webdriver import ActionChains
 | 
	
		
			
				|  |  |  from logging import handlers
 | 
	
		
			
				|  |  |  import logging
 | 
	
		
			
				|  |  | +import requests
 | 
	
		
			
				|  |  |  import random
 | 
	
		
			
				|  |  | +import shutil
 | 
	
		
			
				|  |  |  import time
 | 
	
		
			
				|  |  |  import re
 | 
	
		
			
				|  |  |  import os
 | 
	
	
		
			
				|  | @@ -17,6 +19,7 @@ class CreateAd:
 | 
	
		
			
				|  |  |          self.driver = login_ad.get_driver_loged()
 | 
	
		
			
				|  |  |          self.get_into_create_page()
 | 
	
		
			
				|  |  |          self.send_file_limit_num = 8
 | 
	
		
			
				|  |  | +        self.img_dir = './img'
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      def get_into_create_page(self):
 | 
	
		
			
				|  |  |          # 进入创建页面
 | 
	
	
		
			
				|  | @@ -25,9 +28,9 @@ class CreateAd:
 | 
	
		
			
				|  |  |          time.sleep(random.uniform(5, 7))
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          WebDriverWait(self.driver, 10).until(lambda driver: driver.find_element_by_class_name('ui-fl-r'))
 | 
	
		
			
				|  |  | -        create_element=self.driver.find_element_by_class_name('ui-fl-r')
 | 
	
		
			
				|  |  | -        WebDriverWait(self.driver, 10).until(lambda driver: create_element.is_displayed() and create_element.is_enabled())
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +        create_element = self.driver.find_element_by_class_name('ui-fl-r')
 | 
	
		
			
				|  |  | +        WebDriverWait(self.driver, 10).until(
 | 
	
		
			
				|  |  | +            lambda driver: create_element.is_displayed() and create_element.is_enabled())
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          self.driver.find_element_by_class_name('ui-fl-r').click()
 | 
	
		
			
				|  |  |          WebDriverWait(self.driver, 5).until(lambda driver: driver.find_element_by_css_selector(
 | 
	
	
		
			
				|  | @@ -62,11 +65,10 @@ class CreateAd:
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      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)
 | 
	
		
			
				|  |  | +            file_name = re.split('\/', info_v)[-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
 | 
	
	
		
			
				|  | @@ -128,6 +130,7 @@ class CreateAd:
 | 
	
		
			
				|  |  |              logging.info('头版首页单图上传结束')
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          def multi_page_set():
 | 
	
		
			
				|  |  | +            # TODO:现在图片设置为素材库,需要对应下载然后存储
 | 
	
		
			
				|  |  |              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))
 | 
	
	
		
			
				|  | @@ -185,6 +188,7 @@ class CreateAd:
 | 
	
		
			
				|  |  |              logging.info('头版多图选择 结束')
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          def movie_set():
 | 
	
		
			
				|  |  | +            # TODO:视频转为链接,
 | 
	
		
			
				|  |  |              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()
 | 
	
	
		
			
				|  | @@ -244,12 +248,13 @@ class CreateAd:
 | 
	
		
			
				|  |  |          # 设置头部组件
 | 
	
		
			
				|  |  |          pass
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    def set_page(self, page_path):
 | 
	
		
			
				|  |  | +    def set_page(self, page_link):
 | 
	
		
			
				|  |  |          # 设置图片模块
 | 
	
		
			
				|  |  |          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)
 | 
	
		
			
				|  |  | +        file_name = re.split('\/', page_link)[-1]
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          upload_elements = self.driver.find_elements_by_class_name('upload-img-item-inner-2gsg7NjaZ8')
 | 
	
		
			
				|  |  |          for _ in upload_elements:
 | 
	
		
			
				|  |  |              if _.is_displayed() and _.is_enabled():
 | 
	
	
		
			
				|  | @@ -319,6 +324,8 @@ class CreateAd:
 | 
	
		
			
				|  |  |          logging.info('图片模块设置结束')
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      def set_content(self, config_info):
 | 
	
		
			
				|  |  | +        # TODO:长文本需要优化输入时间
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          # 设置文本模块
 | 
	
		
			
				|  |  |          # 1.文本内容2.文本颜色3.是否加粗4.文本位置设置5.文本大小
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -626,7 +633,17 @@ class CreateAd:
 | 
	
		
			
				|  |  |          time.sleep(1000)
 | 
	
		
			
				|  |  |          self.driver.execute_script('window.close();')
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    def send_file(self, file_path, err_num=0):
 | 
	
		
			
				|  |  | +    def send_file(self, file_link, err_num=0):
 | 
	
		
			
				|  |  | +        # 下载图片,存储于
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if not os.path.exists(self.img_dir):
 | 
	
		
			
				|  |  | +            os.makedirs(self.img_dir)
 | 
	
		
			
				|  |  | +        link_pra = re.split('\/', file_link)
 | 
	
		
			
				|  |  | +        file_path = os.getcwd() + '/' + self.img_dir + '/' + link_pra[-1]
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        rsp = requests.get(file_link)
 | 
	
		
			
				|  |  | +        with open(file_path, 'wb') as f:
 | 
	
		
			
				|  |  | +            f.write(rsp.content)
 | 
	
		
			
				|  |  |          # 有问题,暂时不使用
 | 
	
		
			
				|  |  |          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)
 | 
	
	
		
			
				|  | @@ -634,6 +651,8 @@ class CreateAd:
 | 
	
		
			
				|  |  |      def send_file_alone(self, layout):
 | 
	
		
			
				|  |  |          # 上传文件,单线程
 | 
	
		
			
				|  |  |          # 上传文件,文件上传与整体流程切割开
 | 
	
		
			
				|  |  | +        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)
 | 
	
		
			
				|  |  |          url_ = 'https://mp.weixin.qq.com/promotion/frame?t=ad_system/common_frame&t1=material_std/material_library&token={}'.format(
 | 
	
		
			
				|  |  |              token[0])
 | 
	
	
		
			
				|  | @@ -689,6 +708,29 @@ class CreateAd:
 | 
	
		
			
				|  |  |          self.driver.find_element_by_xpath('/html/body/div[6]/div/div/div[2]/div/div[3]/button[2]').click()
 | 
	
		
			
				|  |  |          time.sleep(1)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    def check_sucess(self, layout_name):
 | 
	
		
			
				|  |  | +        # 进入素材中心
 | 
	
		
			
				|  |  | +        WebDriverWait(self.driver, 100).until(
 | 
	
		
			
				|  |  | +            lambda x: self.driver.find_element_by_id('material'))
 | 
	
		
			
				|  |  | +        self.driver.find_element_by_id('material').click()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        # 输入--落地页名字
 | 
	
		
			
				|  |  | +        input_element = self.driver.find_element_by_xpath('//*[@class="adui-input-base"]')
 | 
	
		
			
				|  |  | +        input_element.send_keys(layout_name)
 | 
	
		
			
				|  |  | +        time.sleep(random.uniform(0.5, 0.6))
 | 
	
		
			
				|  |  | +        input_element.send_keys(Keys.RETURN)
 | 
	
		
			
				|  |  | +        # TODO:有空去除掉时间
 | 
	
		
			
				|  |  | +        time.sleep(random.uniform(3, 5))
 | 
	
		
			
				|  |  | +        # 查询有无
 | 
	
		
			
				|  |  | +        result = self.driver.find_elements_by_xpath('//*[@class="namelink-1V-GPp_906"]')
 | 
	
		
			
				|  |  | +        print('结果', len(result))
 | 
	
		
			
				|  |  | +        if len(result):
 | 
	
		
			
				|  |  | +            return True
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    def remove_all_file(self):
 | 
	
		
			
				|  |  | +        shutil.rmtree(os.getcwd() + '/' + self.img_dir)
 | 
	
		
			
				|  |  | +        pass
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      def create_layout(self, layout, err_num=0):
 | 
	
		
			
				|  |  |          #
 | 
	
		
			
				|  |  |          try:
 | 
	
	
		
			
				|  | @@ -710,23 +752,30 @@ class CreateAd:
 | 
	
		
			
				|  |  |                      self.set_follow_button_(info_v)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              self.set_share_content(layout[str(-3)])
 | 
	
		
			
				|  |  | -            self.driver.execute_script('window.close();')
 | 
	
		
			
				|  |  | -            # 切回到前一页
 | 
	
		
			
				|  |  | -            self.driver.switch_to.window(self.driver.window_handles[-1])
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            return {'sucess': True, 'result_info': ''}
 | 
	
		
			
				|  |  | +            self.remove_all_file()
 | 
	
		
			
				|  |  | +            if self.check_sucess(layout_name=layout[-4]['layout_name']):
 | 
	
		
			
				|  |  | +                return {'sucess': True, 'result_info': ''}
 | 
	
		
			
				|  |  | +            else:
 | 
	
		
			
				|  |  | +                if err_num > 3:
 | 
	
		
			
				|  |  | +                    return {'sucess': False, 'result_info': '过程中全程无错误,失败'}
 | 
	
		
			
				|  |  | +                else:
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    self.log_ad.refresh_driver()
 | 
	
		
			
				|  |  | +                    self.log_ad.select_ad_master(service_name=self.service_name, wechat_name=self.wechat_name)
 | 
	
		
			
				|  |  | +                    self.get_into_create_page()
 | 
	
		
			
				|  |  | +                    return self.create_layout(layout, err_num=err_num + 1)
 | 
	
		
			
				|  |  |          except Exception as e:
 | 
	
		
			
				|  |  |              raise
 | 
	
		
			
				|  |  |              # TODO:有空时讲 e 内容设置为原始内容
 | 
	
		
			
				|  |  |              if err_num > 3:
 | 
	
		
			
				|  |  |                  return {'sucess': False, 'result_info': str(e)}
 | 
	
		
			
				|  |  |              else:
 | 
	
		
			
				|  |  | +                self.log_ad.refresh_driver()
 | 
	
		
			
				|  |  | +                self.log_ad.select_ad_master(service_name=self.service_name, wechat_name=self.wechat_name)
 | 
	
		
			
				|  |  | +                self.get_into_create_page()
 | 
	
		
			
				|  |  |                  return self.create_layout(layout, err_num=err_num + 1)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    def check_sucess(self):
 | 
	
		
			
				|  |  | -        # TODO: 添加一个页面检查,查看落地页是否已经在微信后台创建
 | 
	
		
			
				|  |  | -        pass
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  if __name__ == '__main__':
 | 
	
		
			
				|  |  |      logging.basicConfig(
 |