-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPullTrackingInfo.py
132 lines (113 loc) · 4.37 KB
/
PullTrackingInfo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import requests
import json
from Secrets import *
from Analytics import analytics
def trackingAPI(t_Num):
payload = {
"UPSSecurity": {
"UsernameToken": {
"Username":("" + USERNAME),
"Password":("" + PASSWORD)
},
"ServiceAccessToken": {
"AccessLicenseNumber":("" + AUTHENTICATION_TOKEN)
}
},
"TrackRequest": {
"Request": {
"RequestOption": "1",
"TransactionReference": {
"CustomerContext": "Your Test Case Summary Description"
}
},
"InquiryNumber": ("" + t_Num) }
}
r = requests.post(TRACKING_API, data=json.dumps(payload), headers=HEADERS)
#print r.content
analyzeTrackingAPI(r.content)
def analyzeTrackingAPI(not_analyzed):
toAnalyze = json.loads(not_analyzed)
shipmentInfo = toAnalyze["TrackResponse"]["Shipment"]
shippedFrom = []
shippedTo = []
countFrom = 0
for key, value in shipmentInfo["ShipmentAddress"][0]["Address"].items():
if(countFrom != 1 and countFrom != 2):
shippedFrom.append(value)
countFrom += 1
countTo = 0
for key, value in shipmentInfo["ShipmentAddress"][1]["Address"].items():
if(countTo != 1):
shippedTo.append(value)
countTo += 1
packageDeliveryDate = shipmentInfo["Package"]["Activity"][0]["Date"]
packageDeliveryTime = shipmentInfo["Package"]["Activity"][0]["Time"]
weight = shipmentInfo["ShipmentWeight"]
measurement = weight["Weight"]
units = weight["UnitOfMeasurement"]["Code"]
serviceCode = shipmentInfo["Service"]["Description"]
if shipmentInfo.has_key("PickupDate"):
pickUp = shipmentInfo["PickupDate"]
else:
for i in range(len(shipmentInfo["Package"]["Activity"])):
if(i == (len(shipmentInfo["Package"]["Activity"])) - 2):
pickUp = shipmentInfo["Package"]["Activity"][i]["Date"]
timeInTransitAPI(shippedFrom, shippedTo, measurement, units, serviceCode, pickUp, packageDeliveryDate, packageDeliveryTime)
def timeInTransitAPI(sF, sTo, measurement, units, serviceCode, pickUp, packageDate, packageTime):
payload = {
"Security": {
"UsernameToken": {
"Username": ("" + USERNAME),
"Password": ("" + PASSWORD)
}, "UPSServiceAccessToken": {
"AccessLicenseNumber": ("" + AUTHENTICATION_TOKEN)
}
},
"TimeInTransitRequest": {
"Request": {
"RequestOption": "TNT",
"TransactionReference": {
"CustomerContext": "Developer",
"TransactionIdentifier": "UPS On-Time Analytics"
}
},
"ShipFrom": {
"Address": {
"StateProvinceCode": ("" + sF[2]),
"CountryCode": sF[1],
"PostalCode": sF[0]
}
},
"ShipTo": {
"Address": {
"StateProvinceCode": sTo[1],
"CountryCode": sTo[2],
"PostalCode": sTo[0]
}
},
"Pickup": {
"Date": pickUp
},
"ShipmentWeight": {
"UnitOfMeasurement": {
"Code": units,
"Description": "Pounds"
},
"Weight": measurement
},
"MaximumListSize": "20"
}
}
r = requests.post(TIME_IN_TRANSIT_API, data=json.dumps(payload), headers=HEADERS)
notAnalyzed = r.content
toAnalyze = json.loads(notAnalyzed)
service = toAnalyze["TimeInTransitResponse"]["TransitResponse"]["ServiceSummary"]
serviceType = ""
serviceArrivalTime = ""
serviceArrivalDate = ""
for i in range(len(service)):
if(serviceCode.lower() == service[i]["Service"]["Description"].lower()):
serviceType = service[i]["Service"]["Description"]
serviceArrivalTime = service[i]["EstimatedArrival"]["Arrival"]["Time"]
serviceArrivalDate = service[i]["EstimatedArrival"]["Arrival"]["Date"]
analytics(packageDate, packageTime, serviceArrivalDate, serviceArrivalTime, serviceType, sTo[0])