Explorar el Código

Merge branch 'yifan' of ASJ_ADS/sync_amz_data into master

yifan_huang96 hace 1 año
padre
commit
cb74cb7473
Se han modificado 1 ficheros con 32 adiciones y 55 borrados
  1. 32 55
      sync_amz_data/public/sp_api_client.py

+ 32 - 55
sync_amz_data/public/sp_api_client.py

@@ -278,6 +278,7 @@ class SpApiRequest:
     #     decom_df.fillna('', inplace=True)
     #     # print(decom_df.info())
     #     return decom_df
+
     def fba_inventorySQL(self,conn,seller_id):
         cursor = conn.cursor()
         # 执行语句
@@ -294,43 +295,35 @@ class SpApiRequest:
                                 'afn-fulfillable-quantity']
         return df_datatable
 
+    def get_fba_neccessary_segment(self,conn,seller_id):
+        para = {"reportType": ReportType.GET_FBA_MYI_UNSUPPRESSED_INVENTORY_DATA}
+        reportid = self.create_report(**para)
+        df = self.decompression(reportid)
+        if len(df) == 0:
+            return self.fba_inventorySQL(conn, seller_id)
+            # pd.DataFrame()
+        df['seller_id'] = seller_id
+        df['marketplace_id'] = self.marketplace.marketplace_id
+        df['country_code'] = str(self.marketplace)[-2:]
+        df_rel = df.query("condition=='New'")
+        df_rel = df_rel.groupby(['asin', 'sku', 'seller_id', 'marketplace_id', 'country_code']).agg(
+            {'afn-fulfillable-quantity': sum}).reset_index()
+        df_rel.columns = ['asin_', 'sku_', 'seller_id_', 'marketplace_id_', 'country_code_', 'afn-fulfillable-quantity']
+        print(f"{seller_id}_{str(self.marketplace)[-2:]}_FBA_Inventory_OK")
+        return df_rel
+
     def GET_FBA_MYI_UNSUPPRESSED_INVENTORY_DATA(self,refresh_token,conn=None,seller_id=None,days=-1,**kwargs): # FBA库存信息
         try:
-            para = {"reportType": ReportType.GET_FBA_MYI_UNSUPPRESSED_INVENTORY_DATA}
-            reportid = self.create_report(**para)
-            df = self.decompression(reportid)
-            if len(df)==0:
-                return self.fba_inventorySQL(conn,seller_id)
-                # pd.DataFrame()
-            df['seller_id'] = seller_id
-            df['marketplace_id'] = self.marketplace.marketplace_id
-            df['country_code'] = str(self.marketplace)[-2:]
-            df_rel = df.query("condition=='New'")
-            df_rel = df_rel.groupby(['asin', 'sku', 'seller_id', 'marketplace_id', 'country_code']).agg({'afn-fulfillable-quantity':sum}).reset_index()
-            df_rel.columns = ['asin_', 'sku_', 'seller_id_', 'marketplace_id_', 'country_code_','afn-fulfillable-quantity']
-            print(f"{seller_id}_{str(self.marketplace)[-2:]}_FBA_Inventory_OK")
+            return self.get_fba_neccessary_segment(conn,seller_id)
         except Exception as e:
             print(e)
             try:
                 time.sleep(15)
-                para = {"reportType": ReportType.GET_FBA_MYI_UNSUPPRESSED_INVENTORY_DATA}
-                reportid = self.create_report(**para)
-
-                df = self.decompression(reportid)
-                if len(df) == 0:
-                    return self.fba_inventorySQL(conn, seller_id)
-                df['seller_id'] = seller_id
-                df['marketplace_id'] = self.marketplace.marketplace_id
-                df['country_code'] = str(self.marketplace)[-2:]
-                df_rel = df.query("condition=='New'")
-                df_rel = df_rel.groupby(['asin', 'sku', 'seller_id', 'marketplace_id', 'country_code']).agg(
-                    {'afn-fulfillable-quantity': sum}).reset_index()
-                df_rel.columns = ['asin_', 'sku_', 'seller_id_', 'marketplace_id_', 'country_code_','afn-fulfillable-quantity']
-                print(f"{seller_id}_{str(self.marketplace)[-2:]}_FBA_Inventory_OK")
+                return self.get_fba_neccessary_segment(conn, seller_id)
             except Exception as e:
                 print(e)
                 df_rel = pd.DataFrame(columns=['asin_', 'sku_', 'seller_id_', 'marketplace_id_', 'country_code_','afn-fulfillable-quantity'])
-        return df_rel
+                return df_rel
 
 
     def GET_FLAT_FILE_OPEN_LISTINGS_DATA(self,refresh_token,conn=None,seller_id=None,days=-1): # To datatable asj_ads.seller_listings- listing信息,包括fbm库存
@@ -988,10 +981,13 @@ class SpApiRequest:
         detail_info['update_time'] = datetime.now()
         return detail_info
 
+
+
+
     @staticmethod
     def variations_judge(cate_item, asin): # used in listing_infoTable, 判断是否有父子关系
-        try:
-            variations = cate_item.get_catalog_item(asin=asin, **{"includedData": ['relationships']})#'variations',
+        def temp_func(cate_item, asin):
+            variations = cate_item.get_catalog_item(asin=asin, **{"includedData": ['relationships']})  # 'variations',
             var_info = variations.payload
             # print(variations)
             IsParent = 'Y'
@@ -1014,35 +1010,16 @@ class SpApiRequest:
             else:
                 IsParent = 'SG'
                 parent_asin = asin
-            print(IsParent,'父asin:',parent_asin,'子asin',asin)
+            print(IsParent, '父asin:', parent_asin, '子asin', asin)
             return {"IsParent": IsParent, "parent_asin": parent_asin}
+
+
+        try:
+            return temp_func(cate_item, asin)
         except:
             try:
                 time.sleep(12)
-                variations = cate_item.get_catalog_item(asin=asin,
-                                                        **{"includedData": ['relationships']})  # 'variations',
-                var_info = variations.payload
-                IsParent = 'Y'
-                parent_asin = ''
-                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 = 'Erro_01'
-                else:
-                    IsParent = 'SG'
-                    parent_asin = asin
-                return {"IsParent": IsParent, "parent_asin": parent_asin}
+                return temp_func(cate_item, asin)
             except Exception as e:
                 print("判断是否为父子asin时出错:", e)
                 return {"IsParent": 'Erro', "parent_asin": 'Erro'}