CommonFunction.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import requests
  2. from utils.UrlConfig import headers, refresh_video_url
  3. import json
  4. import pandas as pd
  5. import datetime
  6. from utils.DataBaseConfig import *
  7. from ai_time_task_creative_handler import logger
  8. import traceback
  9. def get_lower_case_name(name):
  10. lst = []
  11. for index, char in enumerate(name):
  12. if char.isupper() and index != 0:
  13. lst.append("_")
  14. lst.append(char)
  15. return "".join(lst).lower()
  16. def get_app_detail(account_id, url, app_id):
  17. """
  18. 获取应用详情
  19. url:http://192.168.1.8:8080/jeecg-boot/ai/aiKuaiShouAppInfo/getAppDetail
  20. 请求方式:post
  21. 入参:accountId 账户id
  22. appId 应用id
  23. 返回参数:
  24. {
  25. "code": 0,
  26. "data": {
  27. "id": 413,
  28. "accountId": 9864909,
  29. "appVersion": "ksjyrlyn-人类一脑涂地大败",
  30. "appName": "233乐园",
  31. "packageName": "com.meta.box",
  32. "platform": 1,
  33. "url": "https://www.233leyuan.com/apiserv/api/deliveryTest/ABTest?id=ksjyrlyn",
  34. "useSdk": 0,
  35. "appPrivacyUrl": "https://webcdn.233leyuan.com/app/useragreement/metax/privacyagreement.html",
  36. "trackUrl": "https://lnk0.com/kYZxds?chn=451&imei_md5=__IMEI2__&androidid_md5_1=__ANDROIDID2__&oaid=__OAID__&adcampaign=__DNAME__&adcreative=__CID__&adgroup=__AID__&did=__DID__&accountid=__ACCOUNTID__&platform=__OS__&clicktime=__TS__&ip=__IP__&useragent=__UA__&channelapp=__CSITE__&callback_url=__CALLBACK__&action=none",
  37. "appMd5": "0b2314c738956dc159ed38dc584ef214",
  38. "imageToken": "marketb3e98f5f68a74870b8d8d814499d6189.jpg",
  39. "status": 2,
  40. "remark": "创建应用成功",
  41. "appId": 3543665,
  42. "createTime": "2021-02-03 16:40:53",
  43. "updateTime": "2021-02-03 16:42:52"},
  44. "message": "success"
  45. }
  46. """
  47. request_data = {"accountId": account_id, "appId": app_id}
  48. try:
  49. request = requests.post(url=url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  50. request_json = json.loads(request)
  51. if request_json["code"] != 0:
  52. logger.info("account_id=%s, appId=%s, the res of getAppDetail is %s" % (account_id, app_id, request_json['message']))
  53. except:
  54. logger.error('account_id=%s, appId=%s, the res of getAppDetail is %s, the traceback is %s ' %
  55. (account_id, app_id, request, traceback.format_exc()))
  56. return {"code": -1}
  57. return request_json
  58. def get_history_video_info(account_id, url, start_time, end_time, video_cnt, related_creative_max_cnt, app_version=None):
  59. """
  60. url:http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getHistoryVideoList
  61. 请求方式:POST
  62. remark: 见入参
  63. 入参:accountId 账户id
  64. startDate 开始时间 --(视频的upload_time)
  65. endDate 结束时间 --(视频的upload_time)
  66. createCount 关联创意数 (小于该值)
  67. num 获取数量 (随机num个)
  68. """
  69. if not app_version:
  70. request_data = {"accountId": account_id,
  71. "startDate": start_time,
  72. "endDate": end_time,
  73. "num": video_cnt,
  74. "createCount": related_creative_max_cnt}
  75. else:
  76. request_data = {"accountId": account_id,
  77. "startDate": start_time,
  78. "endDate": end_time,
  79. "num": video_cnt,
  80. "createCount": related_creative_max_cnt,
  81. "appVersion": app_version}
  82. try:
  83. request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  84. request_json = json.loads(request)
  85. if request_json["code"] != 0:
  86. logger.info("account_id=%s, app_version=%s, the res of getHistoryVideoList is %s" %
  87. (account_id, app_version, request_json['message']))
  88. except Exception:
  89. logger.error('account_id=%s, app_version=%s, the res of getHistoryVideoList is %s, the traceback is %s ' %
  90. (account_id, app_version, request, traceback.format_exc()))
  91. return {"code": -1}
  92. return request_json
  93. def get_new_video_info(account_id, url, app_version=None):
  94. """
  95. url:http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getNewVideoList
  96. 请求方式:POST
  97. remark:
  98. 1、返回 upload_time 在上次调用时间到本次调用时间内的素材(第一次调用返回5分钟前到本次调用时间的素材)
  99. 2、需要调用刷新快手素材的接口 http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getKuaiShouVideoList
  100. (请求方式 POST 入参:accountId 返回结果:成功或失败)
  101. 入参:accountId
  102. """
  103. try:
  104. request = requests.post(url=refresh_video_url, headers=headers,
  105. data=json.JSONEncoder().encode({"accountId": account_id})).text
  106. _ = json.loads(request)
  107. except Exception:
  108. logger.error('account_id=%s, app_version=%s, the res of getKuaiShouVideoList is %s, traceback is %s ' %
  109. (account_id, app_version, request, traceback.format_exc()))
  110. if not app_version:
  111. request_data = {"accountId": account_id}
  112. else:
  113. request_data = {"accountId": account_id, 'appVersion': app_version}
  114. try:
  115. request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  116. request_json = json.loads(request)
  117. if request_json["code"] != 0:
  118. logger.info("account_id=%s, app_version=%s, the res of getNewVideoList is %s" %
  119. (account_id, app_version, request_json['message']))
  120. except Exception:
  121. logger.error('account_id=%s, app_version=%s, the res of getNewVideoList is %s, the traceback is %s ' %
  122. (account_id, app_version, request, traceback.format_exc()))
  123. return {"code": -1}
  124. return request_json
  125. def get_missing_video_info(account_id, cnt, url, start_time, end_time, app_version=None):
  126. """
  127. url:"http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getCreateZeroVideoList"
  128. 请求方式:POST
  129. remark: 视频的upload_time 在startTime 和 endTime 之间,创意关联个数为0,order by upload_time asc limit videoCnt
  130. 入参:
  131. accountId: 账户id
  132. videoCnt: 查询数量
  133. startTime: 开始时间
  134. endTime: 结束时间
  135. """
  136. if not app_version:
  137. request_data = {"accountId": account_id, "videoCnt": cnt, "startTime": start_time, "endTime": end_time}
  138. else:
  139. request_data = {"accountId": account_id, "videoCnt": cnt, "startTime": start_time, "endTime": end_time,
  140. 'appVersion': app_version}
  141. try:
  142. request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  143. request_json = json.loads(request)
  144. if request_json["code"] != 0:
  145. logger.info("account_id=%s, app_version=%s, the res of getCreateZeroVideoList is %s" %
  146. (account_id, app_version, request_json['message']))
  147. except Exception:
  148. logger.error('account_id=%s, app_version=%s, the res of getCreateZeroVideoList is %s, the traceback is %s ' %
  149. (account_id, app_version, request, traceback.format_exc()))
  150. return {"code": -1}
  151. return request_json
  152. def get_top_video_info(account_id, cnt, start_time, end_time, url, app_version=None):
  153. """
  154. url:http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getHistoryTopVideoList
  155. 请求方式:POST
  156. remark: ctop_kuaishou_report_daily_material
  157. 整个项目维度 stat_date 在 startTime 和 endTime, order by sum(charge) desc limit num
  158. 入参:
  159. accountId: 账户id
  160. startTime: 开始时间
  161. endTime: 结束时间
  162. num: 查询数量
  163. 返回:
  164. {
  165. "code": 0,
  166. "data": [
  167. {
  168. "charge": 40871.123,
  169. "video_url": "&di=811c96e5&bp=13890",
  170. "photo_id": "5254011957394409468",
  171. "signature": "4af22090cddc32773c7707de3c652f77",
  172. "channel_type": 0,
  173. "material_type":1, --1:竖版 2:横版
  174. "imageList": ["09f61ca7174160c12d9a68306049ca82","46996bea84287692b3884686a52903ed"]
  175. }
  176. ],
  177. "message": "SUCCESS"
  178. """
  179. if not app_version:
  180. request_data = {"accountId": account_id, "num": cnt, "startTime": start_time, "endTime": end_time}
  181. else:
  182. request_data = {"accountId": account_id, "num": cnt, "startTime": start_time, "endTime": end_time,
  183. 'appVersion': app_version}
  184. try:
  185. request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  186. request_json = json.loads(request)
  187. if request_json["code"] != 0:
  188. logger.info("account_id=%s, app_version=%s, the res of getHistoryTopVideoList is %s" %
  189. (account_id, app_version, request_json['message']))
  190. except Exception:
  191. logger.error('account_id=%s, app_version=%s, the res of getHistoryTopVideoList is %s, the traceback is %s ' %
  192. (account_id, app_version, request, traceback.format_exc()))
  193. return {"code": -1}
  194. return request_json
  195. def get_campaign_and_group_name_rule(account_id):
  196. sql = """select campaign_name, group_name
  197. from ctop_ai_kuaishou_advertiser_strategy
  198. where account_id = %s
  199. order by create_time desc
  200. limit 1
  201. """ % account_id
  202. df = pd.read_sql(sql, engine)
  203. if df.empty:
  204. return None, None
  205. campaign_name = df['campaign_name'].values[0]
  206. group_name = df['group_name'].values[0]
  207. return campaign_name, group_name
  208. def get_request_data(account_id, campaign_id, video_info, campaign_name, group_name,
  209. ai_strategy_remark, name_replace='',
  210. unit_type=4, click_track_url=None, app_id=None):
  211. res = {
  212. "video": video_info,
  213. "operation_type": 1,
  214. "account_id": account_id,
  215. "ai_strategy_remark": ai_strategy_remark,
  216. "campaign_info":
  217. {"campaign_id": campaign_id,
  218. "campaign_name": (campaign_name.replace('自定义', name_replace) + str(datetime.datetime.now()))
  219. if name_replace else campaign_name},
  220. 'group_info':
  221. {'group_name': (group_name.replace('自定义', name_replace) + str(datetime.datetime.now()))
  222. if name_replace else (group_name + str(datetime.datetime.now())),
  223. 'begin_time': str(datetime.date.today()),
  224. 'unit_type': unit_type,
  225. 'app_id': app_id},
  226. 'creative_info': {'is_sticky': 0,
  227. 'click_track_url': click_track_url}
  228. }
  229. return res