CommonFunction.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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_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 refresh_video(account_id, url):
  103. """
  104. 需要调用刷新快手素材的接口 http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getKuaiShouVideoList
  105. (请求方式 POST 入参:accountId 返回结果:成功或失败)
  106. remark: 自动上新之前,需要调用该接口,注意是账户层级的调用,不是app_id层级的调用
  107. """
  108. try:
  109. request = requests.post(url=url, headers=headers,
  110. data=json.JSONEncoder().encode({"accountId": account_id})).text
  111. request_json = json.loads(request)
  112. logger.info('account_id=%s, the res of getKuaiShouVideoList is %s' % (account_id, request_json))
  113. except Exception:
  114. logger.error('account_id=%s, the res of getKuaiShouVideoList is %s, traceback is %s ' %
  115. (account_id, request, traceback.format_exc()))
  116. def get_new_video_info(account_id, url, app_version=None):
  117. """
  118. url:http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getNewVideoList
  119. 请求方式:POST
  120. remark:
  121. 1、返回 upload_time 在上次调用时间到本次调用时间内的素材(第一次调用返回5分钟前到本次调用时间的素材)
  122. 入参:accountId
  123. """
  124. if not app_version:
  125. request_data = {"accountId": account_id}
  126. else:
  127. request_data = {"accountId": account_id, 'appVersion': app_version}
  128. try:
  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 getNewVideoList 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 getNewVideoList 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_missing_video_info(account_id, cnt, url, start_time, end_time, app_version=None):
  140. """
  141. url:"http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getCreateZeroVideoList"
  142. 请求方式:POST
  143. remark: 视频的upload_time 在startTime 和 endTime 之间,创意关联个数为0,order by upload_time asc limit videoCnt
  144. 入参:
  145. accountId: 账户id
  146. videoCnt: 查询数量
  147. startTime: 开始时间
  148. endTime: 结束时间
  149. """
  150. if not app_version:
  151. request_data = {"accountId": account_id, "videoCnt": cnt, "startTime": start_time, "endTime": end_time}
  152. else:
  153. request_data = {"accountId": account_id, "videoCnt": cnt, "startTime": start_time, "endTime": end_time,
  154. 'appVersion': app_version}
  155. try:
  156. request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  157. request_json = json.loads(request)
  158. if request_json["code"] != 0:
  159. logger.info("account_id=%s, app_version=%s, the res of getCreateZeroVideoList is %s" %
  160. (account_id, app_version, request_json['message']))
  161. except Exception:
  162. logger.error('account_id=%s, app_version=%s, the res of getCreateZeroVideoList is %s, the traceback is %s ' %
  163. (account_id, app_version, request, traceback.format_exc()))
  164. return {"code": -1}
  165. return request_json
  166. def get_top_video_info(account_id, cnt, start_time, end_time, url, app_version=None):
  167. """
  168. url:http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getHistoryTopVideoList
  169. 请求方式:POST
  170. remark: ctop_kuaishou_report_daily_material
  171. 整个项目维度 stat_date 在 startTime 和 endTime, order by sum(charge) desc limit num
  172. 入参:
  173. accountId: 账户id
  174. startTime: 开始时间
  175. endTime: 结束时间
  176. num: 查询数量
  177. 返回:
  178. {
  179. "code": 0,
  180. "data": [
  181. {
  182. "charge": 40871.123,
  183. "video_url": "&di=811c96e5&bp=13890",
  184. "photo_id": "5254011957394409468",
  185. "signature": "4af22090cddc32773c7707de3c652f77",
  186. "channel_type": 0,
  187. "material_type":1, --1:竖版 2:横版
  188. "imageList": ["09f61ca7174160c12d9a68306049ca82","46996bea84287692b3884686a52903ed"]
  189. }
  190. ],
  191. "message": "SUCCESS"
  192. """
  193. if not app_version:
  194. request_data = {"accountId": account_id, "num": cnt, "startTime": start_time, "endTime": end_time}
  195. else:
  196. request_data = {"accountId": account_id, "num": cnt, "startTime": start_time, "endTime": end_time,
  197. 'appVersion': app_version}
  198. try:
  199. request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  200. request_json = json.loads(request)
  201. if request_json["code"] != 0:
  202. logger.info("account_id=%s, app_version=%s, the res of getHistoryTopVideoList is %s" %
  203. (account_id, app_version, request_json['message']))
  204. except Exception:
  205. logger.error('account_id=%s, app_version=%s, the res of getHistoryTopVideoList is %s, the traceback is %s ' %
  206. (account_id, app_version, request, traceback.format_exc()))
  207. return {"code": -1}
  208. return request_json
  209. def get_campaign_and_group_name_rule(account_id):
  210. sql = """select campaign_name, group_name
  211. from ctop_ai_kuaishou_advertiser_strategy
  212. where account_id = %s
  213. order by create_time desc
  214. limit 1
  215. """ % account_id
  216. df = pd.read_sql(sql, engine)
  217. if df.empty:
  218. return None, None
  219. campaign_name = df['campaign_name'].values[0]
  220. group_name = df['group_name'].values[0]
  221. return campaign_name, group_name
  222. def get_request_data(account_id, campaign_id, video_info, campaign_name, group_name,
  223. ai_strategy_remark, name_replace='',
  224. unit_type=4, click_track_url=None, app_id=None):
  225. res = {
  226. "video": video_info,
  227. "operation_type": 1,
  228. "account_id": account_id,
  229. "ai_strategy_remark": ai_strategy_remark,
  230. "campaign_info":
  231. {"campaign_id": campaign_id,
  232. "campaign_name": (campaign_name.replace('自定义', name_replace) + str(datetime.datetime.now()))
  233. if name_replace else campaign_name},
  234. 'group_info':
  235. {'group_name': (group_name.replace('自定义', name_replace) + str(datetime.datetime.now()))
  236. if name_replace else (group_name + str(datetime.datetime.now())),
  237. 'begin_time': str(datetime.date.today()),
  238. 'unit_type': unit_type,
  239. 'app_id': app_id},
  240. 'creative_info': {'is_sticky': 0,
  241. 'click_track_url': click_track_url}
  242. }
  243. return res