KuaishouMaterialRequest.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2022/2/9 6:01 下午
  4. # @Site :
  5. # @File : KuaishouMaterialRequest.py
  6. # @Software: PyCharm
  7. # @contact: renyupeng@c-top.com.cn
  8. # @Tel 1501435553
  9. import json
  10. import logging
  11. import random
  12. from Apietl.conn.TidbConn import TidbConn
  13. from Apietl.utlis.Utils import Utils
  14. import requests
  15. class KuaishouMaterialRequest:
  16. def __init__(self):
  17. self.conn = TidbConn.get_connection()
  18. self.parm = Utils.get_args()
  19. self.start_time = self.parm[0]
  20. self.end_time = self.parm[1]
  21. """随机获取一个服务器ip"""
  22. def range_hosts(self):
  23. numList = [201, 204]
  24. num = random.sample(numList, 1)[0]
  25. url = """http://192.168.0.{num}:8083/kuaishou/kuaishouController/getKuaishouVideoByAccountId""".format(num=num)
  26. return url
  27. """切割数组长度"""
  28. @staticmethod
  29. def list_split(items, n):
  30. return [items[i:i + n] for i in range(0, len(items), n)]
  31. """获取缺失视频id的账户"""
  32. def getMaterialAccount(self):
  33. try:
  34. sql = """
  35. SET GLOBAL group_concat_max_len=102400;
  36. select account_id,group_concat(distinct photo_id) from application.kuaishou_material_video_report_daily_dw
  37. where stat_date >= {start_time} and stat_date<={end_time}
  38. and signature is null and photo_id!=0
  39. group by account_id
  40. """.format(start_time=self.start_time, end_time=self.end_time)
  41. results = TidbConn.exec_sql(self.conn, sql)
  42. return results
  43. except Exception as e:
  44. logging.error("KuaishouMaterialRequest Exception:{}.".format(e))
  45. return None
  46. def handler(self):
  47. results = self.getMaterialAccount()
  48. headers = {
  49. 'Content-Type': 'application/json'
  50. }
  51. for result in results:
  52. url = self.range_hosts()
  53. account_id = result[0]
  54. # print(account_id)
  55. material_id = result[1]
  56. material_ids = material_id.split(',')
  57. if len(material_ids) >= 100:
  58. material_list = KuaishouMaterialRequest.list_split(material_ids, 50)
  59. for info in material_list:
  60. info = list(info)
  61. req_data = {"accountId": account_id, "photoIds": info}
  62. print(11111,req_data)
  63. data = json.dumps(req_data)
  64. try:
  65. repo = requests.post(url=url, data=data, headers=headers)
  66. print(22222,repo)
  67. logging.info("post getKuaishouVideoByAccountId response:{}".format(repo))
  68. except Exception as e:
  69. logging.error("""post getKuaishouVideoByAccountId error {e}""".format(e=e))
  70. else:
  71. try:
  72. req_datas = {"accountId": account_id, "photoIds": list(material_ids)}
  73. print(333333,req_datas)
  74. datas = json.dumps(req_datas)
  75. repo = requests.post(url=url, data=datas, headers=headers)
  76. print(44444,repo)
  77. logging.info("post getKuaishouVideoByAccountId response:{}".format(repo))
  78. except Exception as e:
  79. logging.error("""post getKuaishouVideoByAccountId error {e}""".format(e=e))
  80. # print(material_ids)
  81. if __name__ == '__main__':
  82. KuaishouMaterialRequest().handler()