|
@@ -5,7 +5,7 @@ warnings.filterwarnings('ignore')
|
|
|
import numpy as np
|
|
|
from pymysql import Timestamp
|
|
|
from sp_api.util import throttle_retry, load_all_pages
|
|
|
-from sp_api.api import Orders,ListingsItems,Inventories,Reports
|
|
|
+from sp_api.api import Orders,ListingsItems,Inventories,Reports,CatalogItems
|
|
|
from sp_api.base import Marketplaces,ReportType,ProcessingStatus
|
|
|
import pandas as pd
|
|
|
import gzip
|
|
@@ -13,7 +13,6 @@ from io import BytesIO,StringIO
|
|
|
from datetime import datetime, timedelta,timezone
|
|
|
import pytz
|
|
|
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
|
|
@@ -55,6 +54,33 @@ class SpApiRequest:
|
|
|
port=3306)
|
|
|
return conn
|
|
|
|
|
|
+ @classmethod
|
|
|
+ def get_catelog(cls,account_name,country=Marketplaces.US,asin=None):
|
|
|
+ if country in [Marketplaces.US, Marketplaces.BR, Marketplaces.CA,Marketplaces.MX]:
|
|
|
+ region = 'NA'
|
|
|
+ elif country in [Marketplaces.DE,Marketplaces.AE, Marketplaces.BE, Marketplaces.PL,
|
|
|
+ Marketplaces.EG,Marketplaces.ES, Marketplaces.GB, Marketplaces.IN, Marketplaces.IT,
|
|
|
+ Marketplaces.NL, Marketplaces.SA, Marketplaces.SE, Marketplaces.TR,Marketplaces.UK,Marketplaces.FR,
|
|
|
+ ]:
|
|
|
+ region = 'EU'
|
|
|
+ else:
|
|
|
+ region = str(country)[-2:]
|
|
|
+ df = cls.auth_info()
|
|
|
+ refresh_token = df.query("account_name==@account_name and region==@region")['refresh_token'].values[0]
|
|
|
+ cred = {
|
|
|
+ 'refresh_token': refresh_token,
|
|
|
+ 'lwa_app_id': 'amzn1.application-oa2-client.1f9d3d4747e14b22b4b598e54e6b922e', # 卖家中心里面开发者资料LWA凭证
|
|
|
+ 'lwa_client_secret': 'amzn1.oa2-cs.v1.3af0f5649f5b8e151cd5bd25c10f2bf3113172485cd6ffc52ccc6a5e8512b490',
|
|
|
+ 'aws_access_key': 'AKIARBAGHTGOZC7544GN',
|
|
|
+ 'aws_secret_key': 'OSbkKKjShvDoWGBwRORSUqDryBtKWs8AckzwNMzR',
|
|
|
+ 'role_arn': 'arn:aws:iam::070880041373:role/Amazon_SP_API_ROLE'
|
|
|
+ }
|
|
|
+ images_info = CatalogItems(credentials=cred, marketplace=country).get_catalog_item(asin=asin,**{"includedData":['images']})
|
|
|
+ images = images_info.images[0].get('images')[0]['link']
|
|
|
+ title_info = CatalogItems(credentials=cred, marketplace=country).get_catalog_item(asin=asin)
|
|
|
+ title = title_info.payload['summaries'][0]['itemName']
|
|
|
+ return {'images':images,'title':title}
|
|
|
+
|
|
|
def create_report(self,**kwargs):
|
|
|
reportType = kwargs['reportType']
|
|
|
reportOptions =kwargs.get("reportOptions")
|
|
@@ -96,6 +122,7 @@ class SpApiRequest:
|
|
|
df = pd.read_table(rp_table.payload.get("url"),encoding='Shift-JIS')
|
|
|
return df
|
|
|
elif reportId_info.payload.get("processingStatus") in [ProcessingStatus.CANCELLED,ProcessingStatus.FATAL]:
|
|
|
+ print(reportId_info)
|
|
|
print("取消或失败")
|
|
|
break
|
|
|
time.sleep(15)
|
|
@@ -513,9 +540,17 @@ class SpApiRequest:
|
|
|
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)
|
|
|
+ elif data_type =="GET_FLAT_FILE_RETURNS_DATA_BY_RETURN_DATE":
|
|
|
+ return sp_api.GET_FLAT_FILE_RETURNS_DATA_BY_RETURN_DATE(seller_id)
|
|
|
else:
|
|
|
return ""
|
|
|
|
|
|
+ @classmethod
|
|
|
+ def get_refreshtoken(cls):
|
|
|
+ df = cls.auth_info()
|
|
|
+ refreshtoken_list = (df['refresh_token'].to_numpy().tolist())
|
|
|
+ return refreshtoken_list
|
|
|
+
|
|
|
@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()
|
|
@@ -576,6 +611,17 @@ class SpApiRequest:
|
|
|
shopTime.second)
|
|
|
return shopTime_datetime
|
|
|
|
|
|
+ def GET_FLAT_FILE_RETURNS_DATA_BY_RETURN_DATE(self,seller_id):
|
|
|
+ shopReportday = (datetime.now() + timedelta(days=-2)).strftime("%Y-%m-%d")
|
|
|
+ # print(shopReportday)
|
|
|
+ para = {"reportType": ReportType.GET_SELLER_FEEDBACK_DATA,
|
|
|
+ "dataStartTime": shopReportday, "dataEndTime": shopReportday,
|
|
|
+ }
|
|
|
+ reportid = self.create_report(**para) # {"ShowSalesChannel":"true"}
|
|
|
+ decom_df = self.decompression(reportid)
|
|
|
+ print(decom_df)
|
|
|
+ # print(decom_df.columns)
|
|
|
+
|
|
|
def GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL(self,seller_id):
|
|
|
# timezone_ = pytz.timezone(self.timezone)
|
|
|
shopReportday = (datetime.now() + timedelta(days=-1)).strftime("%Y-%m-%d")
|
|
@@ -608,7 +654,7 @@ class SpApiRequest:
|
|
|
country_code = str(self.marketplace)[-2:]
|
|
|
if country_code=='GB':
|
|
|
country_code="UK"
|
|
|
- decom_df['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",
|
|
@@ -642,14 +688,16 @@ class SpApiRequest:
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
- SpApiRequest.get_allShops("GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL")
|
|
|
-
|
|
|
+ # SpApiRequest.get_allShops("GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL")
|
|
|
|
|
|
+ rel = SpApiRequest.get_catelog(account_name='ANLAPUS_US',country=Marketplaces.US,asin='B0BVXB4KT9')
|
|
|
+ print(rel)
|
|
|
|
|
|
"""
|
|
|
create database amz_sp_api;
|
|
|
"""
|
|
|
"""
|
|
|
+
|
|
|
create table amz_sp_api.productInfo
|
|
|
(
|
|
|
`item-name` VARCHAR(300),
|