Skip to content
pages.py 2.2 KiB
Newer Older
Matthieu Weber's avatar
Matthieu Weber committed
# -*- coding: utf-8 -*-

# Copyright(C) 2015      Matthieu Weber
#
# This file is part of a weboob module.
Matthieu Weber's avatar
Matthieu Weber committed
#
# This weboob module is free software: you can redistribute it and/or modify
Matthieu Weber's avatar
Matthieu Weber committed
# 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.
#
# This weboob module is distributed in the hope that it will be useful,
Matthieu Weber's avatar
Matthieu Weber committed
# 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 this weboob module. If not, see <http://www.gnu.org/licenses/>.
Matthieu Weber's avatar
Matthieu Weber committed

from dateutil.parser import parse as parse_date

Matthieu Weber's avatar
Matthieu Weber committed
from weboob.browser.pages import JsonPage
Matthieu Weber's avatar
Matthieu Weber committed
from weboob.capabilities.parcel import Parcel, Event, ParcelNotFound


Matthieu Weber's avatar
Matthieu Weber committed
class SearchPage(JsonPage):
    STATUSES = {
            "WAITING": Parcel.STATUS_PLANNED,
            "IN_TRANSPORT": Parcel.STATUS_IN_TRANSIT,
            "READY_FOR_PICKUP": Parcel.STATUS_ARRIVED,
            "DELIVERED": Parcel.STATUS_ARRIVED,
            }
Matthieu Weber's avatar
Matthieu Weber committed
    def get_info(self, _id):
Matthieu Weber's avatar
Matthieu Weber committed
        shipments = self.doc["shipments"]
        if not shipments:
Matthieu Weber's avatar
Matthieu Weber committed
            raise ParcelNotFound("No such ID: %s" % _id)
Matthieu Weber's avatar
Matthieu Weber committed
        shipment = shipments[0]
        result_id = shipment["trackingCode"]
Matthieu Weber's avatar
Matthieu Weber committed
        if result_id != _id:
            raise ParcelNotFound("ID mismatch: expecting %s, got %s" % (_id, result_id))

Matthieu Weber's avatar
Matthieu Weber committed
        p = Parcel(_id)
        if shipment["estimatedDeliveryTime"]:
            p.arrival = parse_date(shipment["estimatedDeliveryTime"], ignoretz=True)
Matthieu Weber's avatar
Matthieu Weber committed
        events = shipment["events"]
        p.history = [self.build_event(i, data) for i, data in enumerate(events)]
        p.status = self.STATUSES.get(shipment["phase"], Parcel.STATUS_UNKNOWN)
Matthieu Weber's avatar
Matthieu Weber committed
        most_recent = p.history[0]
        p.info = most_recent.activity
        return p

Matthieu Weber's avatar
Matthieu Weber committed
    def build_event(self, index, data):
Matthieu Weber's avatar
Matthieu Weber committed
        event = Event(index)
Matthieu Weber's avatar
Matthieu Weber committed
        event.activity = data["description"]["en"]
        event.date = parse_date(data["timestamp"], ignoretz=True)
        event.location = data["locationName"]
Matthieu Weber's avatar
Matthieu Weber committed
        return event