Added module dpd

This module enables parceloob to track parcels sent with DPD.

Signed-off-by: Matthieu Weber <mweber+weboob@free.fr>
Signed-off-by: Romain Bignon <romain@symlink.me>
This commit is contained in:
Matthieu Weber 2015-05-13 21:16:08 +03:00 committed by Romain Bignon
commit 1ca881e270
6 changed files with 199 additions and 0 deletions

24
modules/dpd/__init__.py Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
from .module import DPDModule
__all__ = ['DPDModule']

32
modules/dpd/browser.py Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
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<id>.+)&locale=en_D2&type=1', SearchPage)
def get_tracking_info(self, _id):
return self.search_page.go(id=_id).get_info(_id)

BIN
modules/dpd/favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 508 B

49
modules/dpd/module.py Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
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)

65
modules/dpd/pages.py Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
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

29
modules/dpd/test.py Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
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")