From 9a78509afcab8ec5fc00d5239d01949df965b8b7 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 29 Sep 2015 20:51:55 +0200 Subject: tracking: Added DHL support via EasyPost. --- modules/tracking.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'modules') diff --git a/modules/tracking.py b/modules/tracking.py index 0905c7c..9696afe 100755 --- a/modules/tracking.py +++ b/modules/tracking.py @@ -16,6 +16,7 @@ from sqlalchemy.orm import sessionmaker, relation, backref from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.exc import IntegrityError from sqlalchemy.sql import or_, and_ +import easypost engine = Session = None Base = declarative_base() @@ -315,6 +316,42 @@ class UpsModule(TrackingModule): results.append(TrackingResult(code, isodate, desc, statuscode == 'D')) return results +class EasypostModule: + def track(self, code): + code = self.splitcode(code) + + try: + tracker = (t for t in easypost.Tracker.all() if t.tracking_code == code).next() + except StopIteration: + tracker = easypost.Tracker.create(tracking_code = code, carrier = self.easypost_carrier) + + results = [] + + if tracker.tracking_details: + delivered = tracker.status == 'delivered' + lastevent = tracker.tracking_details[-1] + date = datetime.datetime.strptime(lastevent.datetime, '%Y-%m-%dT%H:%M:%SZ') + status = lastevent.message + location = [] + for f in ('city', 'state', 'country'): + if lastevent['tracking_location'][f]: + location.append(lastevent['tracking_location'][f]) + if location: + status += ' (%s)' % ', '.join(location) + results.append(TrackingResult(code, date, status.encode('utf-8'), delivered)) + + return results + +class DHLModule(TrackingModule, EasypostModule): + name = 'dhl' + easypost_carrier = 'DHLExpress' + + def get_url(self, code = None): + url = 'http://www.dhl.no/no/express/sporing.html?AWB=' + if code: + url += code + return url + def code_split(code): if ':' in code: return code.split(':') @@ -331,6 +368,7 @@ def get_tracking_module(arg): class Module: def __init__(self, bot): + easypost.api_key = config.get(cfg_section, 'easypost_api_key') self.setup_db() self.irc = bot self.tracking = [] -- cgit v1.2.3