Calling Express Bird Single Number Query Interface Api to Check Express

Keywords: Java JSON encoding

The main functions are as follows: According to the order number entered by the user, the express bird can be automatically identified according to the API interface of the express bird query, so as to realize the function of automatic query.

Docking process

1. Apply for an express bird account to obtain authorization http://www.kdniao.com/reg

Express Bird's account is permanently free, and can be used immediately after applying for real name certification, without need of audit.

2. Choose the services that need to be opened in the background, and encapsulate the data according to the singular identification interface and the instant query interface.

3. Get the returned json data output

import json
import urllib
import urllib.request
import hashlib
import base64
import urllib.parse
 
# Here is the account number and password of the express bird official website
APP_id = "1266271"
APP_key = "7526a46e-3a2a-4f5b-8659-d72f361e3386"
 
 
def encrypt(origin_data, appkey):
    """Data Content Signature:(Request content(Uncoded)+AppKey)Conduct MD5 Encryption, then Base64 Code"""
    m = hashlib.md5()
    m.update((origin_data+appkey).encode("utf8"))
    encodestr = m.hexdigest()
    base64_text = base64.b64encode(encodestr.encode(encoding='utf-8'))
    return base64_text
 
 
def sendpost(url, datas):
    """Send out post request"""
    postdata = urllib.parse.urlencode(datas).encode('utf-8')
    header = {
        "Accept": "application/x-www-form-urlencoded;charset=utf-8",
        "Accept-Encoding": "utf-8"
    }
    req = urllib.request.Request(url, postdata, header)
    get_data = (urllib.request.urlopen(req).read().decode('utf-8'))
    return get_data
 
 
def get_company(logistic_code, appid, appkey, url):
    """Get the code and name of the courier company corresponding to the courier number"""
    data1 = {'LogisticCode': logistic_code}
    d1 = json.dumps(data1, sort_keys=True)
    requestdata = encrypt(d1, appkey)
    post_data = {
        'RequestData': d1,
        'EBusinessID': appid,
        'RequestType': '2002',
        'DataType': '2',
        'DataSign': requestdata.decode()}
    json_data = sendpost(url, post_data)
    sort_data = json.loads(json_data)
    return sort_data
 
 
def get_traces(logistic_code, shipper_code, appid, appkey, url):
    """Query interface supports query by shipping number(Single query)"""
    data1 = {'LogisticCode': logistic_code, 'ShipperCode': shipper_code}
    d1 = json.dumps(data1, sort_keys=True)
    requestdata = encrypt(d1, appkey)
    post_data = {'RequestData': d1, 'EBusinessID': appid, 'RequestType': '1002', 'DataType': '2',
                 'DataSign': requestdata.decode()}
    json_data = sendpost(url, post_data)
    sort_data = json.loads(json_data)
    return sort_data
 
 
def recognise(expresscode):
    """output data"""
    url = 'http://testapi.kdniao.cc:8081/Ebusiness/EbusinessOrderHandle.aspx'
    data = get_company(expresscode, APP_id, APP_key, url)
    if not any(data['Shippers']):
        print("The express message was not found.,Please check if the express number is wrong!")
    else:
        print("It has been found that", str(data['Shippers'][0]['ShipperName'])+"("+
              str(data['Shippers'][0]['ShipperCode'])+")", expresscode)
        trace_data = get_traces(expresscode, data['Shippers'][0]['ShipperCode'], APP_id, APP_key, url)
        if trace_data['Success'] == "false" or not any(trace_data['Traces']):
            print("Not inquired about the express logistics track!")
        else:
            str_state = "Problem pieces"
            if trace_data['State'] == '2':
                str_state = "On the way"
            if trace_data['State'] == '3':
                str_state = "Already signed"
            print("Current status: "+str_state)
            trace_data = trace_data['Traces']
            item_no = 1
            for item in trace_data:
                print(str(item_no)+":", item['AcceptTime'], item['AcceptStation'])
                item_no += 1
            print("\n")
    return
 
while True:
    code = input("Please enter the express number(Esc Sign out): ")
    code = code.strip()
    if code == "esc":
        break
    recognise(code)

Posted by SoccerGloves on Tue, 08 Oct 2019 06:09:43 -0700