|
@@ -0,0 +1,281 @@
|
|
|
+from adstoken import Ads_Api_Token
|
|
|
+import requests
|
|
|
+import datetime,time
|
|
|
+import pandas as pd
|
|
|
+import numpy as np
|
|
|
+
|
|
|
+access_tokenapi = Ads_Api_Token(shop_name='ZosiDirect', profileId='3006125408623189')
|
|
|
+access_token = access_tokenapi._get_access_token()
|
|
|
+
|
|
|
+def time_stamp_convert(df,segment:list):
|
|
|
+ for time_column in segment:
|
|
|
+ df[time_column] = pd.to_datetime(df[time_column]*1000000)
|
|
|
+ return df
|
|
|
+
|
|
|
+def placement_segmentsplit(df,segment):
|
|
|
+ df[segment] = df[segment].astype("string")
|
|
|
+ df[segment+str("_percentage")] = df[segment].str.extract("'percentage':.+([\d\.]{1,}),").astype('float32')
|
|
|
+ df[segment+str("_placement")] = df[segment].str.extract("'placement':.+'(.+)'")
|
|
|
+ df.replace(['nan','Nan','NaN'],np.nan,inplace=True)
|
|
|
+ df.drop(columns=[segment],inplace=True)
|
|
|
+ return df
|
|
|
+
|
|
|
+def columnsName_modify(df):
|
|
|
+ df.columns = [i.replace(".","_") for i in df.columns]
|
|
|
+ return df
|
|
|
+
|
|
|
+def get_profilio_info(token):
|
|
|
+ header = {'Amazon-Advertising-API-ClientId': "amzn1.application-oa2-client.ebd701cd07854fb38c37ee49ec4ba109",
|
|
|
+ 'Amazon-Advertising-API-Scope': "3006125408623189",
|
|
|
+ 'Authorization': 'Bearer ' + token}
|
|
|
+
|
|
|
+ url_ = "https://advertising-api.amazon.com/v2/portfolios/extended" # V3版本
|
|
|
+ req_time = 0
|
|
|
+ while (req_time != 3):
|
|
|
+ req_time += 1
|
|
|
+ res = requests.get(url_, headers=header) # V3版本为post请求
|
|
|
+ if res.status_code not in [200, 207]:
|
|
|
+ time.sleep(3)
|
|
|
+ else:
|
|
|
+ return res.json()
|
|
|
+ return res.text
|
|
|
+# list_portfolio = get_profilio_info(access_token)
|
|
|
+# df_portfolio = pd.json_normalize(list_portfolio)
|
|
|
+# columnsName_modify(df_portfolio)
|
|
|
+
|
|
|
+def get_campaign_info_SP(token):
|
|
|
+ header = {'Amazon-Advertising-API-ClientId': "amzn1.application-oa2-client.ebd701cd07854fb38c37ee49ec4ba109",
|
|
|
+ 'Amazon-Advertising-API-Scope': "3006125408623189",
|
|
|
+ 'Authorization': 'Bearer ' + token,
|
|
|
+ 'Content-Type': 'application/vnd.spCampaign.v3+json',
|
|
|
+ 'Accept': """application/vnd.spCampaign.v3+json"""}
|
|
|
+
|
|
|
+ url_ = "https://advertising-api.amazon.com/sp/campaigns/list" # V3版本
|
|
|
+ req_time = 0
|
|
|
+ while (req_time != 3):
|
|
|
+ req_time += 1
|
|
|
+ res = requests.post(url_, headers=header) # V3版本为post请求
|
|
|
+ print(res.status_code)
|
|
|
+ if res.status_code not in [200, 207]:
|
|
|
+ time.sleep(3)
|
|
|
+ else:
|
|
|
+ return res.json()
|
|
|
+ return res.text
|
|
|
+
|
|
|
+# list_campaign_SP = get_campaign_info_SP(access_token)
|
|
|
+# df_campaign = pd.json_normalize(list_campaign_SP['campaigns'])
|
|
|
+# df_campaign = placement_segmentsplit(df_campaign,"dynamicBidding.placementBidding")
|
|
|
+# columnsName_modify(df_campaign)
|
|
|
+
|
|
|
+def get_campaign_info_SB(token):
|
|
|
+ header = {'Amazon-Advertising-API-ClientId': "amzn1.application-oa2-client.ebd701cd07854fb38c37ee49ec4ba109",
|
|
|
+ 'Amazon-Advertising-API-Scope': "3006125408623189",
|
|
|
+ 'Authorization': 'Bearer ' + token,
|
|
|
+ 'Content-Type': 'application/vnd.sbcampaignresource.v4+json',
|
|
|
+ 'Accept': """application/vnd.sbcampaignresource.v4+json"""}
|
|
|
+
|
|
|
+ url_ = "https://advertising-api.amazon.com/sb/v4/campaigns/list"
|
|
|
+
|
|
|
+ req_time = 0
|
|
|
+ while (req_time != 3):
|
|
|
+ req_time += 1
|
|
|
+ res = requests.post(url_, headers=header)
|
|
|
+ print(res.status_code)
|
|
|
+ if res.status_code not in [200, 207]:
|
|
|
+ time.sleep(3)
|
|
|
+ else:
|
|
|
+ return res.json()
|
|
|
+ return res.text
|
|
|
+
|
|
|
+# list_campaign_SB = get_campaign_info_SB(access_token)
|
|
|
+# df_campaign_SB = pd.json_normalize(list_campaign_SB['campaigns'])
|
|
|
+# df_campaign_SB = placement_segmentsplit(df_campaign_SB,"bidding.bidAdjustmentsByPlacement")
|
|
|
+# columnsName_modify(df_campaign_SB)
|
|
|
+
|
|
|
+def get_campaign_info_SD(token):
|
|
|
+ header = {'Amazon-Advertising-API-ClientId': "amzn1.application-oa2-client.ebd701cd07854fb38c37ee49ec4ba109",
|
|
|
+ 'Amazon-Advertising-API-Scope': "3006125408623189",
|
|
|
+ 'Authorization': 'Bearer ' + token}
|
|
|
+
|
|
|
+ url_ = "https://advertising-api.amazon.com/sd/campaigns"
|
|
|
+
|
|
|
+ req_time = 0
|
|
|
+ while (req_time != 3):
|
|
|
+ req_time += 1
|
|
|
+ res = requests.get(url_, headers=header) # V3版本为post请求
|
|
|
+ print(res.status_code)
|
|
|
+ if res.status_code not in [200, 207]:
|
|
|
+ time.sleep(3)
|
|
|
+ else:
|
|
|
+ return res.json()
|
|
|
+ return res.text
|
|
|
+# list_campaign_SD = get_campaign_info_SD(access_token)
|
|
|
+# df_campaign_SD = pd.json_normalize(list_campaign_SD)
|
|
|
+# df_campaign_SD['portfolioId'] = df_campaign_SD['portfolioId'].map(lambda x: int(x) if pd.isna(x)==False else -1)
|
|
|
+# columnsName_modify(df_campaign_SD)
|
|
|
+
|
|
|
+def get_adGroup_info_SP(token):
|
|
|
+ header = {'Amazon-Advertising-API-ClientId': "amzn1.application-oa2-client.ebd701cd07854fb38c37ee49ec4ba109",
|
|
|
+ 'Amazon-Advertising-API-Scope': "3006125408623189",
|
|
|
+ 'Authorization': 'Bearer ' + access_token,
|
|
|
+ 'Content-Type': "application/vnd.spAdGroup.v3+json",
|
|
|
+ 'Accept': "application/vnd.spAdGroup.v3+json"
|
|
|
+ }
|
|
|
+
|
|
|
+ url_ = "https://advertising-api.amazon.com/sp/adGroups/list"
|
|
|
+ res = requests.post(url_, headers=header)
|
|
|
+ if res.status_code not in [200, 207]:
|
|
|
+ return res.text
|
|
|
+
|
|
|
+ token_ = res.json().get('nextToken')
|
|
|
+ temp_list = [res.json()['adGroups']]
|
|
|
+ while token_ != None:
|
|
|
+ res = requests.post(url_, headers=header, json={'nextToken': token_,"includeExtendedDataFields":True})
|
|
|
+ if res.status_code in [200, 207]:
|
|
|
+ res = res.json()
|
|
|
+ token_ = res.get('nextToken')
|
|
|
+ temp_list[0].extend(res['adGroups'])
|
|
|
+
|
|
|
+ else:
|
|
|
+ print(res.text)
|
|
|
+ break
|
|
|
+ return temp_list[0]
|
|
|
+# list_adGroup_SP = get_adGroup_info_SP(access_token)
|
|
|
+# df_adGroup_SP = pd.json_normalize(list_adGroup_SP)
|
|
|
+# columnsName_modify(df_adGroup_SP)
|
|
|
+
|
|
|
+def get_adGroup_info_SB(token):
|
|
|
+ header = {'Amazon-Advertising-API-ClientId': "amzn1.application-oa2-client.ebd701cd07854fb38c37ee49ec4ba109",
|
|
|
+ 'Amazon-Advertising-API-Scope': "3006125408623189",
|
|
|
+ 'Authorization': 'Bearer ' + access_token,
|
|
|
+ 'Content-Type': "application/vnd.sbadgroupresource.v4+json",
|
|
|
+ 'Accept': "application/vnd.sbadgroupresource.v4+json"
|
|
|
+ }
|
|
|
+
|
|
|
+ url_ = "https://advertising-api.amazon.com/sb/v4/adGroups/list" # V3版本
|
|
|
+ res = requests.post(url_, headers=header,json={"includeExtendedDataFields":True}) # V3版本为post请求
|
|
|
+ if res.status_code not in [200, 207]:
|
|
|
+ return res.text
|
|
|
+
|
|
|
+ token_ = res.json().get('nextToken')
|
|
|
+ temp_list = [res.json()['adGroups']]
|
|
|
+ while token_ != None:
|
|
|
+ res = requests.post(url_, headers=header, json={'nextToken': token_,"includeExtendedDataFields":True})
|
|
|
+ if res.status_code in [200, 207]:
|
|
|
+ res = res.json()
|
|
|
+ token_ = res.get('nextToken')
|
|
|
+ temp_list[0].extend(res['adGroups'])
|
|
|
+
|
|
|
+ else:
|
|
|
+ print(res.text)
|
|
|
+ break
|
|
|
+ return temp_list[0]
|
|
|
+
|
|
|
+
|
|
|
+# list_adGroup_SB = get_adGroup_info_SB(access_token)
|
|
|
+# df_adGroup_SB = pd.json_normalize(list_adGroup_SB)
|
|
|
+# df_adGroup_SB = time_stamp_convert(df_adGroup_SB,['extendedData.creationDate','extendedData.lastUpdateDate'])
|
|
|
+# columnsName_modify(df_adGroup_SB)
|
|
|
+
|
|
|
+def get_adGroup_info_SD(token):
|
|
|
+ header = {'Amazon-Advertising-API-ClientId': "amzn1.application-oa2-client.ebd701cd07854fb38c37ee49ec4ba109",
|
|
|
+ 'Amazon-Advertising-API-Scope': "3006125408623189",
|
|
|
+ 'Authorization': 'Bearer ' + access_token
|
|
|
+ }
|
|
|
+
|
|
|
+ url_ = "https://advertising-api.amazon.com/sd/adGroups/extended" # V3版本
|
|
|
+
|
|
|
+ res = requests.get(url_, headers=header) # ,params={"startIndex":0,"count":1}
|
|
|
+ return res.json()
|
|
|
+
|
|
|
+# list_adGroup_SD = get_adGroup_info_SD(access_token)
|
|
|
+# df_adGroup_SD = pd.json_normalize(list_adGroup_SD)
|
|
|
+# df_adGroup_SD = time_stamp_convert(df_adGroup_SD,['creationDate','lastUpdatedDate'])
|
|
|
+# columnsName_modify(df_adGroup_SD)
|
|
|
+
|
|
|
+def get_adId_info_SP(token):
|
|
|
+ header = {'Amazon-Advertising-API-ClientId': "amzn1.application-oa2-client.ebd701cd07854fb38c37ee49ec4ba109",
|
|
|
+ 'Amazon-Advertising-API-Scope': "3006125408623189",
|
|
|
+ 'Authorization': 'Bearer ' + access_token,
|
|
|
+ 'Content-Type': "application/vnd.spProductAd.v3+json",
|
|
|
+ 'Accept': "application/vnd.spProductAd.v3+json"
|
|
|
+ }
|
|
|
+ url_ = "https://advertising-api.amazon.com/sp/productAds/list"
|
|
|
+
|
|
|
+ res = requests.post(url_, headers=header, json={"includeExtendedDataFields": True}) # V3版本为post请求
|
|
|
+ if res.status_code not in [200, 207]:
|
|
|
+ return res.text
|
|
|
+ print(res.json())
|
|
|
+
|
|
|
+ token_ = res.json().get('nextToken')
|
|
|
+ temp_list = [res.json()['productAds']]
|
|
|
+ while token_ != None:
|
|
|
+ res = requests.post(url_, headers=header, json={'nextToken': token_, "includeExtendedDataFields": True})
|
|
|
+ if res.status_code in [200, 207]:
|
|
|
+ res = res.json()
|
|
|
+ print(res)
|
|
|
+ token_ = res.get('nextToken')
|
|
|
+ temp_list[0].extend(res['productAds'])
|
|
|
+
|
|
|
+ else:
|
|
|
+ print(res.text)
|
|
|
+ break
|
|
|
+ return temp_list[0]
|
|
|
+
|
|
|
+
|
|
|
+# list_adId_SP = get_adId_info_SP(access_token)
|
|
|
+# df_adId_SP = pd.json_normalize(list_adId_SP)
|
|
|
+# df_adId_SP = columnsName_modify(df_adId_SP)
|
|
|
+
|
|
|
+def get_adId_info_SB(token):
|
|
|
+ header = {'Amazon-Advertising-API-ClientId': "amzn1.application-oa2-client.ebd701cd07854fb38c37ee49ec4ba109",
|
|
|
+ 'Amazon-Advertising-API-Scope': "3006125408623189",
|
|
|
+ 'Authorization': 'Bearer ' + access_token,
|
|
|
+ 'Content-Type': "application/vnd.sbadresource.v4+json",
|
|
|
+ 'Accept': "application/vnd.sbadresource.v4+json"
|
|
|
+ }
|
|
|
+ url_ = "https://advertising-api.amazon.com/sb/v4/ads/list"
|
|
|
+
|
|
|
+ res = requests.post(url_, headers=header) # V3版本为post请求
|
|
|
+ if res.status_code not in [200, 207]:
|
|
|
+ return res.text
|
|
|
+# print(res.json())
|
|
|
+ token_ = res.json().get('nextToken')
|
|
|
+ temp_list = [res.json()['ads']]
|
|
|
+ while token_ != None:
|
|
|
+ res = requests.post(url_, headers=header, json={'nextToken': token_})
|
|
|
+ if res.status_code in [200, 207]:
|
|
|
+ res = res.json()
|
|
|
+ print(res)
|
|
|
+ token_ = res.get('nextToken')
|
|
|
+ temp_list[0].extend(res['ads'])
|
|
|
+
|
|
|
+ else:
|
|
|
+ print(res.text)
|
|
|
+ break
|
|
|
+ return temp_list[0]
|
|
|
+
|
|
|
+def SB_segmentDeal(rel):
|
|
|
+ df = pd.json_normalize(rel)
|
|
|
+ df = time_stamp_convert(df,['extendedData.creationDate','extendedData.lastUpdateDate'])
|
|
|
+ df[["creative.asin1","creative.asin2","creative.asin3"]] = df['creative.asins'].map(str).str.slice(1,-1).str.replace("'","").str.split(",",expand=True).applymap(lambda x:None if x==None else x.strip())
|
|
|
+ df.drop(columns="creative.asins",inplace=True)
|
|
|
+ df = columnsName_modify(df)
|
|
|
+ return df
|
|
|
+#
|
|
|
+# list_adId_SB = get_adId_info_SB(access_token)
|
|
|
+# df_adId_SB = SB_segmentDeal(list_adId_SB)
|
|
|
+
|
|
|
+def get_adId_info_SD(token):
|
|
|
+ header = {'Amazon-Advertising-API-ClientId': "amzn1.application-oa2-client.ebd701cd07854fb38c37ee49ec4ba109",
|
|
|
+ 'Amazon-Advertising-API-Scope': "3006125408623189",
|
|
|
+ 'Authorization': 'Bearer ' + access_token
|
|
|
+ }
|
|
|
+ url_ = "https://advertising-api.amazon.com/sd/productAds/extended"
|
|
|
+
|
|
|
+ res = requests.get(url_, headers=header)
|
|
|
+ return res.json()
|
|
|
+
|
|
|
+
|
|
|
+# list_adId_SD = get_adId_info_SD(access_token)
|
|
|
+# df_adId_SD = pd.json_normalize(list_adId_SD)
|