renyupeng пре 2 година
родитељ
комит
e03ee8f29a
1 измењених фајлова са 40 додато и 34 уклоњено
  1. 40 34
      spider/PromoterVideoAnalysisTrend.py

+ 40 - 34
spider/PromoterVideoAnalysisTrend.py

@@ -12,6 +12,7 @@ import json
 
 import requests
 
+from utils.cookie_update import cookie_update
 from utils.mysql_helper import insert, batch_insert
 from utils.mysql_utils import MysqlUtils
 from utils.send_feishu_msg import SendFeiShuMsg
@@ -33,40 +34,45 @@ class PromoterVideoAnalysisTrend:
                     .format(timeRangeType=i, promoterId=promoterId)
                 rep = requests.get(url=url, headers=headers)
                 table_name = 'kwai_promoter_video_analyse_info'
-                data = json.loads(rep.text)["data"]
-                promoter_video_analyse_item = {"avgCommentCount": data["avgCommentCount"],
-                                               "avgLikeCount": data["avgLikeCount"],
-                                               "avgShareCount": data["avgShareCount"],
-                                               "avgVideoWatchCount": data["avgVideoWatchCount"],
-                                               "maxCommentCount": data["maxCommentCount"],
-                                               "maxLikeCount": data["maxLikeCount"],
-                                               "maxShareCount": data["maxShareCount"],
-                                               "maxVideoWatchCount": data["maxVideoWatchCount"],
-                                               "minCommentCount": data["minCommentCount"],
-                                               "minLikeCount": data["minLikeCount"],
-                                               "minShareCount": data["minShareCount"],
-                                               "minVideoWatchCount": data["minVideoWatchCount"],
-                                               "promoterId": data["promoterId"],
-                                               "timeRangeType": i,
-                                               "trend": json.dumps(data["trend"]),
-                                               }
-                insert(table_name=table_name, item=promoter_video_analyse_item)
-                if i == 3:
-                    trend_name = 'kwai_promoter_video_report_analyse_info'
-                    trends_list = tuple(data["trend"])
-                    trend_list = []
-                    if len(trends_list) == 0:
-                        SendFeiShuMsg.send_robot_msg("达人{promoterId}视频数为零".format(promoterId=promoterId))
-                    else:
-                        for num in range(len(trends_list)):
-                            trend = trends_list[num]
-                            analyse_trend = {"date": trend["date"], "promoterId": promoterId,
-                                             "commentCount": trend["commentCount"], "likeCount": trend["likeCount"],
-                                             "shareCount": trend["shareCount"],
-                                             "videoWatchCount": trend["videoWatchCount"]}
-                            trend_list.append(analyse_trend)
-                        batch_insert(table_name=trend_name, item_list=trend_list)
+                if 'token过期或错误' in json.loads(rep.text)["msg"]:
+                    data = json.loads(rep.text)["data"]
+                    promoter_video_analyse_item = {"avgCommentCount": data["avgCommentCount"],
+                                                   "avgLikeCount": data["avgLikeCount"],
+                                                   "avgShareCount": data["avgShareCount"],
+                                                   "avgVideoWatchCount": data["avgVideoWatchCount"],
+                                                   "maxCommentCount": data["maxCommentCount"],
+                                                   "maxLikeCount": data["maxLikeCount"],
+                                                   "maxShareCount": data["maxShareCount"],
+                                                   "maxVideoWatchCount": data["maxVideoWatchCount"],
+                                                   "minCommentCount": data["minCommentCount"],
+                                                   "minLikeCount": data["minLikeCount"],
+                                                   "minShareCount": data["minShareCount"],
+                                                   "minVideoWatchCount": data["minVideoWatchCount"],
+                                                   "promoterId": data["promoterId"],
+                                                   "timeRangeType": i,
+                                                   "trend": json.dumps(data["trend"]),
+                                                   }
+                    insert(table_name=table_name, item=promoter_video_analyse_item)
+                    if i == 3:
+                        trend_name = 'kwai_promoter_video_report_analyse_info'
+                        trends_list = tuple(data["trend"])
+                        trend_list = []
+                        if len(trends_list) == 0:
+                            SendFeiShuMsg.send_robot_msg("达人{promoterId}视频数为零".format(promoterId=promoterId))
+                        else:
+                            for num in range(len(trends_list)):
+                                trend = trends_list[num]
+                                analyse_trend = {"date": trend["date"], "promoterId": promoterId,
+                                                 "commentCount": trend["commentCount"], "likeCount": trend["likeCount"],
+                                                 "shareCount": trend["shareCount"],
+                                                 "videoWatchCount": trend["videoWatchCount"]}
+                                trend_list.append(analyse_trend)
+                            batch_insert(table_name=trend_name, item_list=trend_list)
+                else:
+                    SendFeiShuMsg.send_robot_msg(
+                        '{promoterId},{msg}'.format(promoterId=promoterId, msg=json.loads(rep.text)["msg"]))
 
         except Exception as e:
             SendFeiShuMsg.send_robot_msg(
-                'PromoterVideoAnalysisTrend  {promoterId}请求错误请检查cookie{e}'.format(promoterId=promoterId, e=e))
+                'PromoterVideoAnalysisTrend  {promoterId}请求错误请检查cookie{e}'.
+                    format(promoterId=promoterId, e=e))