Explorar el Código

MOD:ad_cost_day 添加campaign_id

cxyu hace 3 años
padre
commit
173bf7f5d2
Se han modificado 1 ficheros con 46 adiciones y 22 borrados
  1. 46 22
      app/api_data/tx_ad_cost/cost_util.py

+ 46 - 22
app/api_data/tx_ad_cost/cost_util.py

@@ -50,7 +50,7 @@ def get_campaign(account_id, access_token, flag, campaign_ids, dt):
                 parameters[k] = json.dumps(parameters[k])
 
         while True:
-            r = requests.get(url, params=parameters,timeout=5).json()
+            r = requests.get(url, params=parameters, timeout=5).json()
             code = r['code']
             if code == 11017:
                 time.sleep(61)
@@ -113,7 +113,7 @@ def get_adcreatives(account_id, access_token, flag, adc_ids, dt):  # 获取创
 
         while True:
             h = requests.get(url, params=parameters, timeout=1)
-            # logging.info(h.json())
+            # logging.info(json.dumps(h.json(), ensure_ascii=False))
 
             if h.status_code == 200:
                 r = h.json()
@@ -128,7 +128,7 @@ def get_adcreatives(account_id, access_token, flag, adc_ids, dt):  # 获取创
             is_video = 0
             for i in r['data']['list']:
                 # logging.info(i)
-                description=''
+                description = ''
                 if flag == 'MP':
                     if len(i['adcreative_elements']) > 0:
                         d = i['adcreative_elements']
@@ -259,7 +259,7 @@ def images_info_get(account_id, access_token, image_ids):  # 获取图片信息
                     parameters[k] = json.dumps(parameters[k])
 
             while True:
-                h = requests.get(url, params=parameters,timeout=5)
+                h = requests.get(url, params=parameters, timeout=5)
                 # logging.info(h.text)
                 if h.status_code == 200:
                     r = h.json()
@@ -351,7 +351,7 @@ def video_info_get(account_id, access_token, image_ids):  # 获取视频信息
                     parameters[k] = json.dumps(parameters[k])
 
             while True:
-                h = requests.get(url, params=parameters,timeout=5)
+                h = requests.get(url, params=parameters, timeout=5)
                 # logging.info(h.text)
                 if h.status_code == 200:
                     r = h.json()
@@ -443,7 +443,8 @@ def get_ad_info(account_id, access_token, flag, ad_ids, dt):
                 parameters[k] = json.dumps(parameters[k])
 
         while True:
-            r = requests.get(url, params=parameters,timeout=5).json()
+            r = requests.get(url, params=parameters, timeout=5)
+            r = r.json()
             code = r['code']
             if code == 11017:
                 time.sleep(61)
@@ -480,7 +481,7 @@ def ad_cost_day_gdt(account_id, access_token, st, et):
     # 接口文档 https://developers.e.qq.com/docs/api/insights/ad_insights/daily_reports_get?version=1.3
     url = 'https://api.e.qq.com/v1.3/daily_reports/get'
     fields = (
-        'date', 'ad_id', 'adgroup_id', 'cost', 'view_count', 'ctr', 'follow_count', 'web_order_count', 'order_amount')
+        'date', 'ad_id', 'adgroup_id','campaign_id', 'cost', 'view_count', 'ctr', 'follow_count', 'web_order_count', 'order_amount')
     li = []
     page = 1
     while True:
@@ -504,7 +505,7 @@ def ad_cost_day_gdt(account_id, access_token, st, et):
             if type(parameters[k]) is not str:
                 parameters[k] = json.dumps(parameters[k])
         while True:
-            r = requests.get(url, params=parameters,timeout=5)
+            r = requests.get(url, params=parameters, timeout=5)
             r = r.json()
             # logging.info(r)
             code = r['code']
@@ -518,9 +519,12 @@ def ad_cost_day_gdt(account_id, access_token, st, et):
                 if i['cost'] > 0:
                     li.append(
                         (
-                            i['date'], i['ad_id'], i['adgroup_id'], i['cost'] / 100, i['view_count'],
+                            i['date'], i['ad_id'],
+                            i['adgroup_id'], i['campaign_id'],
+                            i['cost'] / 100, i['view_count'],
                             i['ctr'] * i['view_count'],
-                            i['follow_count'], i['web_order_count'], i['order_amount'] / 100, account_id, 'GDT'
+                            i['follow_count'], i['web_order_count'],
+                            i['order_amount'] / 100, account_id, 'GDT'
                         )
                     )
 
@@ -531,8 +535,25 @@ def ad_cost_day_gdt(account_id, access_token, st, et):
             page += 1
     # logging.info(li)
     if len(li) > 0:
