diff --git a/modules/dpd/__init__.py b/modules/dpd/__init__.py new file mode 100644 index 00000000..84d284fe --- /dev/null +++ b/modules/dpd/__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 DPDModule + + +__all__ = ['DPDModule'] diff --git a/modules/dpd/browser.py b/modules/dpd/browser.py new file mode 100644 index 00000000..0f8221ed --- /dev/null +++ b/modules/dpd/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 DPDBrowser(PagesBrowser): + BASEURL = 'https://tracking.dpd.de/' + + search_page = URL('/cgi-bin/simpleTracking.cgi\?parcelNr=(?P.+)&locale=en_D2&type=1', SearchPage) + + def get_tracking_info(self, _id): + return self.search_page.go(id=_id).get_info(_id) diff --git a/modules/dpd/favicon.png b/modules/dpd/favicon.png new file mode 100644 index 00000000..ad68b69b Binary files /dev/null and b/modules/dpd/favicon.png differ diff --git a/modules/dpd/module.py b/modules/dpd/module.py new file mode 100644 index 00000000..97b25b71 --- /dev/null +++ b/modules/dpd/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 DPDBrowser + + +__all__ = ['DPDModule'] + + +class DPDModule(Module, CapParcel): + NAME = 'dpd' + DESCRIPTION = u'DPD website' + MAINTAINER = u'Matthieu Weber' + EMAIL = 'mweber+weboob@free.fr' + LICENSE = 'AGPLv3+' + VERSION = '1.1' + + BROWSER = DPDBrowser + + 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/dpd/pages.py b/modules/dpd/pages.py new file mode 100644 index 00000000..b830aadc --- /dev/null +++ b/modules/dpd/pages.py @@ -0,0 +1,65 @@ +# -*- 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 JsonPage +from weboob.capabilities.parcel import Parcel, Event, ParcelNotFound + +STATUSES = { + 1: Parcel.STATUS_PLANNED, + 2: Parcel.STATUS_IN_TRANSIT, + 3: Parcel.STATUS_IN_TRANSIT, + 4: Parcel.STATUS_IN_TRANSIT, + 5: Parcel.STATUS_ARRIVED, +} + + +class SearchPage(JsonPage): + def build_doc(self, text): + from weboob.tools.json import json + return json.loads(text[1:-1]) + + def get_info(self, _id): + result_id = self.doc.get("TrackingStatusJSON", {}).get("shipmentInfo", {}).get("parcelNumber", None) + if not result_id: + raise ParcelNotFound("No such ID: %s" % _id) + if not _id.startswith(result_id): + raise ParcelNotFound("ID mismatch: expecting %s, got %s" % (_id, result_id)) + + p = Parcel(_id) + events = self.doc.get("TrackingStatusJSON", {}).get("statusInfos", []) + p.history = [self.build_event(i, data) for i, data in enumerate(events)] + p.status = self.guess_status( + self.doc.get("TrackingStatusJSON", {}). + get("shipmentInfo", {}). + get("deliveryStatus")) + p.info = p.history[-1].activity + return p + + def guess_status(self, status_code): + return STATUSES.get(status_code, Parcel.STATUS_UNKNOWN) + + def build_event(self, index, data): + event = Event(index) + date = "%s %s" % (data["date"], data["time"]) + event.date = parse_date(date, dayfirst=False) + event.location = unicode(data["city"]) + event.activity = unicode(", ".join([_["label"] for _ in data["contents"]])) + return event diff --git a/modules/dpd/test.py b/modules/dpd/test.py new file mode 100644 index 00000000..ffb24d8f --- /dev/null +++ b/modules/dpd/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 DPDTest(BackendTest): + MODULE = 'dpd' + + def test_dpd(self): + self.assertRaises(ParcelNotFound, self.backend.get_parcel_tracking, "foo")