Explorar o código

MOD:完善错误记录

cxyu %!s(int64=3) %!d(string=hai) anos
pai
achega
b0f980249f

+ 5 - 40
web_module/tornado_api.py

@@ -3,11 +3,7 @@ from settings import using_config
 import tornado.log
 import tornado.ioloop
 import tornado.web
-import json
-import time
-from wechat_api.get_wechat_info import WechatApi
 from logging import handlers
-from datetime import datetime
 from wechat_action.login_ad import LogIn
 from wechat_action import sql_tools
 import threading
@@ -101,28 +97,6 @@ class create_ad_plan(BaseHandler):
                 table_ad_plan_typesetting=ad_plan_typesetting_table)
             sql_session.execute(ad_plan_typesetting_inserte)
 
-        # 2.2存行为历史记录
-        task_name = '用户: {user_id}  时间:{time_sign}'.format(user_id=user_id,
-                                                           time_sign=datetime.now().strftime("%Y-%m-%d, %H:%M:%S"))
-        for _ in ad_plan_list:
-            # 1.查看历史中有无对应落地页
-
-            # TODO:落地页直接存放到action_record,运行过程中进行不同公众号进行不同操作
-
-            # 2.历史记录
-            print(_)
-            for action_type in ['create_ad_plan', 'create_ad_layout']:
-                object_name = _['title'] if action_type == 'create_ad_plan' else _['idea']['jump_type_page_type'][
-                    'layout_name']
-                object_name = object_name.replace(' ', '')[:29]
-                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()
-
         # 4.开始运行
         threading.Thread(target=user_action.carry_plan,
                          args=(user_id, ad_plan_list, log_ad, db, cookie_canuse)).start()
@@ -161,25 +135,13 @@ class get_ad_plan_local(BaseHandler):
                     'local_ad_plan_info': result_})
 
 
-class create_ad_layout_remote(BaseHandler):
-    # 1.批量创建落地页
-    def post(self):
-        user_id = self.get_argument("user_id", None)
-        layout_name = self.get_argument("layout_name", None)
-        # wechat_json :[{'service_name':'one','wechat_name':''},{'service_name':'','wechat_name':''}]
-        wechat_json = self.get_argument('wechat_json', None)
-        log_ad, cookie_canuse = ad_human_info.refresh_wechat_cookies(self, user_id=user_id)
-        threading.Thread(target=user_action.create_layout,
-                         args=(user_id, layout_name, wechat_json, log_ad, db, cookie_canuse)).start()
-
-
 class create_ad_layout_local(BaseHandler):
     def post(self):
         # TODO:返回一个layout_name重复的一个信息
         request_dict = json.loads(self.request.body)
         user_id = request_dict['user_id']
         layout_typesetting = request_dict['layout_typesetting']
-        layout_name = request_dict['layout_name']
+        layout_name = request_dict['layout_name'].replace(' ', '')[:29]
 
         print(user_id, layout_typesetting, layout_name)
         print('layout-typesetting', type(layout_typesetting), layout_typesetting)
@@ -273,6 +235,8 @@ class ad_human_info(BaseHandler):
 
     # 1.人群包获取
     def get(self):
+        sql_session = db.DBSession()
+
         try:
             # TODO:添加分页
 
@@ -287,7 +251,6 @@ class ad_human_info(BaseHandler):
             if user_id is None or is_refresh is None or wechat_name is None or service_name is None:
                 self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
                 return
-            sql_session = db.DBSession()
             # TODO:一个涉及到selenium-driver的请求-生命周期.----看一下tornado是怎么处理请求的生命周期
             if int(is_refresh) == 1:
                 log_ad, cookie_canuse = self.refresh_wechat_cookies(self, user_id=user_id)
@@ -323,6 +286,8 @@ class ad_human_info(BaseHandler):
                 sql_session.commit()
             except:
                 pass
+
+
 class ad_wechat_info(BaseHandler):
 
     # 1.公众号相关信息获取

+ 89 - 40
web_module/user_action.py

