Ver código fonte

Merge branch 'yifan' of ASJ_ADS/sync_amz_data into master

yifan_huang96 1 ano atrás
pai
commit
cce19e443c
1 arquivos alterados com 47 adições e 25 exclusões
  1. 47 25
      sync_amz_data/public/sp_api_client.py

+ 47 - 25
sync_amz_data/public/sp_api_client.py

@@ -791,7 +791,7 @@ class SpApiRequest:
     def listing_infoTable(cls):
         conn = SpApiRequest.mysql_connect_auth_lst()
         cursor = conn.cursor()
-        cursor.execute(f"""select seller_id,country_code,asin,title from asj_ads.seller_listings where title is not null and title <>'' and (seller_id,country_code,asin) not in (select seller_id,countryCode,asin from asj_ads.Goods where update_time>='{datetime.today().date()}') group by seller_id,title,country_code,asin""")
+        cursor.execute(f"""select seller_id,country_code,asin,title from asj_ads.seller_listings where title is not null and title <>'' and (seller_id,country_code,asin) not in (select seller_id,countryCode,asin from asj_ads.Goods where update_time>='{datetime.today().date()}') group by seller_id,title,country_code,asin order by country_code desc""")
         query_ = cursor.fetchall()
         # print(query_)
         col_name = [i[0] for i in cursor.description]
@@ -854,16 +854,18 @@ class SpApiRequest:
                'BR': Marketplaces.BR, 'CA': Marketplaces.CA, 'MX': Marketplaces.MX
                }
 
-        cate_item = CatalogItems(credentials=aws_credentials, marketplace=mak[countryCode])
+        cate_item = CatalogItems(credentials=aws_credentials, marketplace=mak[countryCode],version='2022-04-01')
         try:
             variations_info = SpApiRequest.variations_judge(cate_item, asin)
         except:
             time.sleep(2.5)
             variations_info = SpApiRequest.variations_judge(cate_item, asin)
         try:
+            cate_item = CatalogItems(credentials=aws_credentials, marketplace=mak[countryCode], version='2020-12-01')
             detail_info = SpApiRequest.get_detail_cat(cate_item, asin, mak, countryCode)
         except:
             time.sleep(2.5)
+            cate_item = CatalogItems(credentials=aws_credentials, marketplace=mak[countryCode], version='2020-12-01')
             detail_info = SpApiRequest.get_detail_cat(cate_item, asin, mak, countryCode)
         # print(countryCode,asin,detail_info,variations_info,)
         detail_info.update(variations_info)
@@ -877,41 +879,56 @@ class SpApiRequest:
     @staticmethod
     def variations_judge(cate_item, asin):
         try:
-            variations = cate_item.get_catalog_item(asin=asin, **{"includedData": ['variations']})
+            variations = cate_item.get_catalog_item(asin=asin, **{"includedData": ['relationships']})#'variations',
             var_info = variations.payload
             # print(variations)
             IsParent = 'Y'
             parent_asin = ''
-            if len(var_info['variations']) > 0:
-                variationType = var_info['variations'][0]['variationType']
-                # print(variationType)
-                if variationType == "CHILD":
-                    parent_asin = var_info['variations'][0]['asins']
+            try:
+                relationships = var_info.get("relationships")[0]['relationships']
+            except:
+                relationships = []
+
+            if len(relationships) > 0:
+                variationType = relationships[0]['type']
+                if relationships[0].get('parentAsins') is not None:
+                    parent_asin = relationships[0]['parentAsins'][0]
                     IsParent = 'N'
+                elif relationships[0].get('childAsins') is not None:
+                    IsParent = 'Y'
+                    parent_asin = asin
                 else:
-                    parent_asin = variations.payload['asin']
+                    parent_asin = 'Erro_01'
             else:
                 IsParent = 'SG'
-                parent_asin = variations.payload['asin']
+                parent_asin = asin
+            print(IsParent,'父asin:',parent_asin,'子asin',asin)
             return {"IsParent": IsParent, "parent_asin": parent_asin}
         except:
             try:
-                time.sleep(3.5)
-                variations = cate_item.get_catalog_item(asin=asin, **{"includedData": ['variations']})
+                variations = cate_item.get_catalog_item(asin=asin,
+                                                        **{"includedData": ['relationships']})  # 'variations',
                 var_info = variations.payload
                 IsParent = 'Y'
                 parent_asin = ''
-                if len(var_info['variations']) > 0:
-                    variationType = var_info['variations'][0]['variationType']
-                    # print(variationType)
-                    if variationType == "CHILD":
-                        parent_asin = var_info['variations'][0]['asins']
+                try:
+                    relationships = var_info.get("relationships")[0]['relationships']
+                except:
+                    relationships = []
+
+                if len(relationships) > 0:
+                    if relationships[0].get('parentAsins') is not None:
+                        parent_asin = relationships[0]['parentAsins'][0]
+                        print(parent_asin)
                         IsParent = 'N'
+                    elif relationships[0].get('childAsins') is not None:
+                        IsParent = 'Y'
+                        parent_asin = asin
                     else:
-                        parent_asin = variations.payload['asin']
+                        parent_asin = 'Erro_01'
                 else:
                     IsParent = 'SG'
-                    parent_asin = variations.payload['asin']
+                    parent_asin = asin
                 return {"IsParent": IsParent, "parent_asin": parent_asin}
             except Exception as e:
                 print("判断是否为父子asin时出错:", e)
@@ -1041,9 +1058,13 @@ class SpApiRequest:
     @classmethod
     def get_allShops(cls,data_type=Literal["GET_FLAT_FILE_OPEN_LISTINGS_DATA","GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL"],days=-1,**kwargs):
         df = cls.auth_info()
-        refreshtoken_list = (df['refresh_token'].to_numpy().tolist())
-        shuffle(refreshtoken_list)
-        # print(type)
+        zosi = df.query("account_name=='AM-ZOSI-US'")['refresh_token'].to_numpy().tolist()
+        print(zosi)
+        refreshtoken_list = df.query("account_name!='AM-ZOSI-US'")['refresh_token'].to_numpy().tolist()
+        zosi.extend(refreshtoken_list)
+        refreshtoken_list = zosi.copy()
+        print(len(refreshtoken_list))
+        # refreshtoken_list = refreshtoken_list+refreshtoken_li
         a_kw = kwargs
         for refresh_token in refreshtoken_list:
             aws_credentials = {
@@ -1064,6 +1085,7 @@ class SpApiRequest:
                 for marketplace in [Marketplaces.US, Marketplaces.BR, Marketplaces.CA,Marketplaces.MX]:
                     sp_api = SpApiRequest(aws_credentials, marketplace)
                     a_kw['countryCode'] = str(marketplace)[-2:]
+                    print(a_kw)
                     try:
                         auth_conn = SpApiRequest.mysql_connect_auth()
                         # print(a_kw)
@@ -1099,9 +1121,9 @@ class SpApiRequest:
             ## sp_api.GET_FLAT_FILE_OPEN_LISTINGS_DATA(auth_conn, seller_id)
 
 if __name__ == '__main__':
-    for days in range(35,45):
-        SpApiRequest.get_allShops("GET_SALES_AND_TRAFFIC_REPORT",days=-days,**{"level":"SKU"})
+    # for days in range(35,45):
+        # SpApiRequest.get_allShops("GET_SALES_AND_TRAFFIC_REPORT",days=-days,**{"level":"SKU"})
     # SpApiRequest.listing_infoTable()
     # rel = SpApiRequest.get_catelog(account_name='ANLAPUS_US',country=Marketplaces.US,asin='B0BVXB4KT9')
     # print(rel)
-
+    pass