Sfoglia il codice sorgente

MOD:扫码状态变化 fix

cxyu 3 anni fa
parent
commit
f0b7ab5c97
2 ha cambiato i file con 107 aggiunte e 40 eliminazioni
  1. 106 39
      web_module/tornado_api.py
  2. 1 1
      wechat_action/sql_tools.py

+ 106 - 39
web_module/tornado_api.py

@@ -118,6 +118,8 @@ class create_ad_plan(BaseHandler):
         log_ad = None
         try:
             request_dict = json.loads(self.request.body, encoding='utf-8')
+            print(self.request.body)
+            print(request_dict)
             ad_plan_list = request_dict['plan_list']
             user_id = request_dict['user_id']
             # 2.2存行为记录
@@ -296,29 +298,29 @@ class ad_human_info(BaseHandler):
                 return
             # 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):
-                    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()
                     threading.Thread(target=user_action.get_human_info,
                                      args=(
                                          user_id, log_ad, db, cookie_canuse, task_name)).start()
                 else:
-                    self.write({'status': {'msg': '', "RetCode": 200}})
+                    self.write({'status': {'msg': '任务有堆积', "RetCode": 200}})
             else:
                 # 1.查看是否在刷新,
                 #     在刷新中,
@@ -349,6 +351,74 @@ class ad_human_info(BaseHandler):
             sql_session.commit()
 
 
+class refresh_wechat_info(BaseHandler):
+    # TODO:刷新以及创建,限时3分钟
+    @staticmethod
+    def refresh_wechat_cookies(tornado_web, user_id):
+        # 1.返回二维码链接
+        # ----1.查看cookie是否可用
+        sql_session = db.DBSession()
+        cookie_db = sql_tools.get_wechat_cookies(sql_session, user_id=user_id)
+
+        # 进行登录操作
+        log_ad = LogIn(user_id=user_id)
+
+        # 使driver可以使用
+        cookie_canuse = False
+        if cookie_db:
+            cookie_db = pickle.loads(cookie_db)
+            if not log_ad.wechat_cookies_check_alive(cookie_db):
+                # cookie 不能使用
+                wechat_code = log_ad.log_in()
+                sql_tools.update_user_scan_action(user_id, sql_session)
+                tornado_web.write({'status': {'msg': 'success', "RetCode": 200},
+                                   'wechat_code': wechat_code})
+                logging.info('cookie can not use')
+            else:
+                # cookie 可以继续使用
+                cookie_canuse = True
+                log_ad.driver.get('https://a.weixin.qq.com/index.html')
+                tornado_web.write({'status': {'msg': 'success', "RetCode": 200}})
+        else:
+            # cookie 不能使用
+            wechat_code = log_ad.log_in()
+            sql_tools.update_user_scan_action(user_id, sql_session)
+            tornado_web.write({'status': {'msg': 'success', "RetCode": 200},
+                               'wechat_code': wechat_code})
+        return log_ad, cookie_canuse
+
+    def get(self):
+        sql_session = db.DBSession()
+        log_ad = None
+        try:
+            user_id = self.get_argument("user_id", None)
+            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):
+                threading.Thread(target=user_action.get_human_info,
+                                 args=(
+                                     user_id, log_ad, db, cookie_canuse, task_name)).start()
+            else:
+                self.write({'status': {'msg': '任务有堆积', "RetCode": 200}})
+        except:
+            pass
+
+
 class ad_wechat_info(BaseHandler):
 
     # 1.公众号相关信息获取
@@ -356,9 +426,6 @@ class ad_wechat_info(BaseHandler):
         sql_session = db.DBSession()
         log_ad = None
         try:
-            # TODO:添加分页,
-            #  公众号,服务商,唯一id设计或者获取
-
             # 0.是否刷新
             # 1.获取userid,以及是否刷新
             user_id = self.get_argument("user_id", None)
@@ -368,29 +435,29 @@ class ad_wechat_info(BaseHandler):
                 return
             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):
-                    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()
                     threading.Thread(target=user_action.get_human_info,
                                      args=(
                                          user_id, log_ad, db, cookie_canuse, task_name)).start()
                 else:
-                    self.write({'status': {'msg': '', "RetCode": 200}})
+                    self.write({'status': {'msg': '任务有堆积', "RetCode": 200}})
             else:
                 result = sql_tools.get_wechat_info(sql_session=sql_session, user_id=user_id)
                 result_list = []

+ 1 - 1
wechat_action/sql_tools.py

@@ -284,7 +284,7 @@ def update_task_status_error(sql_session, user_id, task_name):
 
 def update_user_scan_action(user_id, sql_session):
     sql = '''update wechat_cookies 
-            set scan_action ='doing'
+            set scan_action ='doing',update_time=current_timestamp
             where user_id ='{}' '''.format(user_id)
     sql_session.execute(sql)
     sql_session.commit()