Преглед на файлове

ADD:添加 微信 扫码交互

cxyu преди 3 години
родител
ревизия
df2c7a75a2
променени са 4 файла, в които са добавени 55 реда и са изтрити 18 реда
  1. 16 5
      web_module/tornado_api.py
  2. 4 5
      wechat_action/create_ad_plan_idea.py
  3. 5 7
      wechat_action/login_ad.py
  4. 30 1
      wechat_action/sql_tools.py

+ 16 - 5
web_module/tornado_api.py

@@ -229,16 +229,24 @@ class get_ad_layout_local(BaseHandler):
                     'local_layout_info': result_})
 
 
-# TODO:wechat_info,human_info 这两张表有空时需要进行对应改进
-# TODO:ad_human_info ,ad_wecaht_info 两个的行为需要与create_ad_plan 进行交互
+class get_scan_status(BaseHandler):
+    # 获取到扫码状态
+    def get(self):
+        sql_session = db.DBSession()
+        user_id = self.get_argument("user_id", None)
+        status = sql_tools.get_scan_action_status(user_id, sql_session)
+        if user_id is None:
+            self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
+            return
+        self.write({'status': {'msg': 'success', "RetCode": 200},
+                    'scan_action_status': status})
+
 
+# TODO:wechat_info,human_info 这两张表有空时需要进行对应改进
 class ad_human_info(BaseHandler):
     # TODO:设置一下update---table,如果失败了sql_session需要关闭
-
     @staticmethod
     def refresh_wechat_cookies(tornado_web, user_id):
-        # TODO:添加互动接口,添加状态字段,打开selenium就变换
-
         # 1.返回二维码链接
         # ----1.查看cookie是否可用
         sql_session = db.DBSession()
@@ -254,6 +262,7 @@ class ad_human_info(BaseHandler):
             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')
@@ -265,6 +274,7 @@ class ad_human_info(BaseHandler):
         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
@@ -563,6 +573,7 @@ def make_app():
         ("/get_ad_plan_local", get_ad_plan_local),
         ("/delete_layout_local", delete_ad_layout),
         ("/delete_ad_plan_local", delete_ad_plan),
+        ("/get_scan_status", get_scan_status),
         # ("/create_ad_layout_remote", create_ad_layout_remote),
         ("/ad_human_info", ad_human_info),
         ("/ad_wechat_info", ad_wechat_info),

+ 4 - 5
wechat_action/create_ad_plan_idea.py

@@ -229,7 +229,6 @@ class IdeaAction:
         self.driver.find_element_by_xpath(
             '//*[@class="ql-editor ql-blank"]'
         ).send_keys(self.task['idea']['content'])
-
         time.sleep(random.uniform(0.1, 0.5))
 
         # 朋友圈标题设置
@@ -242,9 +241,9 @@ class IdeaAction:
         # 朋友圈其他设置
         select_elements = self.driver.find_elements_by_xpath('//*[@class="label-2ZOAbuO31o"]')
         for _ in select_elements:
-            if _.text == '了解更多' and self.task['idea']['jump_type_text_type'] == 2:
+            if _.text == '了解更多' and self.task['idea']['jump_type_text_type'] == '2':
                 _.click()
-            if _.text == '原生推广页' and self.task['idea']['jump_type_link_type'] == 2:
+            if _.text == '原生推广页' and self.task['idea']['jump_type_link_type'] == '2':
                 _.click()
 
         # 朋友圈推广页选择
