| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 | 
							- import requests
 
- from utils.UrlConfig import headers
 
- import json
 
- import pandas as pd
 
- import datetime
 
- from utils.DataBaseConfig import *
 
- import traceback
 
- from concurrent_log import ConcurrentTimedRotatingFileHandler
 
- import logging
 
- log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', '%m/%d/%Y %I:%M:%S %p')
 
- log_handler = ConcurrentTimedRotatingFileHandler("logs/commonfunction.log", when="midnight", backupCount=100)
 
- log_handler.setFormatter(log_formatter)
 
- logger = logging.getLogger('common_function_logger')
 
- logger.addHandler(log_handler)
 
- logger.setLevel(logging.DEBUG)
 
- print('id of common_function_logger %s' % id(logger))
 
- logger.info("common_function_logger started!")
 
- 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_list(account_id,url):
 
-     request_data = {"accountId": account_id}
 
-     logger.info("aiKuaiShouAppInfo/list the request_data is %s" % request_data)
 
-     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, the res of list is %s" % (account_id, request_json['message']))
 
-     except:
 
-         logger.error('account_id=%s, the res of list is %s, the traceback is %s ' %
 
-                      (account_id, request, traceback.format_exc()))
 
-         return {"code": -1}
 
-     return request_json
 
- def get_app_detail(account_id, url, app_id):
 
-     request_data = {"accountId": account_id, "appId": app_id}
 
-     logger.info("/getAppDetail the request_data is %s" % request_data)
 
-     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(channel_type,account_id, url, start_time, end_time, video_cnt, related_creative_max_cnt, app_version=None):
 
-     request_data = {"accountId": account_id,
 
-                     "startDate": start_time,
 
-                     "endDate": end_time,
 
-                     "num": video_cnt,
 
-                     "createCount": related_creative_max_cnt}
 
-     if app_version:
 
-         request_data['appVersion'] = app_version
 
-     if channel_type != 2:
 
-         request_data['channelType'] = channel_type
 
-     try:
 
-         logger.info("getHistoryVideoList the request_data is %s" % request_data)
 
-         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 refresh_video(account_id, url):
 
-     try:
 
-         request = requests.post(url=url, headers=headers,
 
-                                 data=json.JSONEncoder().encode({"accountId": account_id})).text
 
-         request_json = json.loads(request)
 
-         logger.info('account_id=%s,  the res of getKuaiShouVideoList is %s' % (account_id, request_json))
 
-     except Exception:
 
-         logger.error('account_id=%s,  the res of getKuaiShouVideoList is %s, traceback is %s ' %
 
-                      (account_id, request, traceback.format_exc()))
 
- #DONE
 
- def get_new_video_info(channel_type,account_id, url, app_version=None):
 
-     request_data = {"accountId":account_id}
 
-     if app_version:
 
-         request_data["appVersion"] = app_version
 
-     if channel_type != 2:
 
-         request_data["channelType"] = channel_type
 
-     try:
 
-         logger.info("getNewVideoList the request_data is %s" % request_data)
 
-         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
 
- #DONE
 
- def get_missing_video_info(channel_type,account_id, cnt, url, start_time, end_time, app_version=None):
 
-     request_data = {"accountId": account_id, "videoCnt": cnt, "startTime": start_time, "endTime": end_time}
 
-     if app_version:
 
-         request_data['appVersion'] = app_version
 
-     if channel_type != 2:
 
-         request_data['channelType'] = channel_type
 
-     try:
 
-         logger.info("getCreateZeroVideoList the request_data is %s" % request_data)
 
-         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
 
- # DONE
 
- def get_top_video_info(channel_type,account_id, cnt, start_time, end_time, url, app_version=None):
 
-     request_data = {"accountId": account_id, "num": cnt, "startTime": start_time, "endTime": end_time}
 
-     if app_version:
 
-         request_data['appVersion'] = app_version
 
-     if channel_type != 2:
 
-         request_data['channelType'] = channel_type
 
-     try:
 
-         logger.info("getHistoryTopVideoList the request_data is %s" % request_data)
 
-         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, creative_name=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,
 
-                           'creative_name': creative_name}
 
-     }
 
-     return res
 
 
  |