CommonFunction.py 11 KB

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