From 35fe94cadd044a248a081bb88d25012caaa87763 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Wed, 25 Feb 2015 09:48:38 +0100 Subject: tracking: Added PostNord support. --- modules/tracking.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'modules') diff --git a/modules/tracking.py b/modules/tracking.py index f408359..c5d292e 100644 --- a/modules/tracking.py +++ b/modules/tracking.py @@ -242,6 +242,39 @@ class TollpostModule(TrackingModule): results.append(TrackingResult(code, date, status.encode('utf-8'), delivered)) return results +class PostNordModule(TrackingModule): + name = 'postnord' + + def get_url(self, code = None): + url = 'http://www.postnordlogistics.no/nb/Sider/search.aspx' + if code: + url += '?' + urllib.urlencode({'k': code}) + return url + + def track(self, code): + code = self.splitcode(code) + url = 'http://logistics.postennorden.com/wsp/rest-services/ntt-service-rest/api/shipment.json' + url += '?' + urllib.urlencode((('id', code), ('locale', 'no'), ('consumerId', config.get(cfg_section, 'postnord_consumerid')))) + try: + u = urllib2.urlopen(url) + except urllib2.HTTPError as e: + raise PackageError(str(e)) + data = json.load(u) + u.close() + response = data['TrackingInformationResponse'] + results = [] + for shipment in response['shipments']: + for item in shipment['items']: + code = item['itemId'] + delivered = (item['status'] == 'DELIVERED') + lastevent = item['events'][-1] + date = datetime.datetime.strptime(lastevent['eventTime'], '%Y-%m-%dT%H:%M:%S') + event = lastevent['eventDescription'] + location = lastevent['location']['name'] + status = '%s (%s)' % (event, location) + results.append(TrackingResult(code, date, status.encode('utf-8'), delivered)) + return results + def code_split(code): if ':' in code: return code.split(':') -- cgit v1.2.3