|
@@ -24,6 +24,7 @@ class SpApiRequest:
|
|
|
self.marketplace = marketplace
|
|
|
self.shopInfo = shop_infos(profile_id)
|
|
|
self.timezone = self.shopInfo['time_zone']
|
|
|
+ self.profileid = profile_id
|
|
|
|
|
|
def mysql_connect(self):
|
|
|
conn = pymysql.connect(user="huangyifan",
|
|
@@ -132,35 +133,42 @@ class SpApiRequest:
|
|
|
decom_df['expedited-shipping'] = decom_df['expedited-shipping'].astype('string',errors='ignore')
|
|
|
decom_df['updateTime'] = datetime.now(tz=pytz.timezone(self.timezone))
|
|
|
decom_df['timezone'] = self.timezone
|
|
|
+ decom_df['profileid'] = str(self.profileid)
|
|
|
decom_df['item-description'] = decom_df['item-description'].str.slice(0,500)
|
|
|
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')
|
|
|
decom_df.fillna('', inplace=True)
|
|
|
+ # print(decom_df.info())
|
|
|
return decom_df
|
|
|
|
|
|
def GET_MERCHANT_LISTINGS_ALL_DATA(self,limit=None):
|
|
|
+ start = time.time()
|
|
|
para = {"reportType":ReportType.GET_MERCHANT_LISTINGS_ALL_DATA}
|
|
|
reportid = self.create_report(**para)
|
|
|
decom_df = self.decompression(reportid)
|
|
|
print("连接数据库")
|
|
|
conn = self.mysql_connect()
|
|
|
+ print("连接成功")
|
|
|
cursor = conn.cursor()
|
|
|
- bondary_date = (datetime.today() + timedelta(days=-28)).strftime("%Y-%m-%d")
|
|
|
+ timezone = pytz.timezone(self.timezone)
|
|
|
+ bondary_date = (datetime.now(tz=timezone)).strftime("%Y-%m-%d") #+ timedelta(days=-28)
|
|
|
cursor.execute(f"""select * from amz_sp_api.productInfo where (mainImageUrl is not null and mainImageUrl not in ('', ' ')) and
|
|
|
(`seller-sku` not in ('',' ') and `seller-sku` is not null) and
|
|
|
`updateTime`>='{bondary_date}'""") #`seller-sku`,`updateTime`,`mainImageUrl`
|
|
|
col = [i[0] for i in cursor.description]
|
|
|
query_rel = cursor.fetchall()
|
|
|
- print(query_rel[0])
|
|
|
+
|
|
|
if len(query_rel)!=0:
|
|
|
+ print(query_rel[0])
|
|
|
df = pd.DataFrame(query_rel,columns=col)
|
|
|
listingid = df['listing-id'].to_numpy().tolist()
|
|
|
decom_df = decom_df.query("`listing-id` not in @listingid")
|
|
|
+ print("数据条数: ",len(decom_df))
|
|
|
# print(f"delete * from amz_sp_api.productInfo where `listing-id` not in {tuple(listingid)}")
|
|
|
- cursor.execute(f"delete from amz_sp_api.productInfo where `listing-id` not in {tuple(listingid)}")
|
|
|
- conn.commit()
|
|
|
+
|
|
|
+ # conn.commit()
|
|
|
|
|
|
if len(decom_df)==0:
|
|
|
return "Done"
|
|
@@ -179,19 +187,41 @@ class SpApiRequest:
|
|
|
# print(list(conn.query("select * from amz_sp_api.orderReport")))
|
|
|
sql = f"""
|
|
|
insert into amz_sp_api.productInfo
|
|
|
- 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)
|
|
|
- """
|
|
|
+ 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)
|
|
|
+ """ #ok
|
|
|
# print(sql)
|
|
|
+ conn = self.mysql_connect()
|
|
|
+ cursor = conn.cursor()
|
|
|
try:
|
|
|
conn.begin()
|
|
|
cursor.executemany(sql, list_df)
|
|
|
conn.commit()
|
|
|
print("插入中...")
|
|
|
+ insert_listingid = df_insert['listing-id'].to_numpy().tolist()
|
|
|
+ cursor.execute(f"delete from amz_sp_api.productInfo where `listing-id` not in {tuple(insert_listingid)} and `updateTime`<'{bondary_date}'")
|
|
|
+ conn.commit()
|
|
|
rowcount += 200
|
|
|
except Exception as e:
|
|
|
conn.rollback()
|
|
|
print(e)
|
|
|
+ try:
|
|
|
+ conn = self.mysql_connect()
|
|
|
+ cursor = conn.cursor()
|
|
|
+ conn.begin()
|
|
|
+ cursor.executemany(sql, list_df)
|
|
|
+ conn.commit()
|
|
|
+ insert_listingid = df_insert['listing-id'].to_numpy().tolist()
|
|
|
+ cursor.execute(f"delete from amz_sp_api.productInfo where `listing-id` not in {tuple(insert_listingid)} and `updateTime`<'{bondary_date}'")
|
|
|
+ conn.commit()
|
|
|
+ except Exception as e:
|
|
|
+ conn.rollback()
|
|
|
+ print(e)
|
|
|
+ break
|
|
|
+ # break
|
|
|
+ conn.close()
|
|
|
print("全部完成")
|
|
|
+ end =time.time()
|
|
|
+ print("duration:",end-start)
|
|
|
return decom_df
|
|
|
|
|
|
def get_mainImage_url(self, sku):
|
|
@@ -201,7 +231,7 @@ class SpApiRequest:
|
|
|
img = r1.payload.get("summaries")[0].get("mainImage")
|
|
|
img_url = None if img is None else img.get("link")
|
|
|
except Exception as e:
|
|
|
- print(e)
|
|
|
+ print("获取图片url过程错误重试, 错误message: ",e)
|
|
|
time.sleep(3)
|
|
|
r1 = listingClient.get_listings_item(sellerId=self.shopInfo['advertiser_id'], sku=sku)
|
|
|
img = r1.payload.get("summaries")[0].get("mainImage")
|
|
@@ -225,6 +255,7 @@ class SpApiRequest:
|
|
|
decom_df.fillna('',inplace=True)
|
|
|
decom_df["ReportDate"] = parse(shopReportday)
|
|
|
decom_df['timezone'] = self.timezone
|
|
|
+ decom_df['profileid'] = str(self.profileid)
|
|
|
list_df = decom_df.to_numpy().tolist()
|
|
|
print(list_df[0])
|
|
|
# tuple_data = [tuple(i) for i in list_df]
|
|
@@ -233,8 +264,8 @@ class SpApiRequest:
|
|
|
# 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)
|
|
|
- """
|
|
|
+ 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)
|
|
|
+ """ #ok
|
|
|
# print(sql)
|
|
|
try:
|
|
|
conn.begin()
|