renyupeng 2 years ago
parent
commit
d647237697
3 changed files with 106 additions and 30 deletions
  1. 20 13
      spider/ReturnPromoterInfoSpider.py
  2. 60 16
      utils/PromoterInfoWebHook.py
  3. 26 1
      utils/send_feishu_msg.py

+ 20 - 13
spider/ReturnPromoterInfoSpider.py

@@ -12,6 +12,7 @@ import json
 
 import requests
 
+from utils.cookie_update import cookie_update
 from utils.mysql_helper import insert
 from utils.mysql_utils import MysqlUtils
 from utils.send_feishu_msg import SendFeiShuMsg
@@ -28,20 +29,26 @@ class ReturnPromoterInfoSpider:
             promoterId=promoterId)
         headers = {'User-Agent': 'Mozilla/5.0',
                    'Cookie': cookie}
+
         try:
             rep = requests.get(url=url, headers=headers)
-            data = json.loads(rep.text)["data"]
-            returnItem = {
-                "city": data["addressInfo"]["city"],
-                "province": data["addressInfo"]["province"],
-                "fanNum": data["fanNum"],
-                "promoterHeadImgUrl": data["promoterHeadImgUrl"],
-                "promoterNickName": data["promoterNickName"],
-                "totalSale": data["promoteBaseInfo"]["totalSale"],
-            }
-            returnItemJson = json.dumps(returnItem)
-            return returnItemJson
+            if json.loads(rep.text)["result"] != 1:
+                errItem = {"result": json.loads(rep.text)["result"]}
+                return json.dumps(errItem)
+            else:
+                data = json.loads(rep.text)["data"]
+                returnItem = {
+                    "city": data["addressInfo"]["city"],
+                    "province": data["addressInfo"]["province"],
+                    "fanNum": data["fanNum"],
+                    "promoterHeadImgUrl": data["promoterHeadImgUrl"],
+                    "promoterNickName": data["promoterNickName"],
+                    "totalSale": data["promoteBaseInfo"]["totalSale"],
+                }
+                returnItemJson = json.dumps(returnItem)
+                return returnItemJson
         except Exception as e:
             SendFeiShuMsg.send_robot_msg(
-                'ReturnPromoterInfoSpider {promoterId请求错误请检查cookie{e}'.format(promoterId=promoterId, e=e))
-            return {"error": "requestsError"}
+                'ReturnPromoterInfoSpider {promoterId}请求错误请检查cookie{e}'.format(promoterId=promoterId, e=e))
+
+

+ 60 - 16
utils/PromoterInfoWebHook.py

@@ -18,7 +18,8 @@ from spider.PromoterVideoAnalysisTrend import PromoterVideoAnalysisTrend
 from multiprocessing import Process
 
 from spider.ReturnPromoterInfoSpider import ReturnPromoterInfoSpider
-from utils.mysql_utils import MysqlUtils
+from utils.cookie_update import cookie_update
+from utils.send_feishu_msg import SendFeiShuMsg
 
 app = Flask(__name__)
 
@@ -31,21 +32,64 @@ def api_root():
 def webhook_get_promoter():
     rep = json.loads(request.data)
     promoterId = rep["promoterId"]
