From 6e40ac66d0597bbfe2b360a319693c84d4c46273 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 9 Jul 2013 22:01:52 +0200 Subject: tracking: Added tollpost support. --- modules/tracking.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'modules') diff --git a/modules/tracking.py b/modules/tracking.py index 7285014..5ba09b2 100644 --- a/modules/tracking.py +++ b/modules/tracking.py @@ -195,6 +195,37 @@ class FedexModule(TrackingModule): results.append(TrackingResult(code, date, status, lastevent['isDelivered'])) return results +class TollpostModule(TrackingModule): + name = 'tollpost' + + def get_url(self, code = None): + url = 'http://www.tollpost.no/minside/SOPS/' + if code: + url += 'consignment/' + code + return code + + def track(self, code): + code = self.splitcode(code) + url = 'http://www.tollpost.no/XMLServer/rest/trackandtrace' + data = urllib.urlencode({'q': code}) + u = urllib2.urlopen(url, data) + data = u.read() + u.close() + data = json.loads(data) + 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 = lastevent['eventTime'].replace('T', ' ') + event = lastevent['eventDescription'] + location = lastevent['location']['name'] + status = '%s (%s)' % (event, location) + results.append(TrackingResult(code, date, status, delivered)) + return results + def code_split(code): if ':' in code: return code.split(':') -- cgit v1.2.3