summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2015-02-25 09:48:38 +0100
committerJon Bergli Heier <snakebite@jvnv.net>2015-02-25 09:48:38 +0100
commit35fe94cadd044a248a081bb88d25012caaa87763 (patch)
treea2bc2e69cec864e34008caa773ddc0fbb8a78a0b
parent772cd8499167ca3740d3e705256c35d4da4da362 (diff)
tracking: Added PostNord support.
-rw-r--r--modules/tracking.py33
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(':')