@@ -1,4 +1,4 @@
-from wechat_action.sql_tools import save_wechat_cookies, save_human_info, delete_wechat_info, save_wechat_info
+from wechat_action.sql_tools import save_human_info, delete_wechat_info, save_wechat_info
 from wechat_api.get_wechat_info import WechatApi
 from wechat_action.create_ad_layout import CreateAd
 from wechat_action.create_ad_plan import CreateAdPlan
@@ -7,21 +7,22 @@ from sqlalchemy import Table
 from communication_tools import dingtalk
 import json
 import logging
-import time
+from datetime import datetime
 
 layout_create_action = 'create_ad_layout'
 ad_plan_create_action = 'create_ad_plan'
+refresh_wechat_action = 'refresh_wechat_info'
 
 
 def run(user_id, log_ad, db, cookie_canuse):
-    sql_session = db.DBSession()
-    # 检查是否有已经开启的任务
-    result = sql_tools.get_action_status(sql_session, user_id)
-    # 有:    只将任务进行添加操作todo
-    if result:
-        action_info = {'user_id': user_id, 'service_name': ''}
-
-        sql_tools.save_action_record()
+    # sql_session = db.DBSession()
+    # # 检查是否有已经开启的任务
+    # result = sql_tools.get_action_status(sql_session, user_id)
+    # # 有:    只将任务进行添加操作todo
+    # if result:
+    #     action_info = {'user_id': user_id, 'service_name': ''}
+    #
+    #     sql_tools.save_action_record()
     # 无:    任务添加到数据库中doing
 
     # 检查是否需要登录
@@ -29,6 +30,7 @@ def run(user_id, log_ad, db, cookie_canuse):
     # 任务中间添加的----不断循环获取同userid,----doing,error,todo状态任务
 
     # 关闭driver
+    pass
 
 
 def cookie_acion(db, log_ad, cookie_canuse, user_id):
@@ -55,6 +57,23 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
         action_record_table = Table('action_record', db.metadata,
                                     autoload=True, autoload_with=db.engine)
         sql_session = db.DBSession()
+        task_name = 'user_id: {user_id}  time:{time_sign} action:create_plan'.format(user_id=user_id,
+                                                                                     time_sign=datetime.now().strftime(
+                                                                                         "%Y-%m-%d, %H:%M:%S"))
+        # 2.2存行为记录
+        for _ in ad_plan_list:
+            print(_)
+            for action_type in ['create_ad_plan', 'create_ad_layout']:
+                object_name = _['title'] if action_type == 'create_ad_plan' else _['idea']['jump_type_page_type'][
+                    'layout_name']
+                object_name = object_name.replace(' ', '')[:29]
+                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()
 
         # cookies保存
         cookie_acion(db, log_ad, cookie_canuse, user_id)
@@ -70,19 +89,19 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
                 res = {'sucess': True, 'result_info': '已经创建过对应落地页'}
                 sql_tools.action_record(res, sql_session, layout_create_action, user_id, layout_name,
                                         action_record_table,
-                                        service_name, wechat_name)
+                                        service_name, wechat_name, task_name)
 
             else:
                 # 1.5无则创建落地页
                 create_ad_layout = CreateAd(login_ad=log_ad, service_name=service_name, wechat_name=wechat_name)
-                create_ad_layout.get_into_create_page()
                 layout_typesetting_dict = sql_tools.get_layout_typesetting(sql_session, user_id,
                                                                            typesetting_name=layout_name)
+                print(layout_typesetting_dict)
                 layout_typesetting_dict = json.loads(layout_typesetting_dict)
                 res = create_ad_layout.create_layout(layout_typesetting_dict)
                 sql_tools.action_record(res, sql_session, layout_create_action, user_id, layout_name,
                                         action_record_table,
-                                        service_name, wechat_name)
+                                        service_name, wechat_name, task_name)
             log_ad.refresh_driver()
             # 3.创建计划
             log_ad.select_ad_master(service_name, wechat_name)
@@ -93,12 +112,13 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
             res = create_ad_plan.run()
             # 4.更新action_record相关计划信息
             sql_tools.action_record(res, sql_session, ad_plan_create_action, user_id, plan_name, action_record_table,
-                                    service_name, wechat_name)
+                                    service_name, wechat_name, task_name)
             log_ad.refresh_driver()
             # 每次运行微信相关操作,对微信相关信息进行刷新
             get_human_info(user_id, log_ad, db, cookie_canuse)
     except Exception as e:
