""" Author renyupeng coding=utf-8 @Time : 2023/2/9 11:23 上午 @Site : @File : PromoterVideoAnalysisTrend.py @Software: PyCharm @contact: renyupeng@c-top.com.cn @Tel 1501435553 """ import json import requests from utils.mysql_helper import insert, batch_insert from utils.mysql_utils import MysqlUtils from utils.send_feishu_msg import SendFeiShuMsg class PromoterVideoAnalysisTrend: def __init__(self): self.conn = MysqlUtils() self.list = [1, 2, 3] def PromoterVideoAnalysisTrendHandler(self, promoterId): sql = "select cookie from ruixuan.kuaishou_supply_chain_cookie" cookie = self.conn.QueryOne(sql)[0] headers = {'User-Agent': 'Mozilla/5.0', 'Cookie': cookie} try: for i in self.list: url = "https://cps.kwaixiaodian.com/gateway/distribute/platform/seller/promoter/video/analysis/trend?" \ "promoterId={promoterId}&timeRangeType={timeRangeType}" \ .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 = [] 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) except Exception as e: SendFeiShuMsg.send_robot_msg('请求错误请检查cookie{e}'.format(e=e))