huangyifan 1 anno fa
parent
commit
767885a456
2 ha cambiato i file con 278 aggiunte e 271 eliminazioni
  1. 273 266
      sync_amz_data/public/SP_SB_SD_jsonDeal.py
  2. 5 5
      sync_amz_data/public/amz_ad_client.py

+ 273 - 266
sync_amz_data/public/SP_SB_SD_jsonDeal.py

@@ -7,275 +7,282 @@ 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]:
+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
 
-    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]:
+
+    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
 
-    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]:
+
+
+    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
-    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]:
+
+
+    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
-#     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)
+
+
+    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())
+
+
+

+ 5 - 5
sync_amz_data/public/amz_ad_client.py

@@ -266,8 +266,8 @@ if __name__ == '__main__':
         'unitsSold14d',
         'attributedBrandedSearches14d',
         'topOfSearchImpressionShare']
-    sb.get_report(
-        record_type="campaigns",
-        report_date="20231008",
-        metrics=metrics
-    )
+    # sb.get_report(
+    #     record_type="campaigns",
+    #     report_date="20231008",
+    #     metrics=metrics
+    # )