Переглянути джерело

MOD:sql_session 收归到user_action与tornado_api

cxyu 3 роки тому
батько
коміт
7b6f047e88

+ 34 - 33
web_module/tornado_api.py

@@ -246,7 +246,6 @@ class get_scan_status(BaseHandler):
 
 # TODO:wechat_info,human_info 这两张表有空时需要进行对应改进
 class ad_human_info(BaseHandler):
-    # TODO:设置一下update---table,如果失败了sql_session需要关闭
     @staticmethod
     def refresh_wechat_cookies(tornado_web, user_id):
         # 1.返回二维码链接
@@ -299,27 +298,29 @@ class ad_human_info(BaseHandler):
             # TODO:一个涉及到selenium-driver的请求-生命周期.----看一下tornado是怎么处理请求的生命周期
             if int(is_refresh) == 1:
                 log_ad, cookie_canuse = self.refresh_wechat_cookies(self, user_id=user_id)
-                task_name = 'user_id: {user_id}  time:{time_sign} action:refresh_wechat_info'.format(
-                    user_id=user_id,
-                    time_sign=datetime.now().strftime(
-                        "%Y-%m-%d, %H:%M:%S"))
-                # 行为记录
-                action_type = refresh_wechat_action
-                object_name = ''
-                service_name = ''
-                wechat_name = ''
-                action_info = {'user_id': user_id, 'service_name': service_name, 'wechat_name': wechat_name,
-                               'action_type': action_type, 'object_name': object_name, 'task_name': task_name,
-                               'status': 'todo'}
-                record_insert = sql_tools.save_action_record(action_record_info=action_info,
-                                                             table_action_record=action_record_table)
-                sql_session.execute(record_insert)
-                sql_session.commit()
+
                 if not create_ad_plan.check_task(user_id=user_id):
+                    task_name = 'user_id: {user_id}  time:{time_sign} action:refresh_wechat_info'.format(
+                        user_id=user_id,
+                        time_sign=datetime.now().strftime(
+                            "%Y-%m-%d, %H:%M:%S"))
+                    # 行为记录
+                    action_type = refresh_wechat_action
+                    object_name = ''
+                    service_name = ''
+                    wechat_name = ''
+                    action_info = {'user_id': user_id, 'service_name': service_name, 'wechat_name': wechat_name,
+                                   'action_type': action_type, 'object_name': object_name, 'task_name': task_name,
+                                   'status': 'todo'}
+                    record_insert = sql_tools.save_action_record(action_record_info=action_info,
+                                                                 table_action_record=action_record_table)
+                    sql_session.execute(record_insert)
+                    sql_session.commit()
                     threading.Thread(target=user_action.get_human_info,
                                      args=(
                                          user_id, log_ad, db, cookie_canuse, task_name)).start()
                 else:
+                    logging.info('任务有堆积')
                     return
                     self.write({'status': {'msg': '任务有堆积', "RetCode": 200}})
             else:
@@ -438,23 +439,23 @@ class ad_wechat_info(BaseHandler):
             if int(is_refresh) == 1:
                 # 检查有无其他任务在处理中,有则等待
                 log_ad, cookie_canuse = ad_human_info.refresh_wechat_cookies(self, user_id=user_id)
-                task_name = 'user_id: {user_id}  time:{time_sign} action:refresh_wechat_info'.format(
-                    user_id=user_id,
-                    time_sign=datetime.now().strftime(
-                        "%Y-%m-%d, %H:%M:%S"))
-                # 行为记录
-                action_type = refresh_wechat_action
-                object_name = ''
-                service_name = ''
-                wechat_name = ''
-                action_info = {'user_id': user_id, 'service_name': service_name, 'wechat_name': wechat_name,
-                               'action_type': action_type, 'object_name': object_name, 'task_name': task_name,
-                               'status': 'todo'}
-                record_insert = sql_tools.save_action_record(action_record_info=action_info,
-                                                             table_action_record=action_record_table)
-                sql_session.execute(record_insert)
-                sql_session.commit()
                 if not create_ad_plan.check_task(user_id=user_id):
+                    task_name = 'user_id: {user_id}  time:{time_sign} action:refresh_wechat_info'.format(
+                        user_id=user_id,
+                        time_sign=datetime.now().strftime(
+                            "%Y-%m-%d, %H:%M:%S"))
+                    # 行为记录
+                    action_type = refresh_wechat_action
+                    object_name = ''
+                    service_name = ''
+                    wechat_name = ''
+                    action_info = {'user_id': user_id, 'service_name': service_name, 'wechat_name': wechat_name,
+                                   'action_type': action_type, 'object_name': object_name, 'task_name': task_name,
+                                   'status': 'todo'}
+                    record_insert = sql_tools.save_action_record(action_record_info=action_info,
+                                                                 table_action_record=action_record_table)
+                    sql_session.execute(record_insert)
+                    sql_session.commit()
                     threading.Thread(target=user_action.get_human_info,
                                      args=(
                                          user_id, log_ad, db, cookie_canuse, task_name)).start()

