~dos: 2 StatusCategory: Add ERROR category carrier: Add InPost support 4 files changed, 62 insertions(+), 1 deletions(-)
Copy & paste the following snippet into your terminal to import this patchset into git:
curl -s https://lists.sr.ht/~martijnbraam/public-inbox/patches/26854/mbox | git am -3Learn more about email & git
From: Sebastian Krzyszkowiak <dos@dosowisko.net> --- shipments/structs.py | 1 + 1 file changed, 1 insertion(+) diff --git a/shipments/structs.py b/shipments/structs.py index 6e7760e..c59baaf 100644 --- a/shipments/structs.py +++ b/shipments/structs.py @@ -5,6 +5,7 @@ class StatusCategory(enum.Enum): LABEL_CREATED = 0 IN_TRANSIT = 1 DELIVERED = 2 + ERROR = 3 class Address: -- 2.32.0
From: Sebastian Krzyszkowiak <dos@dosowisko.net> --- README.md | 3 +- data/org.postmarketos.Shipments.appdata.xml | 1 + shipments/carrier.py | 58 +++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bdac834..2412cf9 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Supported carriers * UPS * PostNL +* InPost Installing ---------- @@ -18,4 +19,4 @@ Installing $ meson build $ meson compile -C build $ sudo meson install -C build -``` \ No newline at end of file +``` diff --git a/data/org.postmarketos.Shipments.appdata.xml b/data/org.postmarketos.Shipments.appdata.xml index da36ba9..4b9b301 100644 --- a/data/org.postmarketos.Shipments.appdata.xml +++ b/data/org.postmarketos.Shipments.appdata.xml @@ -13,6 +13,7 @@ <ul> <li>UPS</li> <li>PostNL</li> + <li>InPost</li> </ul> </description> <launchable type="desktop-id">org.postmarketos.Shipments.desktop</launchable> diff --git a/shipments/carrier.py b/shipments/carrier.py index 8a4ad54..3a9c37a 100644 --- a/shipments/carrier.py +++ b/shipments/carrier.py @@ -201,3 +201,61 @@ class UPS(Carrier): if 'leftAt' in details and details['leftAt'] is not None and details['leftAt'] != '': result.delivery_note = details['leftAt'] return result + +class InPost(Carrier): + DISPLAYNAME = 'InPost' + + STATUS_MAPPING = { + StatusCategory.LABEL_CREATED: ['created', 'offers_prepared', 'offer_selected', 'confirmed', 'dispatched_by_sender_to_pok'], + StatusCategory.IN_TRANSIT: ['dispatched_by_sender', 'collected_from_sender', 'taken_by_courier', 'adopted_at_source_branch', 'sent_from_source_branch', 'adopted_at_sorting_center', 'sent_from_sorting_center', 'adopted_at_target_branch', 'out_for_delivery', 'out_for_delivery_to_address', 'pickup_reminder_sent_address', 'taken_by_courier_from_pok', 'ready_to_pickup_from_branch', 'delay_in_delivery', 'redirect_to_box', 'canceled_redirect_to_box', 'readdressed', 'stack_in_customer_service_point', 'unstack_from_customer_service_point', 'unstack_from_box_machine', 'stack_parcel_in_box_machine_pickup_time_expired', 'stack_parcel_pickup_time_expired'], + StatusCategory.DELIVERED: ['ready_to_pickup_from_pok', 'ready_to_pickup_from_pok_registered', 'ready_to_pickup', 'pickup_reminder_sent', 'delivered', 'return_pickup_confirmation_to_sender', 'courier_avizo_in_customer_service_point', 'stack_in_box_machine'], + StatusCategory.ERROR: ['oversized', 'pickup_time_expired', 'avizo', 'claimed', 'returned_to_sender', 'canceled', 'other', 'rejected_by_receiver', 'undelivered_wrong_address', 'undelivered_incomplete_address', 'undelivered_unknown_receiver', 'undelivered_cod_cash_receiver', 'undelivered', 'undelivered_no_mailbox', 'undelivered_not_live_address', 'undelivered_lack_of_access_letterbox', 'missing', 'taken_by_courier_from_customer_service_point'] + } + + @classmethod + def identify(cls, code): + if not len(code) == 24: + return False + return True + + def get_info(self, code, extra): + url = 'https://inpost.pl/shipx-proxy/' + params = {'number': code} + headers = {'X-Requested-With': 'XMLHttpRequest'} + response = requests.get(url, params=params, headers=headers) + payload = response.json() + result = PackageInfo(code, 'InPost') + result.carrier_name = self.DISPLAYNAME + result.status = " ".join(payload['status'].split('_')).capitalize() + + for category, statuses in self.STATUS_MAPPING.items(): + if payload['status'] in statuses: + result.status_category = category + + result.delivery_note = None + result.description = None + result.service = payload['service'] + if result.service in ['inpost_locker_standard', 'inpost_locker_allegro', 'inpost_locker_allegro_smart']: + target = payload['custom_attributes']['target_machine_detail'] + a = Address() + a.name = f'{target["name"]} - {target["location_description"]}' + a.address1 = target['address']['line1'] + (a.postalcode, a.city) = target['address']['line2'].split(' ') + a.country = 'Poland' + result.destination = a + + if payload['custom_attributes'].get('dropoff_machine_detail'): + dropoff = payload['custom_attributes']['dropoff_machine_detail'] + a = Address() + a.name = f'{dropoff["name"]} - {dropoff["location_description"]}' + a.address1 = dropoff['address']['line1'] + (a.postalcode, a.city) = dropoff['address']['line2'].split(' ') + a.country = 'Poland' + result.origin = a + + for event in payload['tracking_details']: + stamp = datetime.fromisoformat(event['datetime']) + e = PackageEvent(stamp, '', " ".join(event['status'].split('_')).capitalize()) + result.events.append(e) + + return result -- 2.32.0