123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- import requests
- from utils.UrlConfig import headers, refresh_video_url
- import json
- import pandas as pd
- import datetime
- from utils.DataBaseConfig import *
- from ai_time_task_creative_handler import logger
- import traceback
- def get_lower_case_name(name):
- lst = []
- for index, char in enumerate(name):
- if char.isupper() and index != 0:
- lst.append("_")
- lst.append(char)
- return "".join(lst).lower()
- def get_app_detail(account_id, url, app_id):
- """
- 获取应用详情
- url:http://192.168.1.8:8080/jeecg-boot/ai/aiKuaiShouAppInfo/getAppDetail
- 请求方式:post
- 入参:accountId 账户id
- appId 应用id
- 返回参数:
- {
- "code": 0,
- "data": {
- "id": 413,
- "accountId": 9864909,
- "appVersion": "ksjyrlyn-人类一脑涂地大败",
- "appName": "233乐园",
- "packageName": "com.meta.box",
- "platform": 1,
- "url": "https://www.233leyuan.com/apiserv/api/deliveryTest/ABTest?id=ksjyrlyn",
- "useSdk": 0,
- "appPrivacyUrl": "https://webcdn.233leyuan.com/app/useragreement/metax/privacyagreement.html",
- "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",
- "appMd5": "0b2314c738956dc159ed38dc584ef214",
- "imageToken": "marketb3e98f5f68a74870b8d8d814499d6189.jpg",
- "status": 2,
- "remark": "创建应用成功",
- "appId": 3543665,
- "createTime": "2021-02-03 16:40:53",
- "updateTime": "2021-02-03 16:42:52"},
- "message": "success"
- }
- """
- request_data = {"accountId": account_id, "appId": app_id}
- try:
- request = requests.post(url=url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
- request_json = json.loads(request)
- if request_json["code"] != 0:
- logger.info("account_id=%s, appId=%s, the res of getAppDetail is %s" % (account_id, app_id, request_json['message']))
- except:
- logger.error('account_id=%s, appId=%s, the res of getAppDetail is %s, the traceback is %s ' %
- (account_id, app_id, request, traceback.format_exc()))
- return {"code": -1}
- return request_json
- def get_history_video_info(account_id, url, start_time, end_time, video_cnt, related_creative_max_cnt, app_version=None):
- """
- url:http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getHistoryVideoList
- 请求方式:POST
- remark: 见入参
- 入参:accountId 账户id
- startDate 开始时间 --(视频的upload_time)
- endDate 结束时间 --(视频的upload_time)
- createCount 关联创意数 (小于该值)
- num 获取数量 (随机num个)
- """
- if not app_version:
- request_data = {"accountId": account_id,
- "startDate": start_time,
- "endDate": end_time,
- "num": video_cnt,
- "createCount": related_creative_max_cnt}
- else:
- request_data = {"accountId": account_id,
- "startDate": start_time,
- "endDate": end_time,
- "num": video_cnt,
- "createCount": related_creative_max_cnt,
- "appVersion": app_version}
- try:
- request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
- request_json = json.loads(request)
- if request_json["code"] != 0:
- logger.info("account_id=%s, app_version=%s, the res of getHistoryVideoList is %s" %
- (account_id, app_version, request_json['message']))
- except Exception:
- logger.error('account_id=%s, app_version=%s, the res of getHistoryVideoList is %s, the traceback is %s ' %
- (account_id, app_version, request, traceback.format_exc()))
- return {"code": -1}
- return request_json
- def get_new_video_info(account_id, url, app_version=None):
- """
- url:http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getNewVideoList
- 请求方式:POST
- remark:
- 1、返回 upload_time 在上次调用时间到本次调用时间内的素材(第一次调用返回5分钟前到本次调用时间的素材)
- 2、需要调用刷新快手素材的接口 http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getKuaiShouVideoList
- (请求方式 POST 入参:accountId 返回结果:成功或失败)
- 入参:accountId
- """
- try:
- request = requests.post(url=refresh_video_url, headers=headers,
- data=json.JSONEncoder().encode({"accountId": account_id})).text
- _ = json.loads(request)
- except Exception:
- logger.error('account_id=%s, app_version=%s, the res of getKuaiShouVideoList is %s, traceback is %s ' %
- (account_id, app_version, request, traceback.format_exc()))
- if not app_version:
- request_data = {"accountId": account_id}
- else:
- request_data = {"accountId": account_id, 'appVersion': app_version}
- try:
- request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
- request_json = json.loads(request)
- if request_json["code"] != 0:
- logger.info("account_id=%s, app_version=%s, the res of getNewVideoList is %s" %
- (account_id, app_version, request_json['message']))
- except Exception:
- logger.error('account_id=%s, app_version=%s, the res of getNewVideoList is %s, the traceback is %s ' %
- (account_id, app_version, request, traceback.format_exc()))
- return {"code": -1}
- return request_json
- def get_missing_video_info(account_id, cnt, url, start_time, end_time, app_version=None):
- """
- url:"http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getCreateZeroVideoList"
- 请求方式:POST
- remark: 视频的upload_time 在startTime 和 endTime 之间,创意关联个数为0,order by upload_time asc limit videoCnt
- 入参:
- accountId: 账户id
- videoCnt: 查询数量
- startTime: 开始时间
- endTime: 结束时间
- """
- if not app_version:
- request_data = {"accountId": account_id, "videoCnt": cnt, "startTime": start_time, "endTime": end_time}
- else:
- request_data = {"accountId": account_id, "videoCnt": cnt, "startTime": start_time, "endTime": end_time,
- 'appVersion': app_version}
- try:
- request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
- request_json = json.loads(request)
- if request_json["code"] != 0:
- logger.info("account_id=%s, app_version=%s, the res of getCreateZeroVideoList is %s" %
- (account_id, app_version, request_json['message']))
- except Exception:
- logger.error('account_id=%s, app_version=%s, the res of getCreateZeroVideoList is %s, the traceback is %s ' %
- (account_id, app_version, request, traceback.format_exc()))
- return {"code": -1}
- return request_json
- def get_top_video_info(account_id, cnt, start_time, end_time, url, app_version=None):
- """
- url:http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getHistoryTopVideoList
- 请求方式:POST
- remark: ctop_kuaishou_report_daily_material
- 整个项目维度 stat_date 在 startTime 和 endTime, order by sum(charge) desc limit num
- 入参:
- accountId: 账户id
- startTime: 开始时间
- endTime: 结束时间
- num: 查询数量
- 返回:
- {
- "code": 0,
- "data": [
- {
- "charge": 40871.123,
- "video_url": "&di=811c96e5&bp=13890",
- "photo_id": "5254011957394409468",
- "signature": "4af22090cddc32773c7707de3c652f77",
- "channel_type": 0,
- "material_type":1, --1:竖版 2:横版
- "imageList": ["09f61ca7174160c12d9a68306049ca82","46996bea84287692b3884686a52903ed"]
- }
- ],
- "message": "SUCCESS"
- """
- if not app_version:
- request_data = {"accountId": account_id, "num": cnt, "startTime": start_time, "endTime": end_time}
- else:
- request_data = {"accountId": account_id, "num": cnt, "startTime": start_time, "endTime": end_time,
- 'appVersion': app_version}
- try:
- request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
- request_json = json.loads(request)
- if request_json["code"] != 0:
- logger.info("account_id=%s, app_version=%s, the res of getHistoryTopVideoList is %s" %
- (account_id, app_version, request_json['message']))
- except Exception:
- logger.error('account_id=%s, app_version=%s, the res of getHistoryTopVideoList is %s, the traceback is %s ' %
- (account_id, app_version, request, traceback.format_exc()))
- return {"code": -1}
- return request_json
- def get_campaign_and_group_name_rule(account_id):
- sql = """select campaign_name, group_name
- from ctop_ai_kuaishou_advertiser_strategy
- where account_id = %s
- order by create_time desc
- limit 1
- """ % account_id
- df = pd.read_sql(sql, engine)
- if df.empty:
- return None, None
- campaign_name = df['campaign_name'].values[0]
- group_name = df['group_name'].values[0]
- return campaign_name, group_name
- def get_request_data(account_id, campaign_id, video_info, campaign_name, group_name,
- ai_strategy_remark, name_replace='',
- unit_type=4, click_track_url=None, app_id=None):
- res = {
- "video": video_info,
- "operation_type": 1,
- "account_id": account_id,
- "ai_strategy_remark": ai_strategy_remark,
- "campaign_info":
- {"campaign_id": campaign_id,
- "campaign_name": (campaign_name.replace('自定义', name_replace) + str(datetime.datetime.now()))
- if name_replace else campaign_name},
- 'group_info':
- {'group_name': (group_name.replace('自定义', name_replace) + str(datetime.datetime.now()))
- if name_replace else (group_name + str(datetime.datetime.now())),
- 'begin_time': str(datetime.date.today()),
- 'unit_type': unit_type,
- 'app_id': app_id},
- 'creative_info': {'is_sticky': 0,
- 'click_track_url': click_track_url}
- }
- return res
|