-        logging.info(f"{account_id} have ad cost :{len(li)} ")
-        db.quchen_text.executeMany('replace into ad_cost_day values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)', li)
+        # 对一下ad的数据
+        li_df = pandas.DataFrame(li)
+        li_df_g = li_df.groupby([0, 1, 10, 11])
+        li_new = []
+        adgroup_id_dict = {}
+        campaign_id_dict = {}
+        for index, group in li_df_g:
+            adgroup_id_dict[index] = ','.join([str(i) for i in group[2].tolist()])
+            campaign_id_dict[index] = ','.join([str(i) for i in group[3].tolist()])
+        for index, row in li_df_g.agg('sum').iterrows():
+            new_row = row.tolist()
+            new_row = list(index[0:2]) + new_row + list(index[2:])
+            new_row[2] = adgroup_id_dict[index]
+            new_row[3] = campaign_id_dict[index]
+            li_new.append(tuple(new_row))
+        logging.info(f"{account_id} have ad cost :{len(li_new)} ")
+        db.quchen_text.executeMany('''replace into ad_cost_day(dt,ad_id,adgroup_id,campaign_id,cost,
+        view_count,click_count,follow_count,order_count,order_amount,
+        account_id,type) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)''', li_new)
         db.close()
 
 
@@ -540,7 +561,7 @@ def ad_cost_day_mp(account_id, access_token, st, et):
     # 接口文档 https://developers.e.qq.com/docs/api/insights/ad_insights/daily_reports_get?version=1.3
     url = 'https://api.e.qq.com/v1.3/daily_reports/get'
     fields = ('date', 'ad_id', 'adgroup_id', 'cost', 'view_count', 'valid_click_count', 'official_account_follow_count',
-              'order_count', 'order_amount')
+              'order_count', 'order_amount', 'campaign_id')
     li = []
     page = 1
     while True:
@@ -564,7 +585,7 @@ def ad_cost_day_mp(account_id, access_token, st, et):
                 parameters[k] = json.dumps(parameters[k])
 
         while True:
-            r = requests.get(url, params=parameters,timeout=5)
+            r = requests.get(url, params=parameters, timeout=5)
             r = r.json()
             # logging.info(r['data']['list'])
             # import pandas as pd
@@ -581,7 +602,8 @@ def ad_cost_day_mp(account_id, access_token, st, et):
                 if i['cost'] > 0:
                     li.append(
                         (
-                            i['date'], i['ad_id'], i['adgroup_id'],
+                            i['date'], i['ad_id'],
+                            i['adgroup_id'], i['campaign_id'],
                             i['cost'] / 100, i['view_count'],
                             i['valid_click_count'],
                             i['official_account_follow_count'],
@@ -596,24 +618,26 @@ def ad_cost_day_mp(account_id, access_token, st, et):
         else:
             page += 1
     # logging.info(li)
-    # exit()
     if len(li) > 0:
-        # TODO:询问一下adgroup_id,campaign_id作用
         # 对一下ad的数据
         li_df = pandas.DataFrame(li)
-        li_df_g = li_df.groupby([0, 1, 9, 10])
+        li_df_g = li_df.groupby([0, 1, 10, 11])
         li_new = []
         adgroup_id_dict = {}
+        campaign_id_dict = {}
         for index, group in li_df_g:
             adgroup_id_dict[index] = ','.join([str(i) for i in group[2].tolist()])
+            campaign_id_dict[index] = ','.join([str(i) for i in group[3].tolist()])
         for index, row in li_df_g.agg('sum').iterrows():
             new_row = row.tolist()
             new_row = list(index[0:2]) + new_row + list(index[2:])
             new_row[2] = adgroup_id_dict[index]
+            new_row[3] = campaign_id_dict[index]
             li_new.append(tuple(new_row))
         logging.info(f"{account_id} have ad cost :{len(li_new)} ")
-        db.quchen_text.executeMany('replace into ad_cost_day values(%s,%s,%s,%s,%s,'
-                                   '%s,%s,%s,%s,%s,%s)', li_new)
+        db.quchen_text.executeMany('''replace into ad_cost_day(dt,ad_id,adgroup_id,campaign_id,cost,
+        view_count,click_count,follow_count,order_count,order_amount,
+        account_id,type) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)''', li_new)
         db.close()
 
 
@@ -652,7 +676,7 @@ def daily_reports_get(access_token, account_id, level, start_date, end_date, fie
             parameters[k] = json.dumps(parameters[k])
 
     while True:
-        r = requests.get(url, params=parameters,timeout=5)
+        r = requests.get(url, params=parameters, timeout=5)
         if r.status_code == 200:
             break
         else:
@@ -708,7 +732,7 @@ def daily_qq_reports_get(access_token, account_id, compaign_id, level, start_dat
         if type(parameters[k]) is not str:
             parameters[k] = json.dumps(parameters[k])
 
-    r = requests.get(url, params=parameters,timeout=5)
+    r = requests.get(url, params=parameters, timeout=5)
 
     return r.json()