CommonFunction.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. """
  27. 获取应用详情
  28. url:http://192.168.1.8:8080/jeecg-boot/ai/aiKuaiShouAppInfo/list
  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}
  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, the res of list is %s" % (account_id, request_json['message']))
  62. except:
  63. logger.error('account_id=%s, the res of list is %s, the traceback is %s ' %
  64. (account_id, request, traceback.format_exc()))
  65. return {"code": -1}
  66. return request_json
  67. def get_app_detail(account_id, url, app_id):
  68. """
  69. 获取应用详情
  70. url:http://192.168.1.8:8080/jeecg-boot/ai/aiKuaiShouAppInfo/getAppDetail
  71. 请求方式:post
  72. 入参:accountId 账户id
  73. appId 应用id
  74. 返回参数:
  75. {
  76. "code": 0,
  77. "data": {
  78. "id": 413,
  79. "accountId": 9864909,
  80. "appVersion": "ksjyrlyn-人类一脑涂地大败",
  81. "appName": "233乐园",
  82. "packageName": "com.meta.box",
  83. "platform": 1,
  84. "url": "https://www.233leyuan.com/apiserv/api/deliveryTest/ABTest?id=ksjyrlyn",
  85. "useSdk": 0,
  86. "appPrivacyUrl": "https://webcdn.233leyuan.com/app/useragreement/metax/privacyagreement.html",
  87. "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",
  88. "appMd5": "0b2314c738956dc159ed38dc584ef214",
  89. "imageToken": "marketb3e98f5f68a74870b8d8d814499d6189.jpg",
  90. "status": 2,
  91. "remark": "创建应用成功",
  92. "appId": 3543665,
  93. "createTime": "2021-02-03 16:40:53",
  94. "updateTime": "2021-02-03 16:42:52"},
  95. "message": "success"
  96. }
  97. """
  98. request_data = {"accountId": account_id, "appId": app_id}
  99. try:
  100. request = requests.post(url=url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  101. request_json = json.loads(request)
  102. if request_json["code"] != 0:
  103. logger.info("account_id=%s, appId=%s, the res of getAppDetail is %s" % (account_id, app_id, request_json['message']))
  104. except:
  105. logger.error('account_id=%s, appId=%s, the res of getAppDetail is %s, the traceback is %s ' %
  106. (account_id, app_id, request, traceback.format_exc()))
  107. return {"code": -1}
  108. return request_json
  109. def get_history_video_info(account_id, url, start_time, end_time, video_cnt, related_creative_max_cnt, app_version=None):
  110. """
  111. url:http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getHistoryVideoList
  112. 请求方式:POST
  113. remark: 见入参
  114. 入参:accountId 账户id
  115. startDate 开始时间 --(视频的upload_time)
  116. endDate 结束时间 --(视频的upload_time)
  117. createCount 关联创意数 (小于该值)
  118. num 获取数量 (随机num个)
  119. """
  120. if not app_version:
  121. request_data = {"accountId": account_id,
  122. "startDate": start_time,
  123. "endDate": end_time,
  124. "num": video_cnt,
  125. "createCount": related_creative_max_cnt}
  126. else:
  127. request_data = {"accountId": account_id,
  128. "startDate": start_time,
  129. "endDate": end_time,
  130. "num": video_cnt,
  131. "createCount": related_creative_max_cnt,
  132. "appVersion": app_version}
  133. try:
  134. request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  135. request_json = json.loads(request)
  136. if request_json["code"] != 0:
  137. logger.info("account_id=%s, app_version=%s, the res of getHistoryVideoList is %s" %
  138. (account_id, app_version, request_json['message']))
  139. except Exception:
  140. logger.error('account_id=%s, app_version=%s, the res of getHistoryVideoList is %s, the traceback is %s ' %
  141. (account_id, app_version, request, traceback.format_exc()))
  142. return {"code": -1}
  143. return request_json
  144. def refresh_video(account_id, url):
  145. """
  146. 需要调用刷新快手素材的接口 http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getKuaiShouVideoList
  147. (请求方式 POST 入参:accountId 返回结果:成功或失败)
  148. remark: 自动上新之前,需要调用该接口,注意是账户层级的调用,不是app_id层级的调用
  149. """
  150. try:
  151. request = requests.post(url=url, headers=headers,
  152. data=json.JSONEncoder().encode({"accountId": account_id})).text
  153. request_json = json.loads(request)
  154. logger.info('account_id=%s, the res of getKuaiShouVideoList is %s' % (account_id, request_json))
  155. except Exception:
  156. logger.error('account_id=%s, the res of getKuaiShouVideoList is %s, traceback is %s ' %
  157. (account_id, request, traceback.format_exc()))
  158. def get_new_video_info(account_id, url, app_version=None):
  159. """
  160. url:http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getNewVideoList
  161. 请求方式:POST
  162. remark:
  163. 1、返回 upload_time 在上次调用时间到本次调用时间内的素材(第一次调用返回5分钟前到本次调用时间的素材)
  164. 入参:accountId
  165. """
  166. if not app_version:
  167. request_data = {"accountId": account_id}
  168. else:
  169. request_data = {"accountId": account_id, 'appVersion': app_version}
  170. try:
  171. request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  172. request_json = json.loads(request)
  173. if request_json["code"] != 0:
  174. logger.info("account_id=%s, app_version=%s, the res of getNewVideoList is %s" %
  175. (account_id, app_version, request_json['message']))
  176. except Exception:
  177. logger.error('account_id=%s, app_version=%s, the res of getNewVideoList is %s, the traceback is %s ' %
  178. (account_id, app_version, request, traceback.format_exc()))
  179. return {"code": -1}
  180. return request_json
  181. def get_missing_video_info(account_id, cnt, url, start_time, end_time, app_version=None):
  182. """
  183. url:"http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getCreateZeroVideoList"
  184. 请求方式:POST
  185. remark: 视频的upload_time 在startTime 和 endTime 之间,创意关联个数为0,order by upload_time asc limit videoCnt
  186. 入参:
  187. accountId: 账户id
  188. videoCnt: 查询数量
  189. startTime: 开始时间
  190. endTime: 结束时间
  191. """
  192. if not app_version:
  193. request_data = {"accountId": account_id, "videoCnt": cnt, "startTime": start_time, "endTime": end_time}
  194. else:
  195. request_data = {"accountId": account_id, "videoCnt": cnt, "startTime": start_time, "endTime": end_time,
  196. 'appVersion': app_version}
  197. try:
  198. request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  199. request_json = json.loads(request)
  200. if request_json["code"] != 0:
  201. logger.info("account_id=%s, app_version=%s, the res of getCreateZeroVideoList is %s" %
  202. (account_id, app_version, request_json['message']))
  203. except Exception:
  204. logger.error('account_id=%s, app_version=%s, the res of getCreateZeroVideoList is %s, the traceback is %s ' %
  205. (account_id, app_version, request, traceback.format_exc()))
  206. return {"code": -1}
  207. return request_json
  208. def get_top_video_info(account_id, cnt, start_time, end_time, url, app_version=None):
  209. """
  210. url:http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getHistoryTopVideoList
  211. 请求方式:POST
  212. remark: ctop_kuaishou_report_daily_material
  213. 整个项目维度 stat_date 在 startTime 和 endTime, order by sum(charge) desc limit num
  214. 入参:
  215. accountId: 账户id
  216. startTime: 开始时间
  217. endTime: 结束时间
  218. num: 查询数量
  219. remark: 返回结果中 "photo_name" == "all",表示为通用视频,反之为app_id对应的特定视频("photo_name": "ksjytdrca-糖豆人冲啊")
  220. 返回:
  221. {
  222. "code": 0,
  223. "data": [
  224. {
  225. "material_type": 1,
  226. "charge": 154229.670,
  227. "video_url": "http://alimov2.a.yximgs.com/upic/2021/02/03/18/BMjAyMTAyMDMxODU3MjlfMjI0NTY2OTI5MF80MzUxMjc4MTgzNV8wXzM=_b_B142e1ddc44705f978fe2a7fa4f4e9eb1.mp4?tag=1-1613958443-unknown-0-nbzw9am7cu-465420c6c3fe0f27&clientCacheKey=3xeh2gxd84qg462_b.mp4&tt=b&di=8bba1b60&bp=13890",
  228. "photo_id": "5249789835088957477",
  229. "photo_name": "ksjytdrca-糖豆人冲啊",
  230. "signature": "e160cc10d73031a48895a32834b54c46",
  231. "channel_type": 0,
  232. "imageList": [
  233. "3267a05ef40ca8180b87428bcc324628",
  234. "1e5cb59e588a6dd489a9c0c36b7e52cd",
  235. "c0a199395614c5204a2f9a5aa9457fdf",
  236. "659c1865be2e9b561475d204ebf363b4",
  237. "7b85a1262a59e22ff2e82a3374d8beb0",
  238. "2e5542b2b39191219c33e278ccae0bdf",
  239. "45d2d11e052aca20c2a5e546e2ffda30",
  240. "8902152d887dfc119a2ba32d970683d9",
  241. "7becab0d75b2cbec66ea18fbabd33b59",
  242. "e1ba0f57d3abe2822ab3fc02d9ea7ba3",
  243. "9676c587837df85df4aba76a5ce3b8c1",
  244. "1b42543b0b31613740dfaa8fd861136c",
  245. "8a1c95ecf4af672d2f7a1acef36bce15",
  246. "5e6d8d8f0339ab7d2b3e1de533111606",
  247. "8b5b6c6d63e62e04a92d84fe175c05fd"
  248. ]
  249. },
  250. {
  251. "material_type": 1,
  252. "charge": 92135.125,
  253. "video_url": "http://txmov2.a.yximgs.com/upic/2021/02/03/18/BMjAyMTAyMDMxODU3MzhfMjI0NTY2OTI5MF80MzUxMjc5MzU0MV8wXzM=_b_Bd0dab20fe03c02e76080340c66341573.mp4?tag=1-1613958443-unknown-0-9romi5xmpo-d5161ea63e200cde&clientCacheKey=3xu33qupdka3h5y_b.mp4&tt=b&di=8bba1b60&bp=13890",
  254. "photo_id": "5192368941898812785",
  255. "photo_name": "all",
  256. "signature": "fc4004dc82f44f765aa7a0150e3aad8a",
  257. "channel_type": 0,
  258. "imageList": [
  259. "8cc3de1612ced3a6dbdf8e9e054c6ec1",
  260. "7a2e7c070c25e6d06460e9fe62f139c7",
  261. "43dcb78211d4d6d8bdb5a398a57d9d1d",
  262. "14118f0ecd4a86e35165e6130ba69c6b",
  263. "eec697ea33e3ddaaf0e77e65749a6f23",
  264. "da6e9648da85204c717e8beb87b19328",
  265. "0839fffc60203f1ba13bc89c26d437c4",
  266. "feabdeaab2e264e521be06c5fc4c0457"
  267. ]
  268. },
  269. {
  270. "material_type": 1,
  271. "charge": 70956.941,
  272. "video_url": "http://txmov2.a.yximgs.com/upic/2021/02/03/18/BMjAyMTAyMDMxODIwMzdfMjI0NTY2OTI5MF80MzUxMDIxMDg2OF8wXzM=_b_Bfea5219f9c40bdf34f8420429c59434c.mp4?tag=1-1613958443-unknown-0-rk05fpbcb3-7dbb4cb30a941643&clientCacheKey=3x3xi88nniq6m6i_b.mp4&tt=b&di=8bba1b60&bp=13890",
  273. "photo_id": "5219390536383350388",
  274. "photo_name": "all",
  275. "signature": "0ec9b0bac40463d18f151be605682dc8",
  276. "channel_type": 0,
  277. "imageList": [
  278. "9b71f206b6ca9d2f89712b79ebd0d699",
  279. "c802325b325cf67c5c7c13872572642d",
  280. "e79ca1b5dde8709e794b53582e40dbdd",
  281. "37b529460cbebcba259a412dce40bd9c",
  282. "b7b37d27e33d21923c7ade475e7d0ffb",
  283. "b4ebac5da9e624aae208f2d8d9ab6b0e",
  284. "0675ef5a12e9ed01954e40309ff226c1",
  285. "04ed3651b233aad5a1f7e76b1ead48e4",
  286. "6a954e0ecfce38ad854c715c4e0d3a26",
  287. "516a957c2c80a4a74a24d1073412647f",
  288. "95f1a42d06657c4b8b8554b81583db5f",
  289. "30ba9f8a90539f3afe43f3046c54f488",
  290. "e48236f0509398412efac2f9ee258c6d",
  291. "a9f21f3731b653784fcf2dca6d4ca427",
  292. "014383e5b7c33cb9e0a515bf11e9a786"
  293. ]
  294. }
  295. ],
  296. "message": "SUCCESS"
  297. }
  298. """
  299. if not app_version:
  300. request_data = {"accountId": account_id, "num": cnt, "startTime": start_time, "endTime": end_time}
  301. else:
  302. request_data = {"accountId": account_id, "num": cnt, "startTime": start_time, "endTime": end_time,
  303. 'appVersion': app_version}
  304. try:
  305. request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
  306. request_json = json.loads(request)
  307. if request_json["code"] != 0:
  308. logger.info("account_id=%s, app_version=%s, the res of getHistoryTopVideoList is %s" %
  309. (account_id, app_version, request_json['message']))
  310. except Exception:
  311. logger.error('account_id=%s, app_version=%s, the res of getHistoryTopVideoList is %s, the traceback is %s ' %
  312. (account_id, app_version, request, traceback.format_exc()))
  313. return {"code": -1}
  314. return request_json
  315. def get_campaign_and_group_name_rule(account_id):
  316. sql = """select campaign_name, group_name
  317. from ctop_ai_kuaishou_advertiser_strategy
  318. where account_id = %s
  319. order by create_time desc
  320. limit 1
  321. """ % account_id
  322. df = pd.read_sql(sql, engine)
  323. if df.empty:
  324. return None, None
  325. campaign_name = df['campaign_name'].values[0]
  326. group_name = df['group_name'].values[0]
  327. return campaign_name, group_name
  328. def get_request_data(account_id, campaign_id, video_info, campaign_name, group_name,
  329. ai_strategy_remark, name_replace='',
  330. unit_type=4, click_track_url=None, app_id=None):
  331. res = {
  332. "video": video_info,
  333. "operation_type": 1,
  334. "account_id": account_id,
  335. "ai_strategy_remark": ai_strategy_remark,
  336. "campaign_info":
  337. {"campaign_id": campaign_id,
  338. "campaign_name": (campaign_name.replace('自定义', name_replace) + str(datetime.datetime.now()))
  339. if name_replace else campaign_name},
  340. 'group_info':
  341. {'group_name': (group_name.replace('自定义', name_replace) + str(datetime.datetime.now()))
  342. if name_replace else (group_name + str(datetime.datetime.now())),
  343. 'begin_time': str(datetime.date.today()),
  344. 'unit_type': unit_type,
  345. 'app_id': app_id},
  346. 'creative_info': {'is_sticky': 0,
  347. 'click_track_url': click_track_url}
  348. }
  349. return res