summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2015-09-29 20:51:55 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2015-09-29 20:51:55 +0200
commit9a78509afcab8ec5fc00d5239d01949df965b8b7 (patch)
tree25fe79d46fbb85465c6bc7f3c76dd788474747f6 /modules
parentf30f76acd13b45143adde1a78f185f7d81058628 (diff)
tracking: Added DHL support via EasyPost.
Diffstat (limited to 'modules')
-rwxr-xr-xmodules/tracking.py38
1 files changed, 38 insertions, 0 deletions
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 = []