PromoterVideoAnalysisTrend.py 3.5 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):
  21. sql = "select cookie from ruixuan.kuaishou_supply_chain_cookie"
  22. cookie = self.conn.QueryOne(sql)[0]
  23. headers = {'User-Agent': 'Mozilla/5.0',
  24. 'Cookie': cookie}
  25. try:
  26. for i in self.list:
  27. url = "https://cps.kwaixiaodian.com/gateway/distribute/platform/seller/promoter/video/analysis/trend?" \
  28. "promoterId={promoterId}&timeRangeType={timeRangeType}" \
  29. .format(timeRangeType=i, promoterId=promoterId)
  30. rep = requests.get(url=url, headers=headers)
  31. table_name = 'kwai_promoter_video_analyse_info'
  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. for num in range(len(trends_list)):
  55. trend = trends_list[num]
  56. analyse_trend = {"date": trend["date"], "promoterId": promoterId,
  57. "commentCount": trend["commentCount"], "likeCount": trend["likeCount"],
  58. "shareCount": trend["shareCount"], "videoWatchCount": trend["videoWatchCount"]}
  59. trend_list.append(analyse_trend)
  60. batch_insert(table_name=trend_name, item_list=trend_list)
  61. except Exception as e:
  62. SendFeiShuMsg.send_robot_msg('请求错误请检查cookie{e}'.format(e=e))