-        #TODO:直接进行截图
+        log_ad.driver.save_screenshot(
+            'user_id:{}_time_{}_plan_error.png'.format(user_id, datetime.now().strftime("%Y-%m-%d, %H:%M:%S")))
         dingtalk.send_message('user_id:{} 计划执行出错,进行检查\n{}'.format(user_id, str(e)))
         logging.error(e)
     finally:
@@ -111,31 +131,69 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
 
 def get_human_info(user_id, log_ad, db, cookie_canuse):
     try:
-        #TODO: 添加action_record对应数据记录
-
         # 数据库
+        action_record_table = Table('action_record', db.metadata,
+                                    autoload=True, autoload_with=db.engine)
         human_info_table = Table('human_info', db.metadata,
                                  autoload=True, autoload_with=db.engine)
         wechat_info_table = Table('wechat_info', db.metadata,
                                   autoload=True, autoload_with=db.engine)
         sql_session = db.DBSession()
 
+        # 行为记录
+        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_info'
+        action_info = {'user_id': user_id, 'service_name': '', 'wechat_name': '',
+                       'action_type': action_type, '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()
+
         # 1.cookies保存
         cookie_acion(db, log_ad, cookie_canuse, user_id)
-        # 2.刷新人群包
-
-        # 3.刷新微信 公众号 分层 信息
 
         # wechat_info.每次都删除掉前面全部数据,进行更新
         # human_info 进行全局更新
         w_api = WechatApi(log_ad=log_ad)
-        res_list = w_api.get_human_info()
-        if res_list is None:
+        res_info = w_api.get_human_info()
+
+        if not res_info['sucess']:
             dingtalk.send_message('user_id:{} 获取微信数据为空,进行检查'.format(user_id))
             log_ad.driver.close()
+            object_name = ''
+            service_name = ''
+            wechat_name = ''
+            sql_tools.action_record(res_info, sql_session, refresh_wechat_action, user_id,
+                                    object_name, action_record_table,
+                                    service_name, wechat_name, task_name)
             return
+
+        # wechat info进行数据更新
+        # 1.删除所有数据
+        delete_wechat_info(sql_session=sql_session, user_id=user_id)
+        # 2.重新添加一遍相关数据
+        for _ in res_info['result_list']:
+            service_name = _['service_name']
+            wechat_name = _['wechat_name']
+            appid = _['appid']
+            wxname = _['wxname']
+            wechat_info = {'appid': appid, 'wxname': wxname, 'service_name': service_name,
+                           'wechat_name': wechat_name,
+                           'user_id': user_id}
+            wechat_insert = save_wechat_info(wechat_info=wechat_info,
+                                             table_wechat=wechat_info_table)
+
+            sql_session.execute(wechat_insert)
+            sql_session.commit()
+        logging.info('update wechat info')
+
         # human info 相关数据进行更新
-        for _ in res_list:
+        for _ in res_info['result_list']:
             service_name = _['service_name']
             wechat_name = _['wechat_name']
             human_info = _['data']['list']
@@ -155,26 +213,17 @@ def get_human_info(user_id, log_ad, db, cookie_canuse):
                 sql_session.commit()
             print('update human info')
 
-        # wechat info进行数据更新
-        # 1.删除所有数据
-        delete_wechat_info(sql_session=sql_session, user_id=user_id)
-        # 2.重新添加一遍相关数据
-        for _ in res_list:
-            service_name = _['service_name']
-            wechat_name = _['wechat_name']
-            appid = _['appid']
-            wxname = _['wxname']
-            wechat_info = {'appid': appid, 'wxname': wxname, 'service_name': service_name, 'wechat_name': wechat_name,
-                           'user_id': user_id}
-            wechat_insert = save_wechat_info(wechat_info=wechat_info,
-                                             table_wechat=wechat_info_table)
-
-            sql_session.execute(wechat_insert)
-            sql_session.commit()
-        print('update wechat info')
         # 浏览器关闭
         log_ad.driver.close()
+        object_name = ''
+        service_name = ''
+        wechat_name = ''
+        sql_tools.action_record(res_info, sql_session, refresh_wechat_action,
+                                user_id, object_name, action_record_table,
+                                service_name, wechat_name, task_name)
     except Exception as e:
+        log_ad.driver.save_screenshot(
+            'user_id:{}_time_{}_wechat_info_error.png'.format(user_id, datetime.now().strftime("%Y-%m-%d, %H:%M:%S")))
         dingtalk.send_message('user_id:{} 获取微信数据出错,进行检查\n{}'.format(user_id, str(e)))
         logging.error(e)
     finally:

+ 1 - 2
wechat_action/create_ad_layout.py

@@ -707,8 +707,7 @@ class CreateAd:
                     self.get_into_create_page()
                     return self.create_layout(layout, err_num=err_num + 1)
         except Exception as e:
-            print(e)
-            raise
+            logging.error(e)
             # TODO:有空时讲 e 内容设置为原始内容
             if err_num > 3:
                 return {'sucess': False, 'result_info': str(e)}

+ 8 - 6
wechat_action/login_ad.py

@@ -38,11 +38,11 @@ class LogIn:
         # prefs = {"profile.managed_default_content_settings.images": 2, 'permissions.default.stylesheet': 2}
         # options.add_experimental_option("prefs", prefs)
 
-        driver = webdriver.Remote(
-            # command_executor='http://192.168.1.101:4444/wd/hub',
-            command_executor='http://118.31.53.105:4555/wd/hub',
-            options=options)
-        # driver = webdriver.Chrome(options=options)
+        # driver = webdriver.Remote(
+        #     # command_executor='http://192.168.1.101:4444/wd/hub',
+        #     command_executor='http://118.31.53.105:4555/wd/hub',
+        #     options=options)
+        driver = webdriver.Chrome(options=options)
         driver.maximize_window()
         return driver
 
@@ -68,7 +68,7 @@ class LogIn:
     @staticmethod
     def cookies_save(log_ad):
         # 切换窗口,点击创建广告,切到广告页面
-        log_ad.driver.switch_to.window(log_ad.driver.window_handles[-1])
+        log_ad.driver.switch_to.window(log_ad.driver.window_handles[0])
         WebDriverWait(log_ad.driver, 100).until(lambda driver: driver.find_element_by_class_name(
             'ui-mr-medium'))
         # cookie 进行数据库保存
@@ -85,6 +85,8 @@ class LogIn:
                                                           table_wechat_cookies=log_ad.wechat_cookies_table)
             sql_session.execute(wechat_insert)
             sql_session.commit()
