From f568d36e0711d3a9d48fd3fe50a07e9e459d3089 Mon Sep 17 00:00:00 2001 From: Bjoernar Leithe Date: Thu, 30 Jul 2015 11:51:31 +0200 Subject: tracking:Added ups support --- modules/tracking.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'modules') 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 = """ + + %s + %s + %s + + + + + Track + + %s + """ % (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(':') -- cgit v1.2.3