diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2015-02-25 09:48:38 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2015-02-25 09:48:38 +0100 |
commit | 35fe94cadd044a248a081bb88d25012caaa87763 (patch) | |
tree | a2bc2e69cec864e34008caa773ddc0fbb8a78a0b /modules | |
parent | 772cd8499167ca3740d3e705256c35d4da4da362 (diff) |
tracking: Added PostNord support.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/tracking.py | 33 |
1 files changed, 33 insertions, 0 deletions
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(':') |