@@ -257,8 +256,8 @@ class IdeaAction:
                 _.send_keys(self.task['idea']['jump_type_page_type']['layout_name'])
                 _.send_keys(Keys.RETURN)
         WebDriverWait(self.driver, 30).until(
-            lambda driver: len(self.driver.find_elements_by_xpath('//*[@class="adui-table-cellInner"]'))==5)
-        #搜索速度比较慢没有这么快
+            lambda driver: len(self.driver.find_elements_by_xpath('//*[@class="adui-table-cellInner"]')) == 5)
+        # 搜索速度比较慢没有这么快
         time.sleep(1)
         WebDriverWait(self.driver, 30).until(
             lambda driver: [_ for _ in self.driver.find_elements_by_xpath('//*[@class="adui-table-cellInner"]') if

+ 5 - 7
wechat_action/login_ad.py

@@ -39,7 +39,7 @@ class LogIn:
         # options.add_experimental_option("prefs", prefs)
 
         driver = webdriver.Remote(
-            # command_executor='http://192.168.1.101:4444/wd/hub',
+            # command_executor='http://192.168.1.100/wd/hub',
             command_executor='http://118.31.53.105:4555/wd/hub',
             options=options)
         # driver = webdriver.Chrome(options=options)
@@ -57,7 +57,6 @@ class LogIn:
         # WebDriverWait(driver, 3).until(EC.invisibility_of_element_located((By.CSS_SELECTOR, img_selector)))
         # time.sleep(3)
         img_url = self.driver.find_element_by_css_selector(img_selector)
-        print('img', img_url.get_attribute('src'))
         return img_url.get_attribute('src')
 
     def log_in_wait(self):
@@ -76,12 +75,13 @@ class LogIn:
         wechat_cookies = log_ad.wechat_cookie_pickle()
         update_res = log_ad.wechat_cookies_table.update() \
             .where(log_ad.wechat_cookies_table.c.user_id == log_ad.user_id) \
-            .values(cookies=wechat_cookies)
+            .values(cookies=wechat_cookies,
+                    scan_action='done')
         sql_session = log_ad.db.DBSession()
         cursor = sql_session.execute(update_res)
         sql_session.commit()
         if cursor.rowcount == 0:
-            wechat_cookies_info = {'user_id': log_ad.user_id, 'cookies': wechat_cookies}
+            wechat_cookies_info = {'user_id': log_ad.user_id, 'cookies': wechat_cookies, 'scan_action': 'done'}
             wechat_insert = sql_tools.save_wechat_cookies(wechat_cookies_info=wechat_cookies_info,
                                                           table_wechat_cookies=log_ad.wechat_cookies_table)
             sql_session.execute(wechat_insert)
@@ -131,7 +131,6 @@ class LogIn:
         cookies = driver.get_cookies()
         cookie_dict = {}
         for _ in cookies:
-            print(_)
             cookie_dict[_['name']] = _['value']
         return cookie_dict
 
@@ -161,7 +160,6 @@ class LogIn:
         self.driver.get('https://a.weixin.qq.com/client')
         self.driver.get(check_url)
 
-        print('检查cookies 结果', self.driver.page_source)
         if '4101' in self.driver.page_source:
             return False
         else:
@@ -196,7 +194,7 @@ class LogIn:
                     self.driver.get('https://a.weixin.qq.com')
                     break
             except Exception as e:
-                print(e)
+                logging.error(e)
                 err_num = err_num + 1
                 if err_num > 3:
                     break

+ 30 - 1
wechat_action/sql_tools.py

@@ -15,7 +15,8 @@ def save_wechat_cookies(wechat_cookies_info, table_wechat_cookies):
     insert_wechat_cookies = table_wechat_cookies.insert()
     insert_layout = insert_wechat_cookies.values \
         (user_id=wechat_cookies_info['user_id'],
-         cookies=wechat_cookies_info['cookies']
+         cookies=wechat_cookies_info['cookies'],
+         scan_action=wechat_cookies_info['scan_action']
          )
     return insert_layout
 
@@ -99,6 +100,26 @@ def get_human_info(sql_session, service_name, wechat_name):
     return result_list
 
 
+def get_scan_action_status(user_id, sql_session):
+    # 扫码成功 done 扫码失败 error(doing ,超过一分钟) 扫码中 doing
+    sql = '''select timestampdiff(minute ,update_time ,current_timestamp ) as diff_time,
+        scan_action from wechat_cookies wc 
+        where user_id ='{}'
+    '''.format(user_id)
+    cursor = sql_session.execute(sql)
+    lines = cursor.fetchall()
+    if len(lines):
+        diff_time, scan_action = lines[0]
+        if scan_action == 'done':
+            return 'done'
+        elif scan_action =='doing':
+            if diff_time > 1:
+                return 'error'
+            else:
+                return 'doing'
+    return 'error'
+
+
 def get_layout_typesetting_rough(sql_session, user_id, typesetting_name):
     sql = '''
             select typesetting,name,create_time,update_time from layout_typesetting lt 
@@ -261,6 +282,14 @@ def update_task_status_error(sql_session, user_id, task_name):
     sql_session.commit()
 
 
+def update_user_scan_action(user_id, sql_session):
+    sql = '''update wechat_cookies 
+            set scan_action ='doing'
+            where user_id ='{}' '''.format(user_id)
+    sql_session.execute(sql)
+    sql_session.commit()
+
+
 def get_ad_task(sql_session, user_id):
     sql = '''
     select task_name,status,count(*),min(ar.create_time),apt.typesetting from action_record ar