CommonFunction.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import requests
  2. from utils.UrlConfig import headers
  3. import json
  4. import pandas as pd
  5. import datetime
  6. from utils.DataBaseConfig import *
  7. import traceback
  8. from concurrent_log import ConcurrentTimedRotatingFileHandler
  9. import logging
  10. log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', '%m/%d/%Y %I:%M:%S %p')
  11. log_handler = ConcurrentTimedRotatingFileHandler("logs/commonfunction.log", when="midnight", backupCount=100)
  12. log_handler.setFormatter(log_formatter)
  13. logger = logging.getLogger('common_function_logger')
  14. logger.addHandler(log_handler)
  15. logger.setLevel(logging.DEBUG)
  16. print('id of common_function_logger %s' % id(logger))
  17. logger.info("common_function_logger started!")
  18. def get_lower_case_name(name):
  19. lst = []
  20. for index, char in enumerate(name):
  21. if char.isupper() and index != 0:
  22. lst.append("_")
  23. lst.append(char)
  24. return "".join(lst).lower()
  25. def get_app_list(account_id,url):
  26. request_data = {"accountId": account_id}
  27. logger.info("aiKuaiShouAppInfo/list the request_data is %s" % request_data)
  28. try:
  29. request = requests.post(url=url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  30. request_json = json.loads(request)
  31. if request_json["code"] != 0:
  32. logger.info("account_id=%s, the res of list is %s" % (account_id, request_json['message']))
  33. except:
  34. logger.error('account_id=%s, the res of list is %s, the traceback is %s ' %
  35. (account_id, request, traceback.format_exc()))
  36. return {"code": -1}
  37. return request_json
  38. def get_app_detail(account_id, url, app_id):
  39. request_data = {"accountId": account_id, "appId": app_id}
  40. logger.info("/getAppDetail the request_data is %s" % request_data)
  41. try:
  42. request = requests.post(url=url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  43. request_json = json.loads(request)
  44. if request_json["code"] != 0:
  45. logger.info("account_id=%s, appId=%s, the res of getAppDetail is %s" % (account_id, app_id, request_json['message']))
  46. except:
  47. logger.error('account_id=%s, appId=%s, the res of getAppDetail is %s, the traceback is %s ' %
  48. (account_id, app_id, request, traceback.format_exc()))
  49. return {"code": -1}
  50. return request_json
  51. def get_history_video_info(channel_type,account_id, url, start_time, end_time, video_cnt, related_creative_max_cnt, app_version=None):
  52. request_data = {"accountId": account_id,
  53. "startDate": start_time,
  54. "endDate": end_time,
  55. "num": video_cnt,
  56. "createCount": related_creative_max_cnt}
  57. if app_version:
  58. request_data['appVersion'] = app_version
  59. if channel_type != 2:
  60. request_data['channelType'] = channel_type
  61. try:
  62. logger.info("getHistoryVideoList the request_data is %s" % request_data)
  63. request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  64. request_json = json.loads(request)
  65. if request_json["code"] != 0:
  66. logger.info("account_id=%s, app_version=%s, the res of getHistoryVideoList is %s" %
  67. (account_id, app_version, request_json['message']))
  68. except Exception:
  69. logger.error('account_id=%s, app_version=%s, the res of getHistoryVideoList is %s, the traceback is %s ' %
  70. (account_id, app_version, request, traceback.format_exc()))
  71. return {"code": -1}
  72. return request_json
  73. def refresh_video(account_id, url):
  74. try:
  75. request = requests.post(url=url, headers=headers,
  76. data=json.JSONEncoder().encode({"accountId": account_id})).text
  77. request_json = json.loads(request)
  78. logger.info('account_id=%s, the res of getKuaiShouVideoList is %s' % (account_id, request_json))
  79. except Exception:
  80. logger.error('account_id=%s, the res of getKuaiShouVideoList is %s, traceback is %s ' %
  81. (account_id, request, traceback.format_exc()))
  82. #DONE
  83. def get_new_video_info(channel_type,account_id, url, app_version=None):
  84. request_data = {"accountId":account_id}
  85. if app_version:
  86. request_data["appVersion"] = app_version
  87. if channel_type != 2:
  88. request_data["channelType"] = channel_type
  89. try:
  90. logger.info("getNewVideoList the request_data is %s" % request_data)
  91. request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  92. request_json = json.loads(request)
  93. if request_json["code"] != 0:
  94. logger.info("account_id=%s, app_version=%s, the res of getNewVideoList is %s" %
  95. (account_id, app_version, request_json['message']))
  96. except Exception:
  97. logger.error('account_id=%s, app_version=%s, the res of getNewVideoList is %s, the traceback is %s ' %
  98. (account_id, app_version, request, traceback.format_exc()))
  99. return {"code": -1}
  100. return request_json
  101. #DONE
  102. def get_missing_video_info(channel_type,account_id, cnt, url, start_time, end_time, app_version=None):
  103. request_data = {"accountId": account_id, "videoCnt": cnt, "startTime": start_time, "endTime": end_time}
  104. if app_version:
  105. request_data['appVersion'] = app_version
  106. if channel_type != 2:
  107. request_data['channelType'] = channel_type
  108. try:
  109. logger.info("getCreateZeroVideoList the request_data is %s" % request_data)
  110. request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  111. request_json = json.loads(request)
  112. if request_json["code"] != 0:
  113. logger.info("account_id=%s, app_version=%s, the res of getCreateZeroVideoList is %s" %
  114. (account_id, app_version, request_json['message']))
  115. except Exception:
  116. logger.error('account_id=%s, app_version=%s, the res of getCreateZeroVideoList is %s, the traceback is %s ' %
  117. (account_id, app_version, request, traceback.format_exc()))
  118. return {"code": -1}
  119. return request_json
  120. # DONE
  121. def get_top_video_info(channel_type,account_id, cnt, start_time, end_time, url, app_version=None):
  122. request_data = {"accountId": account_id, "num": cnt, "startTime": start_time, "endTime": end_time}
  123. if app_version:
  124. request_data['appVersion'] = app_version
  125. if channel_type != 2:
  126. request_data['channelType'] = channel_type
  127. try:
  128. logger.info("getHistoryTopVideoList the request_data is %s" % request_data)
  129. request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  130. request_json = json.loads(request)
  131. if request_json["code"] != 0:
  132. logger.info("account_id=%s, app_version=%s, the res of getHistoryTopVideoList is %s" %
  133. (account_id, app_version, request_json['message']))
  134. except Exception:
  135. logger.error('account_id=%s, app_version=%s, the res of getHistoryTopVideoList is %s, the traceback is %s ' %
  136. (account_id, app_version, request, traceback.format_exc()))
  137. return {"code": -1}
  138. return request_json
  139. def get_campaign_and_group_name_rule(account_id):
  140. sql = """select campaign_name, group_name
  141. from ctop_ai_kuaishou_advertiser_strategy
  142. where account_id = %s
  143. order by create_time desc
  144. limit 1
  145. """ % account_id
  146. df = pd.read_sql(sql, engine)
  147. if df.empty:
  148. return None, None
  149. campaign_name = df['campaign_name'].values[0]
  150. group_name = df['group_name'].values[0]
  151. return campaign_name, group_name
  152. def get_request_data(account_id, campaign_id, video_info, campaign_name, group_name,
  153. ai_strategy_remark, name_replace='',
  154. unit_type=4, click_track_url=None, app_id=None, creative_name=None):
  155. res = {
  156. "video": video_info,
  157. "operation_type": 1,
  158. "account_id": account_id,
  159. "ai_strategy_remark": ai_strategy_remark,
  160. "campaign_info":
  161. {"campaign_id": campaign_id,
  162. "campaign_name": (campaign_name.replace('自定义', name_replace) + str(datetime.datetime.now()))
  163. if name_replace else campaign_name},
  164. 'group_info':
  165. {'group_name': (group_name.replace('自定义', name_replace) + str(datetime.datetime.now()))
  166. if name_replace else (group_name + str(datetime.datetime.now())),
  167. 'begin_time': str(datetime.date.today()),
  168. 'unit_type': unit_type,
  169. 'app_id': app_id},
  170. 'creative_info': {'is_sticky': 0,
  171. 'click_track_url': click_track_url,
  172. 'creative_name': creative_name}
  173. }
  174. return res