# Author renyupeng # coding=utf-8 # @Time : 2022/2/9 6:01 下午 # @Site : # @File : BytedanceMaterialRequest.py # @Software: PyCharm # @contact: renyupeng@c-top.com.cn # @Tel 1501435553 import json import logging import random import requests from Apietl.conn.TidbConn import TidbConn from Apietl.utlis.Utils import Utils class BytedanceMaterialRequest: def __init__(self): self.conn = TidbConn.get_connection() self.parm = Utils.get_args() self.start_time = self.parm[0] self.end_time = self.parm[1] """随机获取一个服务器ip""" def range_hosts(self): numList = [201, 204] num = random.sample(numList, 1)[0] url = """http://192.168.0.{num}:8085/bytedance/bytedanceController/getBytedanceVideoByAccountId""".format( num=num) return url """切割数组长度""" @staticmethod def list_split(items, n): return [items[i:i + n] for i in range(0, len(items), n)] def getMaterialAdvertiser(self): try: sql = """ SET GLOBAL group_concat_max_len=102400; select account_id,group_concat(distinct material_id) from application.bytedance_material_video_report_daily_dw where stat_datetime >= {start_time} and stat_datetime<={end_time} and signature is null group by account_id """.format(start_time=self.start_time, end_time=self.end_time) results = TidbConn.exec_sql(self.conn, sql) return results except Exception as e: logging.error("getBytedanceVideoByAccountId Exception:{}.".format(e)) return None def handler(self): results = self.getMaterialAdvertiser() headers = { 'Content-Type': 'application/json' } for result in results: url = self.range_hosts() account_id = result[0] # print(account_id) material_id = result[1] material_ids = material_id.split(',') if len(material_ids) >= 100: material_list = BytedanceMaterialRequest.list_split(material_ids, 50) for info in material_list: info = list(map(int, info)) req_data = {"accountId": account_id, "materialIds": info} data = json.dumps(req_data) try: repo = requests.post(url=url, data=data, headers=headers) logging.info("post getBytedanceVideoByAccountId response:{}".format(repo)) except Exception as e: logging.error("""post getBytedanceVideoByAccountId error {e}""".format(e=e)) else: try: req_datas = {"accountId": account_id, "materialIds": list(map(int, material_ids))} datas = json.dumps(req_datas) repo = requests.post(url=url, data=datas, headers=headers) logging.info("post getBytedanceVideoByAccountId response:{}".format(repo)) except Exception as e: logging.error("""post getBytedanceVideoByAccountId error {e}""".format(e=e)) if __name__ == '__main__': BytedanceMaterialRequest().handler()