Procházet zdrojové kódy

MOD:加速--落地页创建速度

cxyu před 3 roky
rodič
revize
b3940310d9

+ 1 - 1
web_module/tornado_api.py

@@ -72,7 +72,6 @@ class create_ad_plan_local(BaseHandler):
 class create_ad_plan(BaseHandler):
     # TODO:只要tornado开着就不允许修改数据库,------想好之后上线如何操作
 
-
     @staticmethod
     def check_task(user_id):
         sql_session = db.DBSession()
@@ -137,6 +136,7 @@ class create_ad_plan(BaseHandler):
                 log_ad.driver.quit()
 
             logging.error(str(e))
+            # TODO:这部分需要处理,,之后换成flask
             self.write('error')
         finally:
             sql_session.commit()

+ 4 - 0
web_module/user_action.py

@@ -57,6 +57,7 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse, task_name):
                                                 action_record_table,
                                                 service_name, wechat_name, task_name)
                 except Exception as e:
+                    # raise
                     res = {'sucess': False, 'result_info': str(e)}
                     logging.error('layout 创建失败,{}'.format(str(e)))
                     # 创建落地页,计划都失败
@@ -85,6 +86,7 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse, task_name):
                                         service_name, wechat_name, task_name)
                 logging.info('创建计划任务结束')
             except Exception as e:
+                # raise
                 log_ad.driver.save_screenshot(
                     'user_id:{}_time_{}_plan_name:{}_plan_error.png'.format(user_id, datetime.now().strftime(
                         "%Y-%m-%d, %H:%M:%S"), plan_name))
@@ -98,10 +100,12 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse, task_name):
 
 
     except Exception as e:
+        # raise
         sql_tools.update_task_status_error(sql_session=sql_session, user_id=user_id, task_name=task_name)
         dingtalk.send_message('user_id:{} 计划执行出错,进行检查\n{}'.format(user_id, str(e)))
         logging.error(e)
     finally:
+        # raise
         logging.info('创建计划,任务结束')
         # 每次运行微信相关操作,对微信相关信息进行刷新
         sql_session.commit()

+ 43 - 35
wechat_action/create_ad_layout.py

@@ -24,24 +24,21 @@ class CreateAd:
 
     def get_into_create_page(self):
         # 进入创建页面
+        logging.info('开始进入创建页面')
         self.driver.find_element_by_id('material').click()
-        try:
-            # 落地页内容有的账户有,有的无,无的默认等5s
-            WebDriverWait(self.driver, 5).until(lambda driver: len(self.driver.find_elements_by_xpath(
-                '//*[@class="adui-table-cell"]')))
-            for i in self.driver.find_elements_by_xpath(
-                    '//*[@class="adui-table-cell"]'):
-                WebDriverWait(self.driver, 5).until(lambda driver: i.is_displayed() and i.is_enabled())
-        except Exception as e:
-            logging.error(str(e))
-            time.sleep(5)
-        WebDriverWait(self.driver, 10).until(lambda driver: self.driver.find_element_by_xpath(
-            '//*[@class="ui-fl-r adui-button-base adui-button-primary adui-button-small"]'))
-        create_element = self.driver.find_element_by_xpath(
-            '//*[@class="ui-fl-r adui-button-base adui-button-primary adui-button-small"]')
-        WebDriverWait(self.driver, 10).until(
-            lambda driver: create_element.is_displayed() and create_element.is_enabled())
-        create_element.click()
+        for i in range(10):
+            try:
+                WebDriverWait(self.driver, 10).until(lambda driver: self.driver.find_element_by_xpath(
+                    '//*[@class="ui-fl-r adui-button-base adui-button-primary adui-button-small"]'))
+                create_element = self.driver.find_element_by_xpath(
+                    '//*[@class="ui-fl-r adui-button-base adui-button-primary adui-button-small"]')
+                WebDriverWait(self.driver, 10).until(
+                    lambda driver: create_element.is_displayed() and create_element.is_enabled())
+                create_element.click()
+            except Exception as e:
+                pass
+            if i == 10:
+                raise ValueError('点击新建推广页 出错')
         logging.info('点击新建推广页 结束')
         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'))
@@ -505,9 +502,10 @@ class CreateAd:
         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)
+                # for i in range(10):
+                #     _.send_keys(Keys.BACKSPACE)
+                _.send_keys(Keys.CONTROL + 'a')
+                _.send_keys(Keys.BACKSPACE)
                 _.send_keys(config_info['text'])
 
         # 文本是否加粗
@@ -632,19 +630,27 @@ class CreateAd:
 
     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)
+        try:
+            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, timeout=0.5)
+            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)
+        except:
+            if err_num < 20:
+                self.send_file(file_link, err_num=err_num + 1)
 
     def send_file_alone(self, layout):
+        def check_file():
+            # TODO:检查是否需要上传
+            pass
+
         def get_url(v_info):
             if 'url' in v_info.keys():
                 file_set.add(v_info['url'])
@@ -736,14 +742,16 @@ class CreateAd:
             if len(result_json['landing_page_list']):
                 logging.info('对应落地页存在于微信后台')
                 return True
+        else:
+            logging.info('对应落地页不存在于微信后台')
 
     def remove_all_file(self):
-        shutil.rmtree(os.getcwd() + '/' + self.img_dir)
-        pass
+        try:
+            shutil.rmtree(os.getcwd() + '/' + self.img_dir)
+        except Exception as e:
+            logging.info(str(e))
 
     def create_layout(self, layout, sql_session, err_num=0):
-        #
-
         try:
             self.send_file_alone(layout)
             self.set_advertisement_sign(layout_name=layout['layoutName'])

+ 5 - 2
wechat_action/login_ad.py

@@ -40,8 +40,9 @@ class LogIn:
         options.add_experimental_option("prefs", prefs)
 
         driver = webdriver.Remote(
-            # command_executor='http://192.168.20.105/wd/hub',
-            command_executor='http://118.31.53.105:4555/wd/hub',
+            # command_executor='http://192.168.0.103/wd/hub',
+            # command_executor='http://118.31.53.105:4555/wd/hub',
+            command_executor='http://47.96.116.151/wd/hub',
             options=options)
         # driver = webdriver.Chrome(options=options)
         driver.maximize_window()
@@ -93,6 +94,7 @@ class LogIn:
             sql_session.execute(wechat_insert)
             sql_session.commit()
         log_ad.driver.switch_to.window(log_ad.driver.window_handles[-1])
+        logging.info('update db cookies over')
 
     def select_ad_master(self, service_name, wechat_name, sql_session):
         logging.info('开始切换服务商')
@@ -105,6 +107,7 @@ class LogIn:
                         ''')
             self.driver.find_element_by_css_selector(
                 '#root > div > header > div > div.CoreLayout__account-2lIr0 > div').click()
+            time.sleep(0.1)
             self.driver.find_element_by_css_selector(
                 '#root > div > div.CoreLayout__headerDropdown-3xWkD > div > div:nth-child(1) > button').click()
             service_names = self.driver.find_elements_by_class_name('CoreLayout__headerDropdownItem-X4S98')

+ 4 - 3
wechat_action/sql_tools.py

@@ -260,14 +260,15 @@ def get_task_in_hand_limit_one(user_id, sql_session):
     on ar.object_name = ad.name 
     where action_type in ('create_ad_plan' ,'refresh_wechat_info')
     and status ='todo'
-    and timestampdiff(minute ,ad.update_time ,current_timestamp )<60
-    and ad.user_id ='{}') as foo
+    and timestampdiff(minute ,ar.update_time ,current_timestamp )<60
+    and ad.user_id ='{user_id}') as foo
     join
     (select task_name from action_record ar 
     where status ='todo' and action_type in ('create_ad_plan' ,'refresh_wechat_info')
+    and user_id = '{user_id}'
     order by create_time 
     limit 1) as foo2 on foo.task_name=foo2.task_name
-    '''.format(user_id)
+    '''.format(user_id=user_id)
     cursor = sql_session.execute(sql)
     lines = cursor.fetchall()
     return lines