~martijnbraam/shipments

9f22ccad698e1d00d8451d1fbebb573c2b7af6cb — Martijn Braam 4 months ago 438eee8
carrier: kruidvat: new carrier

Not really a direct carrier, but the analog photo developmet service
acts like one.
1 files changed, 80 insertions(+), 0 deletions(-)

M shipments/carrier.py
M shipments/carrier.py => shipments/carrier.py +80 -0
@@ 1,3 1,4 @@
import re
from datetime import datetime, timezone, timedelta

import requests


@@ 475,6 476,85 @@ class DHL(Carrier):
        return result


class KruidvatPhoto(Carrier):
    DISPLAYNAME = 'Kruidvat Fotoservice'

    @classmethod
    def identify(cls, code):
        # The order id is a 6 digit number, it needs to be combined with a 6 digit customer number
        # This also supports the customer-order format

        if re.match(r'^\d{6}-\d{6}$', code):
            return True

        if re.match('^\d{6}$', code):
            return True

        return False

    def _addr(self, payload):
        res = Address()
        if 'countryCode' in payload['address']:
            res.country = payload['address']['countryCode']
        return res

    @classmethod
    def get_requirements(cls, code):
        return [
            ('customerid', 'Customer #', 'text'),
        ]

    def get_info(self, code, extra):
        order_id = code
        if '-' in order_id:
            customer_id, order_id = code.split('-', maxsplit=1)
        elif 'customerid' in extra and extra['customerid']:
            customer_id = extra['customerid']
        else:
            raise ValueError("Missing customer id")

        url = f'https://spot.photoprintit.com/spotapi/orderInfo/order?config=5&fullOrderId={customer_id}-{order_id}'
        response = requests.get(url)
        payload = response.json()

        result = PackageInfo(code, self.DISPLAYNAME)
        result.carrier_name = self.DISPLAYNAME
        result.status = payload['summaryStateText']
        if payload['summaryStateCode'] == 'PROCESSING':
            result.status_category = StatusCategory.LABEL_CREATED
        elif payload['summaryStateCode'] == 'DELIVERED':
            result.status_category = StatusCategory.DELIVERED
        elif payload['summaryStateCode'] == 'ERROR':
            result.status_category = StatusCategory.ERROR
        else:
            result.status_category = StatusCategory.IN_TRANSIT

        if payload['summaryPriceText']:
            result.delivery_note = payload['summaryPriceText']

        if len(payload.get('subOrders', [])) == 0:
            return result

        if payload.get('deliveryText', '') != '':
            raw = payload['deliveryText'].split('\n')
            addr = Address()
            addr.name = raw[0]
            if len(raw) > 1:
                addr.address1 = raw[1]
            if len(raw) > 2:
                addr.address2 = raw[2]
            if len(raw) > 3:
                addr.address3 = raw[3]
            result.destination = addr

        if payload.get('orderDate', '') != '':
            stamp = datetime.fromisoformat(payload['orderDate'])
            e = PackageEvent(stamp, None, 'Received order')
            result.events.append(e)

        return result


class Track4PX(Carrier):
    DISPLAYNAME = '4PX'