PromoterVideoAnalysisTrend.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.cookie_update import cookie_update
  14. from utils.mysql_helper import insert, batch_insert
  15. from utils.mysql_utils import MysqlUtils
  16. from utils.send_feishu_msg import SendFeiShuMsg
  17. class PromoterVideoAnalysisTrend:
  18. def __init__(self):
  19. self.conn = MysqlUtils()
  20. self.list = [1, 2, 3]
  21. def PromoterVideoAnalysisTrendHandler(self, promoterId, cookie):
  22. headers = {'User-Agent': 'Mozilla/5.0',
  23. 'Cookie': cookie}
  24. try:
  25. for i in self.list:
  26. url = "https://cps.kwaixiaodian.com/gateway/distribute/platform/seller/promoter/video/analysis/trend?" \
  27. "promoterId={promoterId}&timeRangeType={timeRangeType}" \
  28. .format(timeRangeType=i, promoterId=promoterId)
  29. rep = requests.get(url=url, headers=headers)
  30. table_name = 'kwai_promoter_video_analyse_info'
  31. if 'token过期或错误' in json.loads(rep.text)["msg"]:
  32. data = json.loads(rep.text)["data"]
  33. promoter_video_analyse_item = {"avgCommentCount": data["avgCommentCount"],
  34. "avgLikeCount": data["avgLikeCount"],
  35. "avgShareCount": data["avgShareCount"],
  36. "avgVideoWatchCount": data["avgVideoWatchCount"],
  37. "maxCommentCount": data["maxCommentCount"],
  38. "maxLikeCount": data["maxLikeCount"],
  39. "maxShareCount": data["maxShareCount"],
  40. "maxVideoWatchCount": data["maxVideoWatchCount"],
  41. "minCommentCount": data["minCommentCount"],
  42. "minLikeCount": data["minLikeCount"],
  43. "minShareCount": data["minShareCount"],
  44. "minVideoWatchCount": data["minVideoWatchCount"],
  45. "promoterId": data["promoterId"],
  46. "timeRangeType": i,
  47. "trend": json.dumps(data["trend"]),
  48. }
  49. insert(table_name=table_name, item=promoter_video_analyse_item)
  50. if i == 3:
  51. trend_name = 'kwai_promoter_video_report_analyse_info'
  52. trends_list = tuple(data["trend"])
  53. trend_list = []
  54. if len(trends_list) == 0:
  55. SendFeiShuMsg.send_robot_msg("达人{promoterId}视频数为零".format(promoterId=promoterId))
  56. else:
  57. for num in range(len(trends_list)):
  58. trend = trends_list[num]
  59. analyse_trend = {"date": trend["date"], "promoterId": promoterId,
  60. "commentCount": trend["commentCount"], "likeCount": trend["likeCount"],
  61. "shareCount": trend["shareCount"],
  62. "videoWatchCount": trend["videoWatchCount"]}
  63. trend_list.append(analyse_trend)
  64. batch_insert(table_name=trend_name, item_list=trend_list)
  65. else:
  66. SendFeiShuMsg.send_robot_msg(
  67. '{promoterId},{msg}'.format(promoterId=promoterId, msg=json.loads(rep.text)["msg"]))
  68. except Exception as e:
  69. SendFeiShuMsg.send_robot_msg(
  70. 'PromoterVideoAnalysisTrend {promoterId}请求错误请检查cookie{e}'.
  71. format(promoterId=promoterId, e=e))