BytedanceMaterialRequest.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2022/2/9 6:01 下午
  4. # @Site :
  5. # @File : BytedanceMaterialRequest.py
  6. # @Software: PyCharm
  7. # @contact: renyupeng@c-top.com.cn
  8. # @Tel 1501435553
  9. import json
  10. import logging
  11. import random
  12. import requests
  13. from Apietl.conn.TidbConn import TidbConn
  14. from Apietl.utlis.Utils import Utils
  15. class BytedanceMaterialRequest:
  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}:8085/bytedance/bytedanceController/getBytedanceVideoByAccountId""".format(
  26. num=num)
  27. return url
  28. """切割数组长度"""
  29. @staticmethod
  30. def list_split(items, n):
  31. return [items[i:i + n] for i in range(0, len(items), n)]
  32. def getMaterialAdvertiser(self):
  33. try:
  34. sql = """
  35. SET GLOBAL group_concat_max_len=102400;
  36. select account_id,group_concat(distinct material_id) from application.bytedance_material_video_report_daily_dw
  37. where stat_datetime >= {start_time} and stat_datetime<={end_time}
  38. and signature is null
  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("getBytedanceVideoByAccountId Exception:{}.".format(e))
  45. return None
  46. def handler(self):
  47. results = self.getMaterialAdvertiser()
  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 = BytedanceMaterialRequest.list_split(material_ids, 50)
  59. for info in material_list:
  60. info = list(map(int, info))
  61. req_data = {"accountId": account_id, "materialIds": info}
  62. data = json.dumps(req_data)
  63. try:
  64. repo = requests.post(url=url, data=data, headers=headers)
  65. logging.info("post getBytedanceVideoByAccountId response:{}".format(repo))
  66. except Exception as e:
  67. logging.error("""post getBytedanceVideoByAccountId error {e}""".format(e=e))
  68. else:
  69. try:
  70. req_datas = {"accountId": account_id, "materialIds": list(map(int, material_ids))}
  71. datas = json.dumps(req_datas)
  72. repo = requests.post(url=url, data=datas, headers=headers)
  73. logging.info("post getBytedanceVideoByAccountId response:{}".format(repo))
  74. except Exception as e:
  75. logging.error("""post getBytedanceVideoByAccountId error {e}""".format(e=e))
  76. if __name__ == '__main__':
  77. BytedanceMaterialRequest().handler()