Pārlūkot izejas kodu

To online environment

huangyifan 1 gadu atpakaļ
vecāks
revīzija
3e7f55ae95

+ 19 - 28
sync_amz_data/public/sp_api_client.py

@@ -17,6 +17,7 @@ from dateutil.parser import parse
 import pymysql
 from typing import List, Literal
 from random import shuffle
+from ..settings import MYSQL_AUTH_CONF, MYSQL_DATA_CONF
 
 class SpApiRequest:
 
@@ -29,26 +30,13 @@ class SpApiRequest:
 
     @classmethod
     def mysql_connect_auth(cls):
-        conn = pymysql.connect(user="admin",
-        password="NSYbBSPbkGQUbOSNOeyy",
-        host="retail-data.cnrgrbcygoap.us-east-1.rds.amazonaws.com",
-        database="ansjer_dvadmin",
-        port=3306)
+        conn = pymysql.connect(**MYSQL_AUTH_CONF)
 
         return conn
 
     @classmethod
     def mysql_connect_auth_lst(cls):
-        conn = pymysql.connect(user="root",
-                               password="sandbox",
-                               host="192.168.1.225",
-                               database="asj_ads",
-                               port=3306)
-        # conn = pymysql.connect(user="huangyifan",
-        #                        password="123456",
-        #                        host="127.0.0.1",
-        #                        database="amz_sp_api",
-        #                        port=3306)
+        conn = pymysql.connect(**MYSQL_DATA_CONF)
         return conn
 
     @classmethod
@@ -356,8 +344,8 @@ class SpApiRequest:
             # merged_df['temp_columns'] = merged_df.apply(lambda x: self.get_listing_info(x['seller-sku'],seller_id),axis=1)
             # merged_df[['image_link','title']] = merged_df['temp_columns'].str.split("-----",expand=True)
             merged_df['image_link'] = ''
-            merged_df['title'] = ''
-            merged_df['modifier'] = refresh_token
+            merged_df['title'] = refresh_token
+            merged_df['modifier'] = ''
 
 
             merged_df.fillna('',inplace=True)