+ 7 - 7
web_module/user_action.py

@@ -28,14 +28,14 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse, task_name):
 
         # cookies保存
         if not cookie_canuse:
-            log_ad.cookies_save(log_ad)
+            log_ad.cookies_save(log_ad,sql_session)
         for _ in ad_plan_list:
             service_name = _['service_name']
             wechat_name = _['wechat_name']
             plan_name = _['title']
             try:
                 # 1.检查1.落地页是否创建过了
-                log_ad.select_ad_master(service_name, wechat_name)
+                log_ad.select_ad_master(service_name, wechat_name,sql_session)
                 # 现在默认layout_name在30个字符以内
                 layout_name = _['idea']['jump_type_page_type']['layout_name']
                 if CreateAd.check_sucess_api(layout_name=layout_name, log_ad=log_ad):
@@ -51,7 +51,7 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse, task_name):
                         layout_typesetting_dict = sql_tools.get_layout_typesetting(sql_session, user_id,
                                                                                    typesetting_name=layout_name)
                         layout_typesetting_dict = json.loads(layout_typesetting_dict)
-                        res = create_ad_layout.create_layout(layout_typesetting_dict)
+                        res = create_ad_layout.create_layout(layout_typesetting_dict,sql_session)
                         sql_tools.action_record(res, sql_session, layout_create_action, user_id, layout_name,
                                                 action_record_table,
                                                 service_name, wechat_name, task_name)
@@ -71,12 +71,12 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse, task_name):
 
                 log_ad.refresh_driver()
                 # 3.创建计划
-                log_ad.select_ad_master(service_name, wechat_name)
+                log_ad.select_ad_master(service_name, wechat_name,sql_session)
                 # plan_typesetting_dict = sql_tools.get_ad_plan_typesetting(sql_session=sql_session, user_id=user_id,
                 #                                                           typesetting_name=plan_name)
                 create_ad_plan = CreateAdPlan(login_ad=log_ad, task=_, service_name=service_name,
                                               wechat_name=wechat_name)
-                res = create_ad_plan.run()
+                res = create_ad_plan.run(sql_session)
                 # 4.更新action_record相关计划信息
                 sql_tools.action_record(res, sql_session, ad_plan_create_action, user_id, plan_name,
                                         action_record_table,
@@ -126,7 +126,7 @@ def get_human_info(user_id, log_ad, db, cookie_canuse, task_name):
     try:
         # 1.cookies保存
         if not cookie_canuse:
-            log_ad.cookies_save(log_ad)
+            log_ad.cookies_save(log_ad,sql_session)
 
         # 动作存放
         object_name = ''
@@ -142,7 +142,7 @@ def get_human_info(user_id, log_ad, db, cookie_canuse, task_name):
         # wechat_info.每次都删除掉前面全部数据,进行更新
         # human_info 进行全局更新
         w_api = WechatApi(log_ad=log_ad)
-        res_info = w_api.get_human_info()
+        res_info = w_api.get_human_info(sql_session)
 
         if not res_info['sucess']:
             dingtalk.send_message('user_id:{} 获取微信数据为空,进行检查'.format(user_id))

+ 7 - 5
wechat_action/create_ad_layout.py

@@ -709,7 +709,7 @@ class CreateAd:
         shutil.rmtree(os.getcwd() + '/' + self.img_dir)
         pass
 
-    def create_layout(self, layout, err_num=0):
+    def create_layout(self, layout, sql_session, err_num=0):
         #
 
         try:
@@ -737,9 +737,10 @@ class CreateAd:
                 else:
 
                     self.log_ad.refresh_driver()
-                    self.log_ad.select_ad_master(service_name=self.service_name, wechat_name=self.wechat_name)
+                    self.log_ad.select_ad_master(service_name=self.service_name, wechat_name=self.wechat_name,
+                                                 sql_session=sql_session)
                     self.get_into_create_page()
-                    return self.create_layout(layout, err_num=err_num + 1)
+                    return self.create_layout(layout, sql_session, err_num=err_num + 1)
         except Exception as e:
             raise
             logging.error(e)
@@ -748,9 +749,10 @@ class CreateAd:
                 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.log_ad.select_ad_master(service_name=self.service_name, wechat_name=self.wechat_name,
+                                             sql_session=sql_session)
                 self.get_into_create_page()
-                return self.create_layout(layout, err_num=err_num + 1)
+                return self.create_layout(layout, sql_session, err_num=err_num + 1)
 
 
 if __name__ == '__main__':

+ 7 - 5
wechat_action/create_ad_plan.py

@@ -337,7 +337,7 @@ class CreateAdPlan():
             if len(rsp.json()['list']):
                 return True
 
-    def run(self, err_num=0):
+    def run(self, sql_session, err_num=0):
         try:
             # self.ad_human_action.check_human_info()
             self.set_ad_locations()
@@ -364,8 +364,9 @@ class CreateAdPlan():
                 else:
 
                     self.login_ad.refresh_driver()
-                    self.login_ad.select_ad_master(service_name=self, wechat_name=self.wechat_name)
-                    return self.run(err_num=err_num + 1)
+                    self.login_ad.select_ad_master(service_name=self, wechat_name=self.wechat_name,
+                                                   sql_session=sql_session)
+                    return self.run(sql_session, err_num=err_num + 1)
         except Exception as e:
             raise
             time.sleep(5)
@@ -377,5 +378,6 @@ class CreateAdPlan():
                 return {'sucess': False, 'result_info': str(e)}
             else:
                 self.login_ad.refresh_driver()
-                self.login_ad.select_ad_master(service_name=self.service_name, wechat_name=self.wechat_name)
-                return self.run(err_num=err_num + 1)
+                self.login_ad.select_ad_master(service_name=self.service_name, wechat_name=self.wechat_name,
+                                               sql_session=sql_session)
+                return self.run(sql_sesion, err_num=err_num + 1)

+ 3 - 4
wechat_action/login_ad.py

@@ -66,7 +66,7 @@ class LogIn:
         logging.info('登录成功')
 
     @staticmethod
-    def cookies_save(log_ad):
+    def cookies_save(log_ad, sql_session):
         logging.info('update db cookie')
         # 切换窗口,点击创建广告,切到广告页面
         log_ad.driver.switch_to.window(log_ad.driver.window_handles[0])
@@ -74,7 +74,6 @@ class LogIn:
             'ui-mr-medium'))
         wechat_cookies, wechat_id_tmp = log_ad.wechat_cookie_pickle()
 
