From 0111b5c5a7efe63ad89127050887c103b7d13e67 Mon Sep 17 00:00:00 2001 From: Matthieu Weber Date: Wed, 13 May 2015 21:16:07 +0300 Subject: [PATCH] Added module dhl This module enables parceloob to track parcels sent with DHL. Signed-off-by: Matthieu Weber Signed-off-by: Romain Bignon --- modules/dhl/__init__.py | 24 +++++++++++++ modules/dhl/browser.py | 32 +++++++++++++++++ modules/dhl/favicon.png | Bin 0 -> 419 bytes modules/dhl/module.py | 49 ++++++++++++++++++++++++++ modules/dhl/pages.py | 76 ++++++++++++++++++++++++++++++++++++++++ modules/dhl/test.py | 29 +++++++++++++++ 6 files changed, 210 insertions(+) create mode 100644 modules/dhl/__init__.py create mode 100644 modules/dhl/browser.py create mode 100644 modules/dhl/favicon.png create mode 100644 modules/dhl/module.py create mode 100644 modules/dhl/pages.py create mode 100644 modules/dhl/test.py diff --git a/modules/dhl/__init__.py b/modules/dhl/__init__.py new file mode 100644 index 00000000..353e75c9 --- /dev/null +++ b/modules/dhl/__init__.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2015 Matthieu Weber +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + + +from .module import DHLModule + + +__all__ = ['DHLModule'] diff --git a/modules/dhl/browser.py b/modules/dhl/browser.py new file mode 100644 index 00000000..2f34a0ae --- /dev/null +++ b/modules/dhl/browser.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2015 Matthieu Weber +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + + +from weboob.browser import PagesBrowser, URL + +from .pages import SearchPage + + +class DHLBrowser(PagesBrowser): + BASEURL = 'http://nolp.dhl.de' + + search_page = URL('/nextt-online-public/set_identcodes.do\?lang=en&idc=(?P.+)', SearchPage) + + def get_tracking_info(self, _id): + return self.search_page.go(id=_id).get_info(_id) diff --git a/modules/dhl/favicon.png b/modules/dhl/favicon.png new file mode 100644 index 0000000000000000000000000000000000000000..846a75c8649f786b37ad446053d63e7598a9a102 GIT binary patch literal 419 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I1SD0tpLGH$&H|6fVg?3oVGw3ym^DWND9B#o z>FdgVpOsBWkyGOu-+G{sWQl7;iF1B#Zfaf$gL6@8Vo7R>LV0FMhJw4NZ$Nk>pEv^p zqpYWkV@Srmw>LKC9dZzGy=czDy68)z+9H=n|LfI4SBb2g%+zwhTX+J`4LS8=X-ln( zl230wbFpeq_Mxzgh7*Cdpo1^W9k1KxKMmJ>#cu8Wvcx$%?{0qF&t&b1@5PU%M=#oa z=fbSp`VN!QyWak2*#F((&6BNrFC*PlY=%q*B<~=LwHmne{Ia}U8f63BEv75e> kp0_=qu8Qst28ZlF3@o+54}Z1K?FLDDy85}Sb4q9e0Mj&}SpWb4 literal 0 HcmV?d00001 diff --git a/modules/dhl/module.py b/modules/dhl/module.py new file mode 100644 index 00000000..b4c5f86c --- /dev/null +++ b/modules/dhl/module.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2015 Matthieu Weber +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + + +from weboob.tools.backend import Module +from weboob.capabilities.parcel import CapParcel + +from .browser import DHLBrowser + + +__all__ = ['DHLModule'] + + +class DHLModule(Module, CapParcel): + NAME = 'dhl' + DESCRIPTION = u'DHL website' + MAINTAINER = u'Matthieu Weber' + EMAIL = 'mweber+weboob@free.fr' + LICENSE = 'AGPLv3+' + VERSION = '1.1' + + BROWSER = DHLBrowser + + def get_parcel_tracking(self, id): + """ + Get information abouut a parcel. + + :param id: ID of the parcel + :type id: :class:`str` + :rtype: :class:`Parcel` + :raises: :class:`ParcelNotFound` + """ + return self.browser.get_tracking_info(id) diff --git a/modules/dhl/pages.py b/modules/dhl/pages.py new file mode 100644 index 00000000..a2064bf7 --- /dev/null +++ b/modules/dhl/pages.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2015 Matthieu Weber +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + +from dateutil.parser import parse as parse_date + +from weboob.browser.pages import HTMLPage +from weboob.capabilities.parcel import Parcel, Event, ParcelNotFound + +# Based on http://www.parcelok.com/delivery-status-dhl.html +STATUSES = { + "The instruction data for this shipment have been provided by the sender to DHL " + "electronically": Parcel.STATUS_PLANNED, + "The shipment has been posted by the sender at the retail outlet": Parcel.STATUS_PLANNED, + + "The shipment has been processed in the destination parcel center": Parcel.STATUS_IN_TRANSIT, + "The international shipment has been processed in the parcel center of origin": Parcel.STATUS_IN_TRANSIT, + "The international shipment has been processed in the export parcel center": Parcel.STATUS_IN_TRANSIT, + "The shipment will be transported to the destination country and, from there, handed over to " + "the delivery organization.": Parcel.STATUS_IN_TRANSIT, + "The shipment has arrived in the destination country": Parcel.STATUS_IN_TRANSIT, + "The shipment has arrived at the parcel center.": Parcel.STATUS_IN_TRANSIT, + "Shipment is prepared for customs clearance in country of destination": Parcel.STATUS_IN_TRANSIT, + "The shipment is being prepared for delivery in the delivery depot": Parcel.STATUS_IN_TRANSIT, + "Scheduled for delivery": Parcel.STATUS_IN_TRANSIT, + "The shipment has been loaded onto the delivery vehicle": Parcel.STATUS_IN_TRANSIT, + "Shipment has arrived at delivery location": Parcel.STATUS_IN_TRANSIT, + "With delivery courier": Parcel.STATUS_IN_TRANSIT, + "Delivery attempted; consignee premises closed": Parcel.STATUS_IN_TRANSIT, + "The shipment has been damaged and is being returned to the parcel center for" + "repackaging": Parcel.STATUS_IN_TRANSIT, + + "The shipment has been successfully delivered": Parcel.STATUS_ARRIVED, +} + + +class SearchPage(HTMLPage): + def get_info(self, _id): + result_id = self.doc.xpath('//th[@class="mm_sendungsnummer"]') + if not result_id: + raise ParcelNotFound("No such ID: %s" % _id) + result_id = result_id[0].text + if result_id != _id: + raise ParcelNotFound("ID mismatch: expecting %s, got %s" % (_id, result_id)) + + p = Parcel(_id) + events = self.doc.xpath('//div[@class="accordion-inner"]/table/tbody/tr') + p.history = [self.build_event(i, tr) for i, tr in enumerate(events)] + p.status, p.info = self.guess_status(p.history[-1]) + return p + + def guess_status(self, most_recent): + txt = most_recent.activity + return STATUSES.get(txt, Parcel.STATUS_UNKNOWN), txt + + def build_event(self, index, tr): + event = Event(index) + event.date = parse_date(tr.xpath('./td[1]')[0].text.strip(), dayfirst=True, fuzzy=True) + event.location = unicode(tr.xpath('./td[2]')[0].text.strip()) + event.activity = unicode(tr.xpath('./td[3]')[0].text.strip()) + return event diff --git a/modules/dhl/test.py b/modules/dhl/test.py new file mode 100644 index 00000000..ff21a3dd --- /dev/null +++ b/modules/dhl/test.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2015 Matthieu Weber +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + + +from weboob.tools.test import BackendTest +from weboob.capabilities.parcel import ParcelNotFound + + +class DHLTest(BackendTest): + MODULE = 'dhl' + + def test_dhl(self): + self.assertRaises(ParcelNotFound, self.backend.get_parcel_tracking, "foo")