import xml.etree.ElementTree as ET from feed_class import Feed_Class from mysql_config import Mysql_Config import traceback import logging import time import datetime, pytz def Update_Tracking(refresh_token,order_id_SA): try: date=datetime.datetime.now(pytz.timezone('US/Central')).isoformat() date=date[:date.rfind("-")] feed=Feed_Class(refresh_token) mydb=Mysql_Config() mycursor1 = mydb.cursor() #Geting amazon_order_id and orderitemid from order_buyer_info table sql1 = "SELECT amazon_order_id,OrderItemId FROM sa.orders_buyer_info where order_id_SA="+str(order_id_SA)+";" mycursor1.execute(sql1) amazon_order_id,order_item_id= list(mycursor1.fetchall())[0] #geting tracking no. and shipping agency name from the base_wao_order_logs table sql2="SELECT walmart_tracking_number,walmart_tracking_company FROM sa.base_wao_order_logs where order_id="+str(order_id_SA)+";" mycursor1.execute(sql2) tracking_no,carrier_code=list(mycursor1.fetchall())[0] #feed documnet that to be send to the amazon for updataing the tracking no. feed_data=f'''
1.01 A2Z98DY4QMA4OQ
OrderFulfillment 1 {amazon_order_id} {date} Other {carrier_code} Standard {tracking_no} {order_item_id} 1
''' feed_file= open("tracking1.txt","w") feed_file.write(feed_data) feed_file.close() feed_file=open("tracking1.txt") #we feed the documnet to the amazon from there api submit=feed.submit_feed(feed_type="POST_ORDER_FULFILLMENT_DATA",file=feed_file, content_type='text/tsv') feed_file.close() while True: order_doucumentid=feed.get_feed(feed_id =(submit[1].payload['feedId'])).payload #print(order_doucumentid) if order_doucumentid.get("processingStatus",None)=="DONE": #if the feed document is successfuly processed then the loop will be break #print(order_doucumentid['resultFeedDocumentId']) resultFeedid=order_doucumentid['resultFeedDocumentId'] break if order_doucumentid.get("processingStatus",None)=="FETAL": #if the feed document is in FETAL status logger=logging.getLogger() logging.basicConfig(filename="/root/SA-testing/sa-data-scraping/amazon_sp_feed_api/feed_document_id.log", format='%(asctime)s %(message)s', filemode='a+') logger.error(f"{order_id_SA} {submit[1].payload['feedId']} FETAL") return {"Status":"Unsuccessful due to fetal error"} else: time.sleep(5) result=feed.get_feed_result_document(feed_id=resultFeedid) print(result) myroot= ET.fromstring(result[:-16]) feed_successful=myroot[2][1][2][1].text if str(feed_successful)=='1': #if the amazon Accepts the Feed document and apply the changes according to our feed document mycursor = mydb.cursor() #changes the status of order in orders table corresponding to the order_id_SA sql = "UPDATE sa.orders SET order_status = 'Shipped' WHERE order_id_SA ='"+order_id_SA+"'; " mycursor.execute(sql) mydb.commit() return { "Status":"successfully uploaded tracking number", "type":"tracking update", "feed_id":submit[1].payload['feedId'] } else: logger=logging.getLogger() logging.basicConfig(filename="D:/python/company_project/amazon_sp_feed_api/feed_document_id.log", format='%(asctime)s %(message)s', filemode='a+') logger.error(f"{order_id_SA} {submit[1].payload['feedId']} {resultFeedid}") except: logger=logging.getLogger() logging.basicConfig(filename="D:/python/company_project/amazon_sp_feed_api/feed_error.log", format='%(asctime)s %(message)s', filemode='a+') just_the_string = traceback.format_exc() logger.error(str(just_the_string)) return{ "Status":"error", "type":"tracking update", }