-        sql_session = log_ad.db.DBSession()
         wechat_id = sql_tools.get_wechat_id_from_cookies(log_ad.user_id, sql_session)
         if wechat_id:
             if wechat_id_tmp != wechat_id:
@@ -95,7 +94,7 @@ class LogIn:
             sql_session.commit()
         log_ad.driver.switch_to.window(log_ad.driver.window_handles[-1])
 
-    def select_ad_master(self, service_name, wechat_name):
+    def select_ad_master(self, service_name, wechat_name, sql_session):
         logging.info('开始切换服务商')
         time.sleep(5)
         self.driver.execute_script('''
@@ -127,7 +126,7 @@ class LogIn:
         elements[0].click()
         time.sleep(1)
         logging.info('切换服务商成功')
-        self.cookies_save(self)
+        self.cookies_save(self, sql_session)
 
     @staticmethod
     def get_cookie(driver, login_cookie=True):

+ 3 - 12
wechat_api/get_wechat_info.py

@@ -1,14 +1,5 @@
-from selenium import webdriver
 from selenium.webdriver import ActionChains
-from selenium.webdriver.common.by import By
-from selenium.webdriver.support import expected_conditions as EC
 from selenium.webdriver.support.wait import WebDriverWait
-from wechat_action.create_ad_plan_idea import IdeaAction
-from wechat_action.human_ad import HumanAd
-from selenium.webdriver import ChromeOptions
-from selenium.webdriver.common.keys import Keys
-from functools import wraps
-from wechat_action import login_ad
 import logging
 import json
 import time
@@ -56,7 +47,7 @@ class WechatApi():
         service_name = ''
         self.service_loop(_api_get_name, {'self': self, 'service_name': service_name})
 
-    def get_human_info(self, err_num=0):
+    def get_human_info(self, sql_session, err_num=0):
         try:
             def _get_human_info(self, service_name):
                 # 耗时一秒以内
@@ -111,13 +102,13 @@ class WechatApi():
             service_name = self.driver.find_element_by_xpath('//*[@id="root"]/div/header/div/div[3]/div/div[1]').text
             _get_human_info(self, service_name=service_name)
             self.service_loop(_get_human_info, {'self': self, 'service_name': service_name})
-            self.log_ad.cookies_save(self.log_ad)
+            self.log_ad.cookies_save(self.log_ad, sql_session)
             return {'sucess': True, 'result_info': '', "result_list": self.human_info_list}
         except Exception as e:
             logging.error(str(e))
             if err_num < 3:
                 self.human_info_list = []
-                return self.get_human_info(err_num=err_num + 1)
+                return self.get_human_info(sql_session, err_num=err_num + 1)
             return {'sucess': False, 'result_info': str(e)}
 
     def service_loop(self, function, kwargs):