PromoterVideoAnalysisTrend.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """
  2. Author renyupeng
  3. coding=utf-8
  4. @Time : 2023/2/9 11:23 上午
  5. @Site :
  6. @File : PromoterVideoAnalysisTrend.py
  7. @Software: PyCharm
  8. @contact: renyupeng@c-top.com.cn
  9. @Tel 1501435553
  10. """
  11. import json
  12. import requests
  13. from utils.mysql_helper import insert, batch_insert
  14. from utils.mysql_utils import MysqlUtils
  15. from utils.send_feishu_msg import SendFeiShuMsg
  16. class PromoterVideoAnalysisTrend:
  17. def __init__(self):
  18. self.conn = MysqlUtils()
  19. self.list = [1, 2, 3]
  20. def PromoterVideoAnalysisTrendHandler(self, promoterId, cookie):
  21. headers = {'User-Agent': 'Mozilla/5.0',
  22. 'Cookie': cookie}
  23. try:
  24. for i in self.list:
  25. url = "https://cps.kwaixiaodian.com/gateway/distribute/platform/seller/promoter/video/analysis/trend?" \
  26. "promoterId={promoterId}&timeRangeType={timeRangeType}" \
  27. .format(timeRangeType=i, promoterId=promoterId)
  28. rep = requests.get(url=url, headers=headers)
  29. table_name = 'kwai_promoter_video_analyse_info'
  30. data = json.loads(rep.text)["data"]
  31. promoter_video_analyse_item = {"avgCommentCount": data["avgCommentCount"],
  32. "avgLikeCount": data["avgLikeCount"],
  33. "avgShareCount": data["avgShareCount"],
  34. "avgVideoWatchCount": data["avgVideoWatchCount"],
  35. "maxCommentCount": data["maxCommentCount"],
  36. "maxLikeCount": data["maxLikeCount"],
  37. "maxShareCount": data["maxShareCount"],
  38. "maxVideoWatchCount": data["maxVideoWatchCount"],
  39. "minCommentCount": data["minCommentCount"],
  40. "minLikeCount": data["minLikeCount"],
  41. "minShareCount": data["minShareCount"],
  42. "minVideoWatchCount": data["minVideoWatchCount"],
  43. "promoterId": data["promoterId"],
  44. "timeRangeType": i,
  45. "trend": json.dumps(data["trend"]),
  46. }
  47. insert(table_name=table_name, item=promoter_video_analyse_item)
  48. if i == 3:
  49. trend_name = 'kwai_promoter_video_report_analyse_info'
  50. trends_list = tuple(data["trend"])
  51. trend_list = []
  52. if len(trends_list) == 0:
  53. SendFeiShuMsg.send_robot_msg("达人{promoterId}视频数为零".format(promoterId=promoterId))
  54. else:
  55. for num in range(len(trends_list)):
  56. trend = trends_list[num]
  57. analyse_trend = {"date": trend["date"], "promoterId": promoterId,
  58. "commentCount": trend["commentCount"], "likeCount": trend["likeCount"],
  59. "shareCount": trend["shareCount"],
  60. "videoWatchCount": trend["videoWatchCount"]}
  61. trend_list.append(analyse_trend)
  62. batch_insert(table_name=trend_name, item_list=trend_list)
  63. except Exception as e:
  64. SendFeiShuMsg.send_robot_msg(
  65. 'PromoterVideoAnalysisTrend {promoterId}请求错误请检查cookie{e}'.format(promoterId=promoterId, e=e))