|  | @@ -1,286 +0,0 @@
 | 
	
		
			
				|  |  | -from sync_amz_data.public.adstoken_OAUTH 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()
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -class DataDeal:
 | 
	
		
			
				|  |  | -    def __int__(self):
 | 
	
		
			
				|  |  | -        self.public_url = "https://advertising-api.amazon.com"
 | 
	
		
			
				|  |  | -        self.ClientId = "amzn1.application-oa2-client.ebd701cd07854fb38c37ee49ec4ba109"
 | 
	
		
			
				|  |  | -        self.Scope = "3006125408623189"
 | 
	
		
			
				|  |  | -    def token_(self):
 | 
	
		
			
				|  |  | -        access_tokenapi = Ads_Api_Token(shop_name='ZosiDirect', profileId='3006125408623189')
 | 
	
		
			
				|  |  | -        access_token = access_tokenapi._get_access_token()
 | 
	
		
			
				|  |  | -        Authorization = 'Bearer ' + access_token
 | 
	
		
			
				|  |  | -        return Authorization
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    def time_stamp_convert(self,df,segment:list):
 | 
	
		
			
				|  |  | -        for time_column in segment:
 | 
	
		
			
				|  |  | -            df[time_column] = pd.to_datetime(df[time_column]*1000000)
 | 
	
		
			
				|  |  | -        return df
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    def placement_segmentsplit(self,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(self,df):
 | 
	
		
			
				|  |  | -        df.columns = [i.replace(".","_") for i in df.columns]
 | 
	
		
			
				|  |  | -        return df
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    def get_profilio_info(self,token):
 | 
	
		
			
				|  |  | -        header = {'Amazon-Advertising-API-ClientId': self.ClientId,
 | 
	
		
			
				|  |  | -                  'Amazon-Advertising-API-Scope': self.Scope,
 | 
	
		
			
				|  |  | -                  'Authorization': self.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:
 | 
	
		
			
				|  |  | -                list_portfolio =  res.json()
 | 
	
		
			
				|  |  | -                df_portfolio = pd.json_normalize(list_portfolio)
 | 
	
		
			
				|  |  | -                return self.columnsName_modify(df_portfolio)
 | 
	
		
			
				|  |  | -        return res.text
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    def get_campaign_info_SP(self,token):
 | 
	
		
			
				|  |  | -        header = {'Amazon-Advertising-API-ClientId': self.ClientId,
 | 
	
		
			
				|  |  | -                  'Amazon-Advertising-API-Scope': self.Scope,
 | 
	
		
			
				|  |  | -                  'Authorization': self.token_(),
 | 
	
		
			
				|  |  | -                  'Content-Type': 'application/vnd.spCampaign.v3+json',
 | 
	
		
			
				|  |  | -                  'Accept': """application/vnd.spCampaign.v3+json"""}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        url_ = self.public_url+"/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:
 | 
	
		
			
				|  |  | -                list_campaign_SP = res.json()
 | 
	
		
			
				|  |  | -                df_campaign = pd.json_normalize(list_campaign_SP['campaigns'])
 | 
	
		
			
				|  |  | -                df_campaign = self.placement_segmentsplit(df_campaign,"dynamicBidding.placementBidding")
 | 
	
		
			
				|  |  | -                return self.columnsName_modify(df_campaign)
 | 
	
		
			
				|  |  | -        return res.text
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    def get_campaign_info_SB(self,token):
 | 
	
		
			
				|  |  | -        header = {'Amazon-Advertising-API-ClientId': self.ClientId,
 | 
	
		
			
				|  |  | -                  'Amazon-Advertising-API-Scope': self.Scope,
 | 
	
		
			
				|  |  | -                  'Authorization':  self.token_(),
 | 
	
		
			
				|  |  | -                  'Content-Type': 'application/vnd.sbcampaignresource.v4+json',
 | 
	
		
			
				|  |  | -                  'Accept': """application/vnd.sbcampaignresource.v4+json"""}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        url_ = self.public_url+"/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:
 | 
	
		
			
				|  |  | -                list_campaign_SB = res.json()
 | 
	
		
			
				|  |  | -                df_campaign_SB = pd.json_normalize(list_campaign_SB['campaigns'])
 | 
	
		
			
				|  |  | -                df_campaign_SB = self.placement_segmentsplit(df_campaign_SB,"bidding.bidAdjustmentsByPlacement")
 | 
	
		
			
				|  |  | -                return self.columnsName_modify(df_campaign_SB)
 | 
	
		
			
				|  |  | -        return res.text
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    def get_campaign_info_SD(self,token):
 | 
	
		
			
				|  |  | -        header = {'Amazon-Advertising-API-ClientId': self.ClientId,
 | 
	
		
			
				|  |  | -                  'Amazon-Advertising-API-Scope': self.Scope,
 | 
	
		
			
				|  |  | -                  'Authorization':  self.token_(),}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        url_ = self.public_url+"/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:
 | 
	
		
			
				|  |  | -                list_campaign_SD = res.json()
 | 
	
		
			
				|  |  | -                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)
 | 
	
		
			
				|  |  | -                return self.columnsName_modify(df_campaign_SD)
 | 
	
		
			
				|  |  | -        return res.text
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    def get_adGroup_info_SP(self,token):
 | 
	
		
			
				|  |  | -        header = {'Amazon-Advertising-API-ClientId': self.ClientId,
 | 
	
		
			
				|  |  | -                  'Amazon-Advertising-API-Scope': self.Scope,
 | 
	
		
			
				|  |  | -                  'Authorization':  self.token_(),
 | 
	
		
			
				|  |  | -                  'Content-Type': "application/vnd.spAdGroup.v3+json",
 | 
	
		
			
				|  |  | -                  'Accept': "application/vnd.spAdGroup.v3+json"
 | 
	
		
			
				|  |  | -                  }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        url_ = self.public_url+"/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
 | 
	
		
			
				|  |  | -        list_adGroup_SP = temp_list[0]
 | 
	
		
			
				|  |  | -        df_adGroup_SP = pd.json_normalize(list_adGroup_SP)
 | 
	
		
			
				|  |  | -        return self.columnsName_modify(df_adGroup_SP)
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    def get_adGroup_info_SB(self,token):
 | 
	
		
			
				|  |  | -        header = {'Amazon-Advertising-API-ClientId': self.ClientId,
 | 
	
		
			
				|  |  | -                  'Amazon-Advertising-API-Scope': self.Scope,
 | 
	
		
			
				|  |  | -                  'Authorization':  self.token_(),
 | 
	
		
			
				|  |  | -                  'Content-Type': "application/vnd.sbadgroupresource.v4+json",
 | 
	
		
			
				|  |  | -                  'Accept': "application/vnd.sbadgroupresource.v4+json"
 | 
	
		
			
				|  |  | -                  }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        url_ = self.public_url+"/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
 | 
	
		
			
				|  |  | -        list_adGroup_SB = temp_list[0]
 | 
	
		
			
				|  |  | -        df_adGroup_SB = pd.json_normalize(list_adGroup_SB)
 | 
	
		
			
				|  |  | -        df_adGroup_SB = self.time_stamp_convert(df_adGroup_SB,['extendedData.creationDate','extendedData.lastUpdateDate'])
 | 
	
		
			
				|  |  | -        return self.columnsName_modify(df_adGroup_SB)
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    def get_adGroup_info_SD(self,token):
 | 
	
		
			
				|  |  | -        header = {'Amazon-Advertising-API-ClientId': self.ClientId,
 | 
	
		
			
				|  |  | -                  'Amazon-Advertising-API-Scope': self.Scope,
 | 
	
		
			
				|  |  | -                  'Authorization':  self.token_(),
 | 
	
		
			
				|  |  | -                  }
 | 
	
		
			
				|  |  | -        url_ = self.public_url+"/sd/adGroups/extended"  # V3版本
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        res = requests.get(url_, headers=header)  # ,params={"startIndex":0,"count":1}
 | 
	
		
			
				|  |  | -        #
 | 
	
		
			
				|  |  | -        list_adGroup_SD = res.json()
 | 
	
		
			
				|  |  | -        df_adGroup_SD = pd.json_normalize(list_adGroup_SD)
 | 
	
		
			
				|  |  | -        df_adGroup_SD = self.time_stamp_convert(df_adGroup_SD,['creationDate','lastUpdatedDate'])
 | 
	
		
			
				|  |  | -        return self.columnsName_modify(df_adGroup_SD)
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    def get_adId_info_SP(self,token):
 | 
	
		
			
				|  |  | -        header = {'Amazon-Advertising-API-ClientId': self.ClientId,
 | 
	
		
			
				|  |  | -                  'Amazon-Advertising-API-Scope': self.Scope,
 | 
	
		
			
				|  |  | -                  'Authorization':  self.token_(),
 | 
	
		
			
				|  |  | -                  'Content-Type': "application/vnd.spProductAd.v3+json",
 | 
	
		
			
				|  |  | -                  'Accept': "application/vnd.spProductAd.v3+json"
 | 
	
		
			
				|  |  | -                  }
 | 
	
		
			
				|  |  | -        url_ = self.public_url+"/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
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        list_adId_SP = temp_list[0]
 | 
	
		
			
				|  |  | -        df_adId_SP = pd.json_normalize(list_adId_SP)
 | 
	
		
			
				|  |  | -        return self.columnsName_modify(df_adId_SP)
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    def get_adId_info_SB(self,token):
 | 
	
		
			
				|  |  | -        header = {'Amazon-Advertising-API-ClientId': self.ClientId,
 | 
	
		
			
				|  |  | -                  'Amazon-Advertising-API-Scope': self.Scope,
 | 
	
		
			
				|  |  | -                  'Authorization':  self.token_(),
 | 
	
		
			
				|  |  | -                  'Content-Type': "application/vnd.sbadresource.v4+json",
 | 
	
		
			
				|  |  | -                  'Accept': "application/vnd.sbadresource.v4+json"
 | 
	
		
			
				|  |  | -                  }
 | 
	
		
			
				|  |  | -        url_ = self.public_url+"/sb/v4/ads/list"
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        res = requests.post(url_, headers=header)  # V3版本为post请求
 | 
	
		
			
				|  |  | -        if res.status_code not in [200, 207]:
 | 
	
		
			
				|  |  | -            return res.text
 | 
	
		
			
				|  |  | -        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
 | 
	
		
			
				|  |  | -        list_adId_SB = temp_list[0]
 | 
	
		
			
				|  |  | -        return self.SB_segmentDeal(list_adId_SB)
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    def SB_segmentDeal(self,rel):
 | 
	
		
			
				|  |  | -        df = pd.json_normalize(rel)
 | 
	
		
			
				|  |  | -        df = self.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 = self.columnsName_modify(df)
 | 
	
		
			
				|  |  | -        return df
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    def get_adId_info_SD(self,token):
 | 
	
		
			
				|  |  | -        header = {'Amazon-Advertising-API-ClientId': self.ClientId,
 | 
	
		
			
				|  |  | -                  'Amazon-Advertising-API-Scope': self.Scope,
 | 
	
		
			
				|  |  | -                  'Authorization':  self.token_(),
 | 
	
		
			
				|  |  | -                  }
 | 
	
		
			
				|  |  | -        url_ = self.public_url+"/sd/productAds/extended"
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        res = requests.get(url_, headers=header)
 | 
	
		
			
				|  |  | -        return pd.json_normalize(res.json())
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 |