+        log_ad.driver.switch_to.window(log_ad.driver.window_handles[-1])
+
 
     def select_ad_master(self, service_name, wechat_name):
         logging.info('开始切换服务商')

+ 3 - 1
wechat_action/sql_tools.py

@@ -117,6 +117,7 @@ def get_layout_typesetting(sql_session, user_id, typesetting_name):
             select typesetting from layout_typesetting lt 
             where user_id ='{}' and is_delete=0 and name='{}' ;
     '''.format(user_id, typesetting_name)
+    print(sql)
     cursor = sql_session.execute(sql)
     lines = cursor.fetchall()
     if lines:
@@ -263,7 +264,7 @@ def get_action_status(sql_session, user_id):
 
 
 def action_record(res, sql_session, action_type, user_id, object_name, action_table, service_name,
-                  wechat_name):
+                  wechat_name,task_name):
     print('get in action record ', service_name, wechat_name, res)
     status = 'done' if res['sucess'] else 'error'
     print(action_type)
@@ -273,6 +274,7 @@ def action_record(res, sql_session, action_type, user_id, object_name, action_ta
         .where(action_table.c.user_id == user_id) \
         .where(action_table.c.object_name == object_name) \
         .where(action_table.c.action_type == action_type) \
+        .where(action_table.c.task_name == task_name) \
         .where(action_table.c.status == 'todo') \
         .values({
         action_table.c.status: status,