Python UPS XML跟踪

时间:2017-10-06 22:32:24

标签: python ups-api

所以我基本上试图让python输出货件的状态。不幸的是,ClassicUPS软件包只会显示交付日期和#34; True"或"错误"如果在途中。

这是默认的" ups.py":

class TrackingInfo(object):

def __init__(self, ups_conn, tracking_number):
    self.tracking_number = tracking_number

    tracking_request = {
        'TrackRequest': {
            'Request': {
                'TransactionReference': {
                    'CustomerContext': 'Get tracking status',
                    'XpciVersion': '1.0',
                },
                'RequestAction': 'Track',
                'RequestOption': 'activity',
            },
            'TrackingNumber': tracking_number,
        },
    }

    self.result = ups_conn._transmit_request('track', tracking_request)

@property
def shipment_activities(self):
    # Possible Status.StatusType.Code values:
    #   I: In Transit
    #   D: Delivered
    #   X: Exception
    #   P: Pickup
    #   M: Manifest

    shipment_activities = (self.result.dict_response['TrackResponse']
                                  ['Shipment']['Package']['Activity'])
    if type(shipment_activities) != list:
        shipment_activities = [shipment_activities]

    return shipment_activities
@property
def ship_status(self):
    # Possible Status.StatusType.Code values:
    #   I: In Transit
    #   D: Delivered
    #   X: Exception
    #   P: Pickup
    #   M: Manifest

    ship_status = (self.result.dict_response['TrackResponse']['Shipment']['Package']['Activity']['Status']['StatusType']['Description'])
    if type(ship_status) == list:
        ship_status = [ship_status]

    return ship_status
@property
def delivered(self):
    delivered = [x for x in self.shipment_activities
                 if x['Status']['StatusType']['Code'] == 'D']
    if delivered:
        return datetime.strptime(delivered[0]['Date'], '%Y%m%d')

@property
def in_transit(self):
    in_transit = [x for x in self.shipment_activities
                 if x['Status']['StatusType']['Description'] != '']

    return len(in_transit) > 0   ### If I remove "len" and "> 0" then it shows a string of results which I will show below

我的自我创造" ups_test.py"文件:

from ClassicUPS import UPSConnection
from datetime import datetime
import xmltodict
import xml.etree.ElementTree as ET

# Credentials obtained from the UPS website
ups = UPSConnection('api-key',
                'username',
                'password',
                'shipper#',  # Optional if you are not creating a shipment
                debug=False)      # Use the UPS sandbox API rather than prod

tracking = ups.tracking_info('1ZXXXXXXXXXXXXXXXX')



print tracking.delivered
print tracking.ship_status
# try:
# if tracking.in_transit == (False) :
    # print "Still in Transit"
# elif tracking.in_transit == (True) :
    # print ("Delivered")
# except:
    # print "Error"

try: 
    if tracking.delivered == (None):
        print ("Still in Transit")
    elif tracking.delivered != (None):
        Print ("Delivered")
except:
    print "error"

来自" ups_test.py"的输出:

[{u'Status': {u'StatusType': {u'Code': u'I', u'Description': u'THE SHIPMENT HAS
BEEN DROPPED OFF AND IS NOW AT THE UPS STORE(R)'}, u'StatusCode': {u'Code': u'XD
'}}, u'Date': u'20171006', u'ActivityLocation': {u'Address': {u'City': u'INDIANA
POLIS', u'StateProvinceCode': u'IN', u'CountryCode': u'US'}}, u'Time': u'180000'
}, {u'Status': {u'StatusType': {u'Code': u'M', u'Description': u'BILLING INFORMA
TION RECEIVED'}, u'StatusCode': {u'Code': u'MP'}}, u'Date': u'20171005', u'Activ
ityLocation': {u'Address': {u'CountryCode': u'US'}}, u'Time': u'153442'}]

我的问题是,如何让python输出特定数据,如:

Description = u['Description' : u'THE SHIPMENT HAS
BEEN DROPPED OFF AND IS NOW AT THE UPS STORE(R)']

1 个答案:

答案 0 :(得分:2)

解决了这个问题。

        tracking = ups.tracking_info(CusTracking)
        status = tracking.shipment_activities

        if status[0]['Status']['StatusType']['Code'] == 'M':
            print "Ready for pickup"
            cursor.execute("UPDATE rma_order SET from_cus_tracking_status=1 WHERE order_id=%s" % Id)