-    sql = "select cookie from kwai_promoter.kuaishou_supply_chain_cookie"
-    cookie = MysqlUtils().QueryOne(sql)[0]
-    rollback = ReturnPromoterInfoSpider().PromoterInfoSpiderHandler(promoterId, cookie)
-    p1 = Process(target=PromoterInfoSpider().PromoterInfoSpiderHandler, args=(promoterId, cookie))
-    p2 = Process(target=PromoterFansInfo().PromoterFansInfoHandler, args=(promoterId, cookie))
-    p3 = Process(target=PromoterLiveInfoSpider().PromoterLiveInfoSpiderHander, args=(promoterId, cookie))
-    p4 = Process(target=PromoterVideoAnalysisInfo().PromoterVideoAnalysisInfoHandler, args=(promoterId, cookie))
-    p5 = Process(target=PromoterVideoAnalysisTrend().PromoterVideoAnalysisTrendHandler, args=(promoterId, cookie))
-    p1.start()
-    p2.start()
-    p3.start()
-    p4.start()
-    p5.start()
-    return rollback
+
+    cookie = cookie_update.get_cookie_handler()[0]
+    phone_num = cookie_update.get_cookie_handler()[1]
+    roll = ReturnPromoterInfoSpider().PromoterInfoSpiderHandler(promoterId, cookie)
+
+    if json.loads(roll).__contains__("result"):
+        if json.loads(roll)["result"] == 100110000:
+            SendFeiShuMsg.send_cookie_robot_msg("cookie 永久失效 轻更新cookie 唯一电话为{phone_num}".format(phone_num=phone_num))
+            cookie_update.update_cookie_handler(phone_num)
+            retry_cookie = cookie_update.get_cookie_handler()[0]
+            roll = ReturnPromoterInfoSpider().PromoterInfoSpiderHandler(promoterId, retry_cookie)
+            p1 = Process(target=PromoterInfoSpider().PromoterInfoSpiderHandler, args=(promoterId, retry_cookie))
+            p2 = Process(target=PromoterFansInfo().PromoterFansInfoHandler, args=(promoterId, retry_cookie))
+            p3 = Process(target=PromoterLiveInfoSpider().PromoterLiveInfoSpiderHander, args=(promoterId, retry_cookie))
+            p4 = Process(target=PromoterVideoAnalysisInfo().PromoterVideoAnalysisInfoHandler,
+                         args=(promoterId, retry_cookie))
+            p5 = Process(target=PromoterVideoAnalysisTrend().PromoterVideoAnalysisTrendHandler,
+                         args=(promoterId, retry_cookie))
+            p1.start()
+            p2.start()
+            p3.start()
+            p4.start()
+            p5.start()
+            return roll
+
+        else:
+            SendFeiShuMsg.send_cookie_robot_msg("cookie 临时失效 唯一电话为{phone_num}".format(phone_num=phone_num))
+            cookie_update.update_temporary_cookie_handler(phone_num)
+            retry_cookie = cookie_update.get_cookie_handler()[0]
+            roll = ReturnPromoterInfoSpider().PromoterInfoSpiderHandler(promoterId, retry_cookie)
+            p1 = Process(target=PromoterInfoSpider().PromoterInfoSpiderHandler, args=(promoterId, retry_cookie))
+            p2 = Process(target=PromoterFansInfo().PromoterFansInfoHandler, args=(promoterId, retry_cookie))
+            p3 = Process(target=PromoterLiveInfoSpider().PromoterLiveInfoSpiderHander, args=(promoterId, retry_cookie))
+            p4 = Process(target=PromoterVideoAnalysisInfo().PromoterVideoAnalysisInfoHandler,
+                         args=(promoterId, retry_cookie))
+            p5 = Process(target=PromoterVideoAnalysisTrend().PromoterVideoAnalysisTrendHandler,
+                         args=(promoterId, retry_cookie))
+            p1.start()
+            p2.start()
+            p3.start()
+            p4.start()
+            p5.start()
+            return roll
+
+    else:
+        p1 = Process(target=PromoterInfoSpider().PromoterInfoSpiderHandler, args=(promoterId, cookie))
+        p2 = Process(target=PromoterFansInfo().PromoterFansInfoHandler, args=(promoterId, cookie))
+        p3 = Process(target=PromoterLiveInfoSpider().PromoterLiveInfoSpiderHander, args=(promoterId, cookie))
+        p4 = Process(target=PromoterVideoAnalysisInfo().PromoterVideoAnalysisInfoHandler, args=(promoterId, cookie))
+        p5 = Process(target=PromoterVideoAnalysisTrend().PromoterVideoAnalysisTrendHandler, args=(promoterId, cookie))
+        p1.start()
+        p2.start()
+        p3.start()
+        p4.start()
+        p5.start()
+        return roll
 
 
 if __name__ == '__main__':
-    app.run(port=9999, host=ConfConstant.URL, debug=True)
+    # app.run(port=9999, host=ConfConstant.URL, debug=True)
+    app.run(port=9999,debug=True)

+ 26 - 1
utils/send_feishu_msg.py

@@ -41,4 +41,29 @@ class SendFeiShuMsg:
                 logging.error("飞书通知发送失败,请检查!")
                 return False
         except Exception as e:
-            logging.error("send_robot_msg Error:{}".format(traceback.format_exc()))
+            logging.error("send_robot_msg Error:{}".format(traceback.format_exc()))
+
+    @staticmethod
+    def send_cookie_robot_msg(msg_content):
+        try:
+            url = 'https://open.feishu.cn/open-apis/bot/v2/hook/4c20b26f-6a58-4ebf-9754-80ab1b35387e'
+            req_head = {"Content-Type": "application/json"}
+            req_data = {
+                "msg_type": "text",
+                "content": {
+                    "text": msg_content
+                }
+            }
+            data = json.dumps(req_data)
+            logging.info("send_robot_msg req_url:{}, req_data:{}".format(url, data))
+            response = requests.post(url=url, data=data, headers=req_head)
+            result = response.json()
+            logging.info("send_robot_msg response:{}".format(response.json()))
+            if result.get("StatusCode") == 0:
+                logging.info("飞书通知发送成功")
+                return True
+            else:
+                logging.error("飞书通知发送失败,请检查!")
+                return False
+        except Exception as e:
+            logging.error("send_robot_msg Error:{}".format(traceback.format_exc()))