Explorar el Código

Merge branch 'yifan' of ASJ_ADS/sync_amz_data into master

yifan_huang96 hace 1 año
padre
commit
77ba39fb3c
Se han modificado 1 ficheros con 68 adiciones y 15 borrados
  1. 68 15
      sync_amz_data/public/sp_api_client.py

+ 68 - 15
sync_amz_data/public/sp_api_client.py

@@ -16,7 +16,7 @@ import time
 from sync_amz_data.public.amz_ad_client import shop_infos
 from dateutil.parser import parse
 import pymysql
-
+from typing import List, Literal
 
 
 class SpApiRequest:
@@ -37,7 +37,8 @@ class SpApiRequest:
         port=3306)
         return conn
 
-    def mysql_connect(self):
+    @classmethod
+    def mysql_connect(cls):
         conn = pymysql.connect(user="huangyifan",
                                password="123456",
                                host="192.168.1.18",
@@ -45,7 +46,14 @@ class SpApiRequest:
                                port=3306)
         return conn
 
-
+    @classmethod
+    def mysql_adTest_connect(cls):
+        conn = pymysql.connect(user="root",
+                               password="sandbox",
+                               host="192.168.1.225",
+                               database="asj_ads",
+                               port=3306)
+        return conn
 
     def create_report(self,**kwargs):
         reportType = kwargs['reportType']
@@ -476,14 +484,32 @@ class SpApiRequest:
                 conn.rollback()
                 return '出错回滚'
 
-    @classmethod
-    def get_allShops(cls):
+    @staticmethod
+    def auth_info():
         auth_conn = SpApiRequest.mysql_connect_auth()
         cursor = auth_conn.cursor()
         cursor.execute("select * from amazon_sp_report.amazon_sp_auth_info;")
         columns_name = [i[0] for i in cursor.description]
         rel = cursor.fetchall()
         df = pd.DataFrame(rel, columns=columns_name)
+        return df
+
+    @classmethod
+    def get_orders_allShops(cls):
+        pass
+
+    @staticmethod
+    def data_judge(sp_api,data_type,seller_id,auth_conn):
+        if data_type == "GET_FLAT_FILE_OPEN_LISTINGS_DATA":
+            return sp_api.GET_FLAT_FILE_OPEN_LISTINGS_DATA(auth_conn,seller_id)
+        elif data_type =="GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL":
+            return sp_api.GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL(seller_id)
+        else:
+            return ""
+
+    @classmethod
+    def get_allShops(cls,data_type=Literal["GET_FLAT_FILE_OPEN_LISTINGS_DATA","GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL"]):
+        df = cls.auth_info()
         refreshtoken_list = (df['refresh_token'].to_numpy().tolist())
         refreshtoken_list.reverse()
         for refresh_token in refreshtoken_list:
@@ -506,7 +532,8 @@ class SpApiRequest:
                     sp_api = SpApiRequest(aws_credentials, marketplace)
                     try:
                         auth_conn = SpApiRequest.mysql_connect_auth()
-                        sp_api.GET_FLAT_FILE_OPEN_LISTINGS_DATA(auth_conn, seller_id)
+                        cls.data_judge(sp_api, data_type, seller_id, auth_conn)
+                        ## sp_api.GET_FLAT_FILE_OPEN_LISTINGS_DATA(auth_conn, seller_id)
                     except Exception as e:
                         print(e)
             elif region_circle == 'EU':
@@ -518,15 +545,19 @@ class SpApiRequest:
                     sp_api = SpApiRequest(aws_credentials, marketplace)
                     try:
                         auth_conn = SpApiRequest.mysql_connect_auth()
-                        sp_api.GET_FLAT_FILE_OPEN_LISTINGS_DATA(auth_conn, seller_id)
+                        cls.data_judge(sp_api, data_type, seller_id, auth_conn)
+                        ## sp_api.GET_FLAT_FILE_OPEN_LISTINGS_DATA(auth_conn, seller_id)
                     except Exception as e:
                         print(e)
 
             else:
+            # if region_circle not in ['NA','EU']:
                 auth_conn = SpApiRequest.mysql_connect_auth()
+                print(region_circle)
                 marketplace = eval(f'Marketplaces.{region_circle}')
                 sp_api = SpApiRequest(aws_credentials, marketplace)
-                sp_api.GET_FLAT_FILE_OPEN_LISTINGS_DATA(auth_conn, seller_id)
+                cls.data_judge(sp_api, data_type, seller_id, auth_conn)
+            ## sp_api.GET_FLAT_FILE_OPEN_LISTINGS_DATA(auth_conn, seller_id)
 
     def timeDeal(self, orgTime):
         orgTime = parse(orgTime)
@@ -538,34 +569,56 @@ class SpApiRequest:
 
     def GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL(self,seller_id):
         # timezone_ = pytz.timezone(self.timezone)
-        shopReportday = (datetime.now() + timedelta(days=-2)).strftime("%Y-%m-%d")
+        shopReportday = (datetime.now() + timedelta(days=-1)).strftime("%Y-%m-%d")
         # print(shopReportday)
         para = {"reportType":ReportType.GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL,"dataStartTime":shopReportday,"dataEndTime":shopReportday,"reportOptions":{"ShowSalesChannel":"true"}}
         reportid = self.create_report(**para) #{"ShowSalesChannel":"true"}
         decom_df = self.decompression(reportid)
-
         decom_df[decom_df.select_dtypes(float).columns] = decom_df[decom_df.select_dtypes(float).columns].fillna(0.0)
         decom_df[decom_df.select_dtypes(int).columns] = decom_df[decom_df.select_dtypes(int).columns].fillna(0)
         decom_df[decom_df.select_dtypes(datetime).columns] = decom_df[decom_df.select_dtypes(datetime).columns].astype('string')
         if "purchase-order-number" in decom_df.columns:
             decom_df['purchase-order-number'] = decom_df['purchase-order-number'].astype("string")
+
         decom_df.fillna('',inplace=True)
         # decom_df.to_csv('order.csv')
         decom_df["ReportDate"] = parse(shopReportday)
-        decom_df['timezone'] =  decom_df[["purchase-date"]].map(lambda x: parse(x).tzname()).fillna(method='bfill')
+        decom_df['timezone'] =  decom_df["purchase-date"].map(lambda x: parse(x).tzname()).fillna(method='bfill')
+        print("==========================================================")
         decom_df[["purchase-date", "last-updated-date"]] = decom_df[["purchase-date", "last-updated-date"]].applymap(
             lambda x: self.timeDeal(x) if pd.isna(x) == False or x != None else x)
+        if 'is-business-order' not in decom_df.columns:
+            decom_df['is-business-order'] = '-'
+        if 'purchase-order-number' not in decom_df.columns:
+            decom_df['purchase-order-number'] = '-'
+        if 'price-designation' not in decom_df.columns:
+            decom_df['price-designation'] = '-'
 
         decom_df['seller_id'] = seller_id
-        list_df = decom_df.to_numpy().tolist()
+        country_code = str(self.marketplace)[-2:]
+        if country_code=='GB':
+            country_code="UK"
+            decom_df['country_code'] = "UK"
+        decom_df['country_code'] = country_code
+        # print(decom_df[])
+        reserve_columns = ["amazon-order-id","merchant-order-id","purchase-date","last-updated-date","order-status",
+                        "fulfillment-channel","sales-channel","order-channel","ship-service-level","product-name",
+                        "sku","asin","item-status","quantity","currency","item-price","item-tax","shipping-price",
+                        "shipping-tax","gift-wrap-price","gift-wrap-tax","item-promotion-discount",
+                        "ship-promotion-discount","ship-city","ship-state","ship-postal-code","ship-country",
+                        "promotion-ids","is-business-order","purchase-order-number","price-designation","ReportDate",
+                        "timezone","seller_id","country_code"
+                        ]
+        list_df = decom_df[reserve_columns].to_numpy().tolist()
+        # print(list_df)
         print(list_df[0])
         # tuple_data = [tuple(i) for i in list_df]
         conn = self.mysql_connect()
         cursor = conn.cursor()
         # print(list(conn.query("select * from amz_sp_api.orderReport")))
         sql = f"""
-            insert into amz_sp_api.orderReport
-            values (%s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s,%s)
+            insert into amz_sp_api.orderreport_renew1
+            values (%s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s)
         """ #ok
         # print(sql)
         try:
@@ -579,7 +632,7 @@ class SpApiRequest:
 
 
 if __name__ == '__main__':
-    SpApiRequest.get_allShops()
+    SpApiRequest.get_allShops("GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL")