PromoterVideoAnalysisTrend.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. 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. try:
  30. table_name = 'kwai_promoter_video_analyse_info'
  31. data = json.loads(rep.text)["data"]
  32. promoter_video_analyse_item = {"avgCommentCount": data["avgCommentCount"],
  33. "avgLikeCount": data["avgLikeCount"],
  34. "avgShareCount": data["avgShareCount"],
  35. "avgVideoWatchCount": data["avgVideoWatchCount"],
  36. "maxCommentCount": data["maxCommentCount"],
  37. "maxLikeCount": data["maxLikeCount"],
  38. "maxShareCount": data["maxShareCount"],
  39. "maxVideoWatchCount": data["maxVideoWatchCount"],
  40. "minCommentCount": data["minCommentCount"],
  41. "minLikeCount": data["minLikeCount"],
  42. "minShareCount": data["minShareCount"],
  43. "minVideoWatchCount": data["minVideoWatchCount"],
  44. "promoterId": data["promoterId"],
  45. "timeRangeType": i,
  46. "trend": json.dumps(data["trend"]),
  47. }
  48. insert(table_name=table_name, item=promoter_video_analyse_item)
  49. if i == 3:
  50. trend_name = 'kwai_promoter_video_report_analyse_info'
  51. trends_list = tuple(data["trend"])
  52. trend_list = []
  53. if len(trends_list) == 0:
  54. SendFeiShuMsg.send_robot_msg("达人{promoterId}视频数为零".format(promoterId=promoterId))
  55. else:
  56. for num in range(len(trends_list)):
  57. trend = trends_list[num]
  58. analyse_trend = {"date": trend["date"], "promoterId": promoterId,
  59. "commentCount": trend["commentCount"], "likeCount": trend["likeCount"],
  60. "shareCount": trend["shareCount"],
  61. "videoWatchCount": trend["videoWatchCount"]}
  62. trend_list.append(analyse_trend)
  63. batch_insert(table_name=trend_name, item_list=trend_list)
  64. except Exception as e:
  65. SendFeiShuMsg.send_robot_msg(
  66. 'PromoterVideoAnalysisTrend {promoterId}请求错误请检查cookie{e}'.
  67. format(promoterId=promoterId, e=json.loads(rep.text)["msg"]))