UPS Tracking Number Scrapper
Forked from https://gist.github.com/Ajithkumarsekar/10b5733173613865a69a43439487c2fe
Made compatible with Python 3 and works with UPS tracking numbers.
UPS Tracking Number Scrapper
Forked from https://gist.github.com/Ajithkumarsekar/10b5733173613865a69a43439487c2fe
Made compatible with Python 3 and works with UPS tracking numbers.
#!/usr/bin/python | |
#python3 | |
import requests | |
import datetime | |
import json | |
def attach_day(dt): | |
# dt format 7/24/2017 | |
month, day, year = (int(x) for x in dt.split('/')) | |
date = datetime.date(year, month, day) | |
mdy = dt.split('/') | |
# format Mon 24/7/2017 | |
return date.strftime('%A')[:3] + ' ' + '/'.join([mdy[1], mdy[0], mdy[2]]) | |
def get_standard_time(tm): | |
# tm format 4:21 pm | |
tm = tm.strip() | |
if tm[5:] == 'am': | |
return tm | |
hour = int(tm.split(':')[0]) | |
if hour != 12: | |
hour = hour + 12 | |
time = str(hour) + ':' + tm.split(':')[1].split(' ')[0] | |
# format time 16:21 | |
return time | |
def get_package_details(track_no): | |
try: | |
s = requests.Session() | |
response = s.get('https://www.ups.com/track?loc=en_US&requester=ST/') | |
xsrf_token_header = s.cookies.get_dict()['X-XSRF-TOKEN-ST'] | |
xsrf_token_cookie = s.cookies.get_dict()['X-CSRF-TOKEN'] | |
header = { | |
'Origin': 'https://www.ups.com', | |
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) ' | |
'Chrome/59.0.3071.115 Safari/537.36', | |
'Content-Type': 'application/json', | |
'Accept': 'application/json, text/plain, */*', | |
'X-Requested-With': 'XMLHttpRequest', | |
'Referer': 'https://www.ups.com/track?loc=en_US&requester=QUIC&tracknum=%s/trackdetails' % ( | |
str(track_no)), | |
'accept-encoding': 'gzip, deflate, br', | |
'accept-language': 'en-US,en;q=0.9', | |
'x-xsrf-token': xsrf_token_header | |
} | |
data_dict = { | |
"Locale":"en_US", | |
"TrackingNumber":[ str(track_no)], | |
"Requester":"quic", | |
"returnToValue":"" | |
} | |
data = json.dumps(data_dict) | |
s.cookies.set('X-CSRF-TOKEN',xsrf_token_cookie) | |
url = "https://www.ups.com/track/api/Track/GetStatus?loc=en_US" | |
print ("Sending request ...") | |
response = s.post(url, data=data, headers=header) | |
if response.status_code == 200: | |
print ("response received successfully\n") | |
else: | |
print ("request failed, error code : ",response.status_code) | |
res_json = response.json() | |
print(res_json["trackDetails"][0]) | |
print(res_json["trackDetails"][0]["requestedTrackingNumber"]) | |
print(res_json["trackDetails"][0]["packageStatus"]) | |
print(res_json["trackDetails"][0]["deliveredDate"]) | |
print(res_json["trackDetails"][0]["deliveredTime"]) | |
if res_json["trackDetails"][0]["errorCode"] != None: | |
print( res_json["trackDetails"][0]["errorCode"] ) | |
# exists the function if package id is wrong | |
return | |
key_status = res_json["trackDetails"][0]["packageStatus"] | |
# changes the scheduled delivery date and time w.r.t status | |
if key_status == "On the Way": | |
pass | |
schld_deli = attach_day(res_json["trackDetails"][0]['scheduledDeliveryDate']) | |
else: | |
schld_deli = attach_day( | |
res_json["trackDetails"][0]["deliveredDate"]) + " " + get_standard_time( | |
res_json["trackDetails"][0]["deliveredTime"]) | |
result = { | |
'tracking_no': track_no, | |
'ship date': attach_day(res_json["trackDetails"][0]["additionalInformation"]["shippedOrBilledDate"]), | |
'status': key_status, | |
'scheduled delivery': schld_deli | |
} | |
return result | |
except Exception as e: | |
print ('Error occurred : \n Error Message: ' + str(e)) | |
track_id = input('Enter the tracking no : ') | |
print (get_package_details(track_id) ) |