瀏覽代碼

MOD:更新获取微信相关信息

cxyu 3 年之前
父節點
當前提交
d824619c91
共有 4 個文件被更改,包括 221 次插入384 次删除
  1. 68 126
      web_module/tornado_api.py
  2. 89 203
      web_module/user_action.py
  3. 4 2
      wechat_action/sql_tools.py
  4. 60 53
      wechat_api/get_wechat_info.py

+ 68 - 126
web_module/tornado_api.py

@@ -6,6 +6,8 @@ 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
@@ -44,21 +46,6 @@ class BaseHandler(tornado.web.RequestHandler):
         self.set_header('Access-Control-Allow-Methods', '*')
 
 
-class create_ad_plan_remote(BaseHandler):
-    # 1.批量创建计划
-    # 返回创建计划是否已经开始
-    # TODO:落地页创建也在这个里面
-
-    def post(self):
-        user_id = self.get_argument("user_id", None)
-        ad_plan_name = self.get_argument("ad_plan_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_ad_plan,
-                         args=(user_id, ad_plan_name, wechat_json, log_ad, db, cookie_canuse)).start()
-
-
 class create_ad_plan_local(BaseHandler):
     def post(self):
         request_dict = json.loads(self.request.body, encoding='utf-8')
@@ -71,7 +58,6 @@ class create_ad_plan_local(BaseHandler):
             self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
             return
         # 落地页名字精确到毫秒,默认是全局唯一
-        # TODO:检查一下plan--内容 有无问题-----和前端确定一下
         for _ in ad_plan_list:
             ad_plan_name = _['title']
             ad_plan_typesetting_info = {'user_id': user_id, 'name': ad_plan_name,
@@ -86,11 +72,11 @@ class create_ad_plan_local(BaseHandler):
 
 class create_ad_plan(BaseHandler):
     # TODO:只要tornado开着就不允许修改数据库,------想好之后上线如何操作
+    # TODO:需要与刷新用户cookie相关action 联动,---------正在刷新用户信息时,不能进行计划创建.反之同理
     def post(self):
         request_dict = json.loads(self.request.body, encoding='utf-8')
         print(request_dict)
         user_id = request_dict['user_id']
-        # TODO:task_name设置全局唯一
         ad_plan_list = request_dict['plan_list']
 
         # 1.查看是否cookie可用
@@ -116,7 +102,8 @@ class create_ad_plan(BaseHandler):
             sql_session.execute(ad_plan_typesetting_inserte)
 
         # 2.2存行为历史记录
-        task_name = '{user_id}_{time_sign}'.format(user_id=user_id, time_sign=time.time())
+        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.查看历史中有无对应落地页
 
@@ -170,7 +157,7 @@ class get_ad_plan_local(BaseHandler):
             _['create_time'] = create_time.strftime("%Y-%m-%d %H:%M:%S")
             _['update_time'] = update_time.strftime("%Y-%m-%d %H:%M:%S")
             result_.append(_)
-        self.write({'statu': {'msg': 'success', "RetCode": 200},
+        self.write({'status': {'msg': 'success', "RetCode": 200},
                     'local_ad_plan_info': result_})
 
 
@@ -240,35 +227,15 @@ class get_ad_layout_local(BaseHandler):
             _['create_time'] = create_time.strftime("%Y-%m-%d %H:%M:%S")
             _['update_time'] = update_time.strftime("%Y-%m-%d %H:%M:%S")
             result_.append(_)
-        self.write({'statu': {'msg': 'success', "RetCode": 200},
+        self.write({'status': {'msg': 'success', "RetCode": 200},
                     'local_layout_info': result_})
 
 
 # TODO:wechat_info,human_info 这两张表有空时需要进行对应改进
-class ad_status(BaseHandler):
-    def get(self):
-        user_id = self.get_argument("user_id", None)
-        if user_id is None:
-            self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
-            return
-        sql_session = db.DBSession()
-
-        lines = sql_tools.get_ad_status(sql_session=sql_session, user_id=user_id)
-        result = []
-        for line in lines:
-            action_type, wechat_name, service_name, update_time, create_time, status = line
-            result.append(
-                {'action_type': json.loads(action_type), 'wechat_name': wechat_name, 'service_name': service_name,
-                 'update_time': update_time.strftime("%Y-%m-%d %H:%M:%S"),
-                 'create_time': create_time.strftime("%Y-%m-%d %H:%M:%S"), 'status': status})
-        # result = json.loads(result)
-        print(result)
-        self.write({'status': {'msg': 'success', "RetCode": 200},
-                    'ad_status_info': result})
-
+# TODO:ad_human_info ,ad_wecaht_info 两个的行为需要与create_ad_plan 进行交互
 
 class ad_human_info(BaseHandler):
-    # TODO:不允许短时间,刷新
+    # TODO:设置一下update---table,如果失败了sql_session需要关闭
 
     @staticmethod
     def refresh_wechat_cookies(tornado_web, user_id):
@@ -286,8 +253,6 @@ class ad_human_info(BaseHandler):
         cookie_canuse = False
         if cookie_db:
             cookie_db = pickle.loads(cookie_db)
-
-            # TODO:log 日志需要进行对应配置
             if not log_ad.wechat_cookies_check_alive(cookie_db):
                 # cookie 不能使用
                 wechat_code = log_ad.log_in()
@@ -308,51 +273,56 @@ class ad_human_info(BaseHandler):
 
     # 1.人群包获取
     def get(self):
-        # TODO:添加分页
-
-        # 0.是否刷新
-        # 1.获取userid,以及是否刷新
-        user_id = self.get_argument("user_id", None)
-        human_package_name = self.get_argument('human_package_name', None)
-        is_refresh = self.get_argument("is_refresh", None)
-        wechat_name = self.get_argument('wechat_name', None)
-        service_name = self.get_argument('service_name', None)
-        print(user_id, is_refresh)
-        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)
-            threading.Thread(target=user_action.get_human_info,
-                             args=(
-                                 user_id, log_ad, db, cookie_canuse)).start()
-
-        else:
-            # 1.查看是否在刷新,
-            #     在刷新中,
-            #       返回正在刷新
-
-            # -------不管上面逻辑让他们多刷新几次
-            #     不在刷新
-            #       返回对应数据
-            # 2.获取userid对应数据
-            result = sql_tools.get_human_info(sql_session=sql_session,
-                                              service_name=service_name, wechat_name=wechat_name)
-            print(result)
-            result = json.loads(result)
-            if human_package_name:
-                result = [_ for _ in result if human_package_name in _['name']]
-            result_ = []
-            for i in range(len(result)):
-                _ = result[i]
-                _['id'] = i
-                result_.append(_)
-            self.write({'status': {'msg': 'success', "RetCode": 200},
-                        'human_info': result})
-
+        try:
+            # TODO:添加分页
+
+            # 0.是否刷新
+            # 1.获取userid,以及是否刷新
+            user_id = self.get_argument("user_id", None)
+            human_package_name = self.get_argument('human_package_name', None)
+            is_refresh = self.get_argument("is_refresh", None)
+            wechat_name = self.get_argument('wechat_name', None)
+            service_name = self.get_argument('service_name', None)
+            print(user_id, is_refresh)
+            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)
+                threading.Thread(target=user_action.get_human_info,
+                                 args=(
+                                     user_id, log_ad, db, cookie_canuse)).start()
 
+            else:
+                # 1.查看是否在刷新,
+                #     在刷新中,
+                #       返回正在刷新
+
+                # -------不管上面逻辑让他们多刷新几次
+                #     不在刷新
+                #       返回对应数据
+                # 2.获取userid对应数据
+                result = sql_tools.get_human_info(sql_session=sql_session,
+                                                  service_name=service_name, wechat_name=wechat_name)
+                print(result)
+                result = json.loads(result)
+                if human_package_name:
+                    result = [_ for _ in result if human_package_name in _['name']]
+                result_ = []
+                for i in range(len(result)):
+                    _ = result[i]
+                    _['id'] = i
+                    result_.append(_)
+                self.write({'status': {'msg': 'success', "RetCode": 200},
+                            'human_info': result})
+        except Exception as e:
+            logging.error(str(e))
+            try:
+                sql_session.commit()
+            except:
+                pass
 class ad_wechat_info(BaseHandler):
 
     # 1.公众号相关信息获取
@@ -495,35 +465,7 @@ class get_plan_action_record(BaseHandler):
             _['wechat_id_info'] = wechat_id_info
             _['status'] = status
             result_.append(_)
-        self.write({'statu': {'msg': 'success', "RetCode": 200},
-                    'local_ad_plan_info': result_})
-
-
-class get_task_list(BaseHandler):
-    def get(self):
-        user_id = self.get_argument('user_id', None)
-        sql_session = db.DBSession()
-        if user_id is None:
-            self.write({'status': {'msg': 'url parameter error', "RetCode": 400}})
-            return
-        # 落地页名字精确到毫秒,默认是全局唯一
-
-        result = sql_tools.get_ad_task(sql_session=sql_session, user_id=user_id)
-        result_ = []
-        for i in range(len(result)):
-            print(result[i])
-            user_id, name, service_name, wechat_name, create_time, status, typesetting, wechat_id_info = result[i]
-            _ = {}
-            _['typesetting'] = json.loads(typesetting)
-            _['ad_plan_name'] = name
-            _['id'] = i
-            _['create_time'] = create_time.strftime("%Y-%m-%d %H:%M:%S")
-            _['service_name'] = service_name
-            _['wechat_name'] = wechat_name
-            _['wechat_id_info'] = wechat_id_info
-            _['status'] = status
-            result_.append(_)
-        self.write({'statu': {'msg': 'success', "RetCode": 200},
+        self.write({'status': {'msg': 'success', "RetCode": 200},
                     'local_ad_plan_info': result_})
 
 
@@ -572,14 +514,19 @@ class get_all_ad_task(BaseHandler):
             num = num + 1
         print(json.dumps(task_dict))
 
-        self.write({'statu': {'msg': 'success', "RetCode": 200},
+        self.write({'status': {'msg': 'success', "RetCode": 200},
                     'local_ad_plan_info': result_})
 
 
+def heart_jump():
+    # TODO:tornado 心跳检测,下周做----线程不断检查,线程生命周期60分钟
+    pass
+
+
 def make_app():
     return tornado.web.Application([
-        ("/get_all_ad_task", get_all_ad_task),
-        ("/create_ad_plan", create_ad_plan),
+        ("/get_all_ad_task", get_all_ad_task),  # 获取所有任务状态,
+        ("/create_ad_plan", create_ad_plan),  #
         ("/get_ad_wechat_service_name", get_ad_wechat_service_name),
         ("/get_ad_wechat_wechat_name", get_ad_wechat_wechat_name),
         # ("/create_ad_plan_local", create_ad_plan_local),
@@ -588,13 +535,10 @@ def make_app():
         ("/get_ad_plan_local", get_ad_plan_local),
         ("/delete_layout_local", delete_ad_layout),
         ("/delete_ad_plan_local", delete_ad_plan),
-        # ("/create_ad_plan_remote", create_ad_plan_remote),
         # ("/create_ad_layout_remote", create_ad_layout_remote),
         ("/ad_human_info", ad_human_info),
         ("/ad_wechat_info", ad_wechat_info),
-        # ("/ad_status", ad_status),
         ("/get_plan_action_record", get_plan_action_record),
-        # ("/get_task_list", get_task_list)
     ], debug=True, autoreload=True)
 
 
@@ -613,11 +557,9 @@ if __name__ == "__main__":
         format="%(asctime)s - %(levelname)s %(filename)s %(funcName)s %(lineno)s - %(message)s"
     )
     handler = logging.FileHandler('tornado.log')
-    logger=logging.getLogger()
+    logger = logging.getLogger()
     logger.addHandler(handler)
     logger.setLevel(logging.INFO)
-    logger.info('foo')
-    tornado.log.LogFormatter()
     app = make_app()
     app.listen(8888)
     tornado.ioloop.IOLoop.current().start()

+ 89 - 203
web_module/user_action.py

@@ -4,7 +4,9 @@ from wechat_action.create_ad_layout import CreateAd
 from wechat_action.create_ad_plan import CreateAdPlan
 from wechat_action import sql_tools
 from sqlalchemy import Table
+from communication_tools import dingtalk
 import json
+import logging
 import time
 
 layout_create_action = 'create_ad_layout'
@@ -37,39 +39,32 @@ def cookie_acion(db, log_ad, cookie_canuse, user_id):
     log_ad.log_in_wait()
     # 1.保存cookie,
     if not cookie_canuse:
-        wechat_cookies = log_ad.wechat_cookie_pickle()
-
-        update_res = wechat_cookies_table.update() \
-            .where(wechat_cookies_table.c.user_id == user_id) \
-            .values(cookies=wechat_cookies)
-        update_res = sql_session.execute(update_res)
-        sql_session.commit()
-        if update_res.rowcount == 0:
-            wechat_cookies_info = {'user_id': user_id, 'cookies': wechat_cookies}
-            wechat_insert = save_wechat_cookies(wechat_cookies_info=wechat_cookies_info,
-                                                table_wechat_cookies=wechat_cookies_table)
-
-            sql_session.execute(wechat_insert)
-            sql_session.commit()
+        log_ad.cookies_save(log_ad)
         print('update wechat cookies')
 
 
 def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
     # TODO:action_record涉及到的地方都需要改动
+    # 创建中的数据需要进行删除
 
-    action_record_table = Table('action_record', db.metadata,
-                                autoload=True, autoload_with=db.engine)
-    sql_session = db.DBSession()
+    # TODO:
+    #  1.任务有不断中途新增,该如何操作.------只管
+    #  2.落地页创建一半失败了怎么办
 
-    # cookies保存
-    cookie_acion(db, log_ad, cookie_canuse, user_id)
     try:
+        action_record_table = Table('action_record', db.metadata,
+                                    autoload=True, autoload_with=db.engine)
+        sql_session = db.DBSession()
+
+        # cookies保存
+        cookie_acion(db, log_ad, cookie_canuse, user_id)
+
         for _ in ad_plan_list:
             service_name = _['service_name']
             wechat_name = _['wechat_name']
             # 1.检查1.落地页是否创建过了
             log_ad.select_ad_master(service_name, wechat_name)
-            # TODO:现在默认layout_name在30个字符以内
+            # 现在默认layout_name在30个字符以内
             layout_name = _['idea']['jump_type_page_type']['layout_name'].replace(' ', '')[:29]
             if CreateAd.check_sucess_api(layout_name=layout_name, log_ad=log_ad):
                 res = {'sucess': True, 'result_info': '已经创建过对应落地页'}
@@ -90,7 +85,6 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
                                         service_name, wechat_name)
             log_ad.refresh_driver()
             # 3.创建计划
-            # TODO:添加计划是否创建检查,现默认计划没有重复
             log_ad.select_ad_master(service_name, wechat_name)
             plan_name = _['title'].replace(' ', '')[:29]
             # plan_typesetting_dict = sql_tools.get_ad_plan_typesetting(sql_session=sql_session, user_id=user_id,
@@ -101,9 +95,12 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
             sql_tools.action_record(res, sql_session, ad_plan_create_action, user_id, plan_name, action_record_table,
                                     service_name, wechat_name)
             log_ad.refresh_driver()
-            # TODO:添加final ,来关闭chrome
+            # 每次运行微信相关操作,对微信相关信息进行刷新
+            get_human_info(user_id, log_ad, db, cookie_canuse)
     except Exception as e:
-        print(e)
+        #TODO:直接进行截图
+        dingtalk.send_message('user_id:{} 计划执行出错,进行检查\n{}'.format(user_id, str(e)))
+        logging.error(e)
     finally:
         try:
             print('任务结束')
@@ -112,191 +109,80 @@ def carry_plan(user_id, ad_plan_list, log_ad, db, cookie_canuse):
             pass
 
 
-# TODO:这里都是线程调度的函数,设定线程生命周期最长60分钟
-
 def get_human_info(user_id, log_ad, db, cookie_canuse):
-    # 数据库
-    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()
-
-    # TODO:log_ad 在这个线程结束之后并没有自动关闭,需要设置一下log_ad这个类删除,
-    #   然后看一下多次请求之后,线程数量,
-
-    # TODO:需要添加公众号信息
-
-    # 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()
-
-    # human info 相关数据进行更新
-    for _ in res_list:
-        service_name = _['service_name']
-        wechat_name = _['wechat_name']
-        human_info = _['data']['list']
+    try:
+        #TODO: 添加action_record对应数据记录
 
-        update_res = human_info_table.update() \
-            .where(human_info_table.c.service_name == service_name) \
-            .where(human_info_table.c.wechat_name == wechat_name) \
-            .values(human_info=human_info)
-        update_res = sql_session.execute(update_res)
-        sql_session.commit()
-        if update_res.rowcount == 0:
-            human_info = {'service_name': service_name, 'wechat_name': wechat_name, 'human_info': human_info}
-            human_insert = save_human_info(human_info=human_info,
-                                           table_human=human_info_table)
+        # 数据库
+        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()
+
+        # 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:
+            dingtalk.send_message('user_id:{} 获取微信数据为空,进行检查'.format(user_id))
+            log_ad.driver.close()
+            return
+        # human info 相关数据进行更新
+        for _ in res_list:
+            service_name = _['service_name']
+            wechat_name = _['wechat_name']
+            human_info = _['data']['list']
 
-            sql_session.execute(human_insert)
+            update_res = human_info_table.update() \
+                .where(human_info_table.c.service_name == service_name) \
+                .where(human_info_table.c.wechat_name == wechat_name) \
+                .values(human_info=human_info)
+            update_res = sql_session.execute(update_res)
             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']
-        wechat_info = {'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()
-
-
-def create_layout(user_id, layout_name, wechat_json, log_ad, db, cookie_canuse):
-    # TODO:
-    #  1.任务有不断中途新增,该如何操作.------只管
-    #  2.落地页创建一半失败了怎么办
-    #  4.cookie记录每次刷新都会失效,需要改进
-
-    action_record_table = Table('action_record', db.metadata,
-                                autoload=True, autoload_with=db.engine)
-    sql_session = db.DBSession()
-    # 等待页面加载完成
-
-    # cookies保存
-    cookie_acion(db, log_ad, cookie_canuse, user_id)
-
-    # 获取到对应layout
-    typesetting_json = sql_tools.get_layout_typesetting(sql_session=sql_session, user_id=user_id,
-                                                        typesetting_name=layout_name)
-    if not typesetting_json:
-        return
-    print(typesetting_json)
-    typesetting_dict = json.loads(typesetting_json)
-    typesetting_dict[-4] = {'layout_name': layout_name}
-    # action 进行对应记录
-    wechat_json = json.loads(wechat_json)
-
-    for _ in wechat_json:
-        service_name = _['service_name']
-        wechat_name = _['wechat_name']
-        # 检查落地页之前有无创建过
-        layout_num = sql_tools.check_layout_alive(sql_session, service_name, wechat_name, layout_name)
-        if layout_num:
-            continue
-        log_ad.select_ad_master(service_name, wechat_name)
-        res = CreateAd(login_ad=log_ad, service_name=service_name, wechat_name=wechat_name).create_layout(
-            typesetting_dict)
-        print(res)
-        if not res['sucess']:
-            now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
-            try:
-                log_ad.driver.save_screenshot(
-                    'create_ad_layout-{layout_name}-{now_time}-{service_name}-{wechat_name}.png'.format(
-                        layout_name=layout_name,
-                        now_time=now_time,
-                        service_name=service_name,
-                        wechat_name=wechat_name))
-            except:
-                pass
-            # TODO:截图,传回错误信息
-        action_type = layout_create_action
-        # TODO:进行对应数据修改
-        sql_tools.action_record(res, sql_session, action_type, user_id, layout_name, action_record_table, service_name,
-                                wechat_name)
-        log_ad.refresh_driver()
-
-    # 成功一个record,更新一个record
-    log_ad.driver.quit()
-
-
-def create_ad_plan(user_id, ad_plan_name, wechat_json, log_ad, db, cookie_canuse):
-    # TODO:
-    #  1.任务有不断中途新增,该如何操作.------只管
-    #  2.落地页创建一半失败了怎么办
-    #  4.cookie记录每次刷新都会失效,需要改进
-
-    action_record_table = Table('action_record', db.metadata,
-                                autoload=True, autoload_with=db.engine)
-    sql_session = db.DBSession()
-    action_type = ad_plan_create_action
-    # 等待页面加载完成
-
-    # 1.cookies保存
-    cookie_acion(db, log_ad, cookie_canuse, user_id)
-
-    # 获取到对应layout
-    typesetting_json = sql_tools.get_ad_plan_typesetting(sql_session=sql_session, user_id=user_id,
-                                                         typesetting_name=ad_plan_name)
-    if not typesetting_json:
-        return
-    print(typesetting_json)
-    typesetting_dict = json.loads(typesetting_json)
-    typesetting_dict['title'] = ad_plan_name
-    # action 进行对应记录
-    wechat_json = json.loads(wechat_json)
-
-    for _ in wechat_json:
-        service_name = _['service_name']
-        wechat_name = _['wechat_name']
-        # 检查这个名字是否已经创建过
-        layout_name = typesetting_dict['idea']['jump_type-page_type']
-        if not sql_tools.check_layout_alive(sql_session, service_name, wechat_name, layout_name):
-            res = {'sucess': False, 'result_info': '公众号对应落地页未创建'}
-            sql_tools.action_record(res, sql_session, action_type, user_id, ad_plan_name, action_record_table,
-                                    service_name,
-                                    wechat_name)
-            continue
-        if sql_tools.check_plan_alive(sql_session, service_name, wechat_name, ad_plan_name):
-            continue
-        log_ad.select_ad_master(service_name, wechat_name)
-        res = CreateAdPlan(login_ad=log_ad, task=typesetting_dict, service_name=service_name,
-                           wechat_name=wechat_name).run()
-        print(res)
-        if not res['sucess']:
-            now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
-            try:
-                log_ad.driver.save_screenshot(
-                    'create_ad_plan-{plan_name}-{now_time}-{service_name}-{wechat_name}.png'.format(
-                        plan_name=ad_plan_name,
-                        now_time=now_time,
-                        service_name=service_name,
-                        wechat_name=wechat_name))
-            except:
-                pass
-            # 截图,传回错误信息
-
-        sql_tools.action_record(res, sql_session, action_type, user_id, ad_plan_name, action_record_table, service_name,
-                                wechat_name)
-        log_ad.refresh_driver()
+            if update_res.rowcount == 0:
+                human_info = {'service_name': service_name, 'wechat_name': wechat_name, 'human_info': human_info}
+                human_insert = save_human_info(human_info=human_info,
+                                               table_human=human_info_table)
+
+                sql_session.execute(human_insert)
+                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)
 
-    # 成功一个record,更新一个record
-    log_ad.driver.quit()
+            sql_session.execute(wechat_insert)
+            sql_session.commit()
+        print('update wechat info')
+        # 浏览器关闭
+        log_ad.driver.close()
+    except Exception as e:
+        dingtalk.send_message('user_id:{} 获取微信数据出错,进行检查\n{}'.format(user_id, str(e)))
+        logging.error(e)
+    finally:
+        try:
+            print('任务结束')
+            log_ad.driver.quit()
+        except:
+            pass
 
 
 if __name__ == "__main__":

+ 4 - 2
wechat_action/sql_tools.py

@@ -42,8 +42,8 @@ def save_layout_typesetting_info(layout_typesetting_info, table_layout_typesetti
 
 def save_ad_plan_typesetting_info(ad_plan_typesetting_info, table_ad_plan_typesetting):
     # TODO:正常action_record  返回plan_name layout_name有问题
-    for k,v in ad_plan_typesetting_info.items():
-        print(k,v)
+    for k, v in ad_plan_typesetting_info.items():
+        print(k, v)
     insert_ad_plan_typesetting = table_ad_plan_typesetting.insert()
     insert_ad_plan_typesetting = insert_ad_plan_typesetting.values \
         (typesetting=ad_plan_typesetting_info['typesetting'],
@@ -58,6 +58,8 @@ def save_wechat_info(wechat_info, table_wechat):
     insert_wechat = insert_wechat.values \
         (service_name=wechat_info['service_name'],
          wechat_name=wechat_info['wechat_name'],
+         appid=wechat_info['appid'],
+         wxname=wechat_info['wxname'],
          user_id=wechat_info['user_id']
          )
     return insert_wechat

+ 60 - 53
wechat_api/get_wechat_info.py

@@ -9,6 +9,8 @@ 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
 import re
 import requests
@@ -22,6 +24,7 @@ class WechatApi():
         self.human_info_list = []
 
     def api_get_name(self):
+        # 微信公众号相关内容获取,现在基本不使用,用get_human_info为主
         def _api_get_name(self, service_name):
             WebDriverWait(self.driver, 100).until(
                 lambda driver: True if service_name in self.driver.page_source and len(
@@ -53,62 +56,68 @@ class WechatApi():
         service_name = ''
         self.service_loop(_api_get_name, {'self': self, 'service_name': service_name})
 
-    def get_human_info(self):
-        #TODO:需要有重试机制
-        def _get_human_info(self, service_name):
-            # 耗时一秒以内
-            # self.driver.get('https://a.weixin.qq.com/client')
+    def get_human_info(self, err_num=0):
+        try:
+            def _get_human_info(self, service_name):
+                # 耗时一秒以内
+                # self.driver.get('https://a.weixin.qq.com/client')
 
-            WebDriverWait(self.driver, 100).until(
-                lambda driver: True if service_name in self.driver.page_source and len(
-                    re.findall('g_tk=(\d+)', self.driver.page_source)) else False)
+                WebDriverWait(self.driver, 100).until(
+                    lambda driver: True if service_name in self.driver.page_source and len(
+                        re.findall('g_tk=(\d+)', self.driver.page_source)) else False)
 
-            cookie_dict = self.log_ad.get_cookie(self.driver)
-            url_token = re.findall('g_tk=(\d+)', self.driver.page_source)[0]
+                cookie_dict = self.log_ad.get_cookie(self.driver)
+                url_token = re.findall('g_tk=(\d+)', self.driver.page_source)[0]
 
-            # 得到各个appid
-            wechat_names_url = 'https://a.weixin.qq.com/cgi-bin/agency/get_delivery_metrics?page=1&page_size=10&search_key=&order_by=&ascending=1&only_collect=0&g_tk={token}&_={time_}'.format(
-                token=url_token, time_=int(time.time()))
-            rsp = requests.get(url=wechat_names_url, cookies=cookie_dict)
+                # 得到各个appid
+                wechat_names_url = 'https://a.weixin.qq.com/cgi-bin/agency/get_delivery_metrics?page=1&page_size=10&search_key=&order_by=&ascending=1&only_collect=0&g_tk={token}&_={time_}'.format(
+                    token=url_token, time_=int(time.time()))
+                rsp = requests.get(url=wechat_names_url, cookies=cookie_dict)
 
-            print(wechat_names_url)
-            print(rsp.text)
-
-            # 普通用户的需要在1s内获取到
-            # 得到wechat_token
-            for i in rsp.json()['list']:
-                wechat_name = i['nickname']
-                wechat_id = i['appid']
-                wechat_tran_url = 'http://a.weixin.qq.com/cgi-bin/agency/redirect_mp?appid={wechat_id}&g_tk={token}&mgr_type=1'.format(
-                    token=url_token, wechat_id=wechat_id)
-                session = requests.session()
-                rsp = session.get(wechat_tran_url, cookies=cookie_dict)
-
-                print(rsp.url)
-                token_id = re.findall('token=(\d+)', rsp.url)[0]
-                print(token_id)
-                # 得到人群包
-                #TODO:人群包相关的数据
-                human_url = 'https://mp.weixin.qq.com/promotion/dmpmgr?action=readlist&page=1&page_size=100&token={wechat_token}&appid=&spid=&_={time_}'.format(
-                    wechat_token=token_id, time_=int(time.time()))
-                print(human_url)
-                rsp = session.get(url=human_url)
+                print(wechat_names_url)
                 print(rsp.text)
-                res_json = rsp.json()
-                res_json['service_name'] = service_name
-                res_json['wechat_name'] = wechat_name
-
-                self.human_info_list.append(res_json)
-                print(self.human_info_list)
-                import json
-                print(json.dumps(self.human_info_list))
 
+                # 普通用户的需要在1s内获取到
+                # 得到wechat_token
+                for i in rsp.json()['list']:
+                    wechat_wxname = i['wxname']
+                    wechat_name = i['nickname']
+                    wechat_id = i['appid']
+                    wechat_tran_url = 'http://a.weixin.qq.com/cgi-bin/agency/redirect_mp?appid={wechat_id}&g_tk={token}&mgr_type=1'.format(
+                        token=url_token, wechat_id=wechat_id)
+                    session = requests.session()
+                    rsp = session.get(wechat_tran_url, cookies=cookie_dict)
+
+                    print(rsp.url)
+                    token_id = re.findall('token=(\d+)', rsp.url)[0]
+                    print(token_id)
+                    # 得到人群包
+                    human_url = 'https://mp.weixin.qq.com/promotion/dmpmgr?action=readlist&page=1&page_size=100&token={wechat_token}&appid=&spid=&_={time_}'.format(
+                        wechat_token=token_id, time_=int(time.time()))
+                    print(human_url)
+                    rsp = session.get(url=human_url)
+                    print(rsp.text)
+                    res_json = rsp.json()
+                    res_json['service_name'] = service_name
+                    res_json['wechat_name'] = wechat_name
+                    res_json['wxname'] = wechat_wxname
+                    res_json['appid'] = wechat_id
+
+                    self.human_info_list.append(res_json)
+                    print(self.human_info_list)
+                    print(json.dumps(self.human_info_list))
 
-        time.sleep(random.uniform(3, 5))
-        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})
-        return self.human_info_list
+            time.sleep(random.uniform(3, 5))
+            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)
+            return 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)
 
     def service_loop(self, function, kwargs):
 
@@ -139,7 +148,7 @@ class WechatApi():
         self.driver.execute_script('''
             window.scroll(0,1000000);
                                             ''')
-        time.sleep(random.uniform(3, 5))
+        # time.sleep(random.uniform(3, 5))
         click_service_change()
 
         # WebDriverWait(self.driver, 10).until(
@@ -178,11 +187,9 @@ class WechatApi():
                         kwargs['service_name'] = choice_service_name
                     function(**kwargs)
                     break
-            # TODO:之后去除掉,time.sleep超过 1s 的,暂时先能跑
-            time.sleep(random.uniform(3, 5))
+            # time.sleep(random.uniform(3, 5))
             click_service_change()
 
 
 if __name__ == "__main__":
-    # TODO:获取api失败时该怎么操作,
     pass