summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjoernar Leithe <bl@selbu.org>2015-07-30 11:51:31 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2015-07-30 15:37:30 +0200
commitf568d36e0711d3a9d48fd3fe50a07e9e459d3089 (patch)
tree1174710b3e5f9ac3bedc1d21275d37b9359c56fc
parent4011141108f5992f8231208ba9f42d52da2e4d0e (diff)
tracking:Added ups support
-rwxr-xr-xmodules/tracking.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/modules/tracking.py b/modules/tracking.py
index 1e74001..0905c7c 100755
--- a/modules/tracking.py
+++ b/modules/tracking.py
@@ -102,6 +102,7 @@ class TrackingModuleMeta(type):
class TrackingModule(object):
__metaclass__ = TrackingModuleMeta
+ config_section = 'module/tracking'
def splitcode(self, code):
return code_split(code)[-1]
@@ -245,6 +246,75 @@ class PostNordModule(TrackingModule):
results.append(TrackingResult(code, date, status.encode('utf-8'), delivered))
return results
+class UpsModule(TrackingModule):
+ name = 'ups'
+
+ def get_url(self, code = None):
+ if code is None:
+ url = 'http://www.ups.com/WebTracking/track'
+ else:
+ data = {
+ 'TypeOfInquiryNumber': 'T',
+ 'InquiryNumber1': code,
+ }
+ url = 'http://www.ups.com/WebTracking/processInputRequest?' + urllib.urlencode(data)
+ return url
+
+ def track(self, code):
+ code = self.splitcode(code)
+
+ access_key = config.get(self.config_section, 'ups_access_key')
+ username = config.get(self.config_section, 'ups_username')
+ password = config.get(self.config_section, 'ups_password')
+
+ url = 'https://wwwcie.ups.com/ups.app/xml/Track'
+
+ request = """<?xml version="1.0" ?>
+ <AccessRequest xml:lang='en-US'>
+ <AccessLicenseNumber>%s</AccessLicenseNumber>
+ <UserId>%s</UserId>
+ <Password>%s</Password>
+ </AccessRequest>
+ <?xml version="1.0" ?>
+ <TrackRequest>
+ <Request>
+ <RequestAction>Track</RequestAction>
+ </Request>
+ <TrackingNumber>%s</TrackingNumber>
+ </TrackRequest>""" % (access_key, username, password, code)
+
+ try:
+ u = urllib2.urlopen(url, request)
+ except urllib2.HTTPError as e:
+ raise PackageError(str(e))
+
+ xml = ET.parse(u)
+
+ packages = xml.findall('Shipment/Package')
+ if packages is None:
+ raise NoPackageFound('No packages found for \002%s\002.' % code)
+
+ results = []
+ for package in packages:
+ code = package.find('TrackingNumber').text
+
+ activity = package.find('Activity')
+ if activity is None:
+ continue
+
+ address = activity.find('ActivityLocation/Address')
+ location_text = "%s, %s" % (address.find('City').text, address.find('CountryCode').text)
+ statuscode = activity.find('Status/StatusType/Code').text
+ event_text = activity.find('Status/StatusType/Description').text
+ date = activity.find('Date').text
+ time = activity.find('Time').text
+ year, month, day = int(date[0:4]), int(date[4:6]), int(date[6:8])
+ hour, minute, second = int(time[0:2]), int(time[2:4]), int(time[4:6])
+ isodate = datetime.datetime(year, month, day, hour, minute, second)
+ desc = "%s (%s)" % (event_text, location_text)
+ results.append(TrackingResult(code, isodate, desc, statuscode == 'D'))
+ return results
+
def code_split(code):
if ':' in code:
return code.split(':')