@@ -768,14 +756,14 @@ class SpApiRequest:
             cursor = conn.cursor()
         # print(list(conn.query("select * from amz_sp_api.orderReport")))
         sql = f"""
-            insert into asj_ads.orderReport_(`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`,
+            insert into asj_ads.orderReport_(`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`,`insert_time`)
             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)
@@ -803,8 +791,9 @@ class SpApiRequest:
     def listing_infoTable(cls):
         conn = SpApiRequest.mysql_connect_auth_lst()
         cursor = conn.cursor()
-        cursor.execute(f"""select seller_id,country_code,asin,modifier from asj_ads.seller_listings where modifier is not null and (seller_id,country_code,asin) not in (select seller_id,country_code,asin from asj_ads.Goods where update_time>='{datetime.today().date()}') group by seller_id,modifier,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,country_code,asin from asj_ads.Goods where update_time>='{datetime.today().date()}') group by seller_id,title,country_code,asin""")
         query_ = cursor.fetchall()
+        # print(query_)
         col_name = [i[0] for i in cursor.description]
         df_datatable = pd.DataFrame(query_, columns=col_name)
         count=0
@@ -813,7 +802,7 @@ class SpApiRequest:
         while count<len(df_datatable):
             df = df_datatable.iloc[count:count+distance,:]
             count = count+distance
-            df['detail_info'] = df.apply(lambda x: cls.get_listing_info01(x['modifier'],x['country_code'],x['asin'],x['seller_id']),axis=1)
+            df['detail_info'] = df.apply(lambda x: cls.get_listing_info01(x['title'],x['country_code'],x['asin'],x['seller_id']),axis=1)
             detail_info_k = df['detail_info'].map(lambda x: list(x.keys())).to_numpy().tolist()
             detail_info_v = df['detail_info'].map(lambda x: list(x.values())).to_numpy().tolist()
 
@@ -846,6 +835,7 @@ class SpApiRequest:
 
     @staticmethod
     def get_listing_info01(refresh_token, countryCode, asin, seller_id):
+        # print(refresh_token)
         aws_credentials = {
             'refresh_token': refresh_token,
             'lwa_app_id': 'amzn1.application-oa2-client.1f9d3d4747e14b22b4b598e54e6b922e',  # 卖家中心里面开发者资料LWA凭证
@@ -889,6 +879,7 @@ class SpApiRequest:
         try:
             variations = cate_item.get_catalog_item(asin=asin, **{"includedData": ['variations']})
             var_info = variations.payload
+            # print(variations)
             IsParent = 'Y'
             parent_asin = ''
             if len(var_info['variations']) > 0:
@@ -1108,7 +1099,7 @@ class SpApiRequest:
 if __name__ == '__main__':
     for days in range(35,45):
         SpApiRequest.get_allShops("GET_SALES_AND_TRAFFIC_REPORT",days=-days,**{"level":"SKU"})
-    SpApiRequest.listing_infoTable()
+    # SpApiRequest.listing_infoTable()
     # rel = SpApiRequest.get_catelog(account_name='ANLAPUS_US',country=Marketplaces.US,asin='B0BVXB4KT9')
     # print(rel)
 

+ 12 - 0
sync_amz_data/settings/__init__.py

@@ -23,6 +23,18 @@ else:
 #     "db": 0
 # }
 
+MYSQL_AUTH_CONF = dict(user="admin",
+                       password="NSYbBSPbkGQUbOSNOeyy",
+                       host="retail-data.cnrgrbcygoap.us-east-1.rds.amazonaws.com",
+                       database="ansjer_dvadmin",
+                       port=3306)
+
+MYSQL_DATA_CONF = dict(user=MYSQL_DATA_USER,
+                       password=MASQL_DATA_PASSWORD,
+                       host=MASQL_DATA_HOST,
+                       database="asj_ads",
+                       port=3306)
+
 AWS_LWA_CLIENT = {
     'lwa_client_id': 'amzn1.application-oa2-client.ebd701cd07854fb38c37ee49ec4ba109',
     'lwa_client_secret': 'cbf0514186db4df91e04a8905f0a91b605eae4201254ced879d8bb90df4b474d',

+ 4 - 0
sync_amz_data/settings/dev.py

@@ -5,3 +5,7 @@ LOG_LEVEL = 10
 LOG_HANDLERS = ["console"]
 
 DATA_PATH = "/tmp"
+
+MASQL_DATA_HOST = "192.168.1.225"
+MYSQL_DATA_USER = "root"
+MASQL_DATA_PASSWORD = "sandbox"

+ 5 - 1
sync_amz_data/settings/online.py

@@ -5,4 +5,8 @@ LOG_HANDLERS = ["console", "email"]  # 使用supervisor重定向日志文件
 
 DATA_PATH = "/var/run/amazon_ad_api"
 
-LOG_LEVEL = 10
+LOG_LEVEL = 10
+
+MASQL_DATA_HOST = "amzn-retail.cluster-cnrgrbcygoap.us-east-1.rds.amazonaws.com"
+MYSQL_DATA_USER = "admin"
+MASQL_DATA_PASSWORD = "pvmBNS8q3duiUvvp"

+ 1 - 1
sync_get_open_listing_data.py

@@ -16,7 +16,7 @@ def func_run():
         sp_api_client.SpApiRequest.listing_infoTable()
 # func_run()
 
-
+#
 
 if __name__ == '__main__':
     sched = BlockingScheduler()

+ 4 - 2
sync_get_order_data.py

@@ -2,7 +2,7 @@
 import warnings
 warnings.filterwarnings('ignore')
 from apscheduler.schedulers.blocking import BlockingScheduler
-
+from sync_amz_data.settings import MYSQL_AUTH_CONF, MYSQL_DATA_CONF
 from sync_amz_data.public import sp_api_client
 def func_run():
     try:
@@ -11,7 +11,7 @@ def func_run():
     except Exception as e:
         print(e)
     try:
-        for days in (-2,-3,-4): #range(-34,-2):#
+        for days in (-2,-3,-4): #range(-29,-2):#
            sp_api_client.SpApiRequest.get_allShops("GET_SALES_AND_TRAFFIC_REPORT",days=days,**{"level":"SKU"})
            sp_api_client.SpApiRequest.get_allShops("GET_SALES_AND_TRAFFIC_REPORT",days=days,**{"level":"PARENT"})
            sp_api_client.SpApiRequest.get_allShops("GET_SALES_AND_TRAFFIC_REPORT",days=days,**{"level":"CHILD"})
@@ -27,3 +27,5 @@ if __name__ == '__main__':
                   second=30)
     sched.start()
 
+
+