Nikita Travkin: 1 carrier: Add Russian Post 3 files changed, 77 insertions(+), 0 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/26863/mbox | git am -3Learn more about email & git
--- README.md | 1 + data/org.postmarketos.Shipments.appdata.xml | 1 + shipments/carrier.py | 75 +++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/README.md b/README.md index 2412cf9..ae4539d 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Supported carriers * UPS * PostNL +* Russian Post * InPost Installing diff --git a/data/org.postmarketos.Shipments.appdata.xml b/data/org.postmarketos.Shipments.appdata.xml index 4b9b301..4a4f369 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>Russian Post</li> <li>InPost</li> </ul> </description> diff --git a/shipments/carrier.py b/shipments/carrier.py index b184a53..ee88818 100644 --- a/shipments/carrier.py +++ b/shipments/carrier.py @@ -96,6 +96,81 @@ class PostNL(Carrier): pass return result +class RussianPost(Carrier): + DISPLAYNAME = 'Russian Post' + + @classmethod + def identify(cls, code): + if len(code) != 14: + return False + + a = 0 + b = 0 + x = True + for char in code[:-1]: + if not char.isdigit(): + return False + num = int(char) + + if x: + a += int(num) * 3 + else: + b += int(num) + x = not x + + s = (a + b) % 10 + calculated = 10 - s + if code[-1].isdigit(): + check = int(code[-1]) + else: + return False + + return calculated == check + + def get_info(self, code, extra): + url = 'https://www.pochta.ru/tracking?p_p_id=trackingPortlet_WAR_portalportlet&p_p_lifecycle=2&p_p_resource_id=tracking.get-by-barcodes' + response = requests.post(url, data = {'barcodes':code}) + payload = response.json()['response'][0]['trackingItem'] + + result = PackageInfo(code, self.DISPLAYNAME) + result.carrier_name = self.DISPLAYNAME + result.status = payload['commonStatus'] + if payload['globalStatus'] == 'ARCHIVED': + result.status_category = StatusCategory.DELIVERED + elif payload['globalStatus'] == 'REGISTERED': + result.status_category = StatusCategory.LABEL_CREATED + else: + result.status_category = StatusCategory.IN_TRANSIT + + result.description = payload['title'] + result.delivery_note = payload['mailType'] + ' (' + payload['mailCtgText'] + ')' + result.weight = payload['weight'] + + for event in payload['trackingHistoryItemList']: + # Timestamp is a full ISO 8601 with timezones + stamp = datetime.fromisoformat(event['date'][0:10]) + e = PackageEvent(stamp, event['description'], event['humanStatus']) + result.events.append(e) + + if result.status == "": + result.status = result.events[0].label + + r = Address() + r.name = payload['recipient'] + r.city = payload['destinationCityName'] + r.country = payload['destinationCountryName'] + r.postalcode = payload['indexTo'] + result.destination = r + + s = Address() + s.name = payload['sender'] + s.city = payload['originCityName'] + s.country = payload['originCountryName'] + s.postalcode = payload['indexFrom'] + result.origin = s + + return result + class UPS(Carrier): DISPLAYNAME = 'UPS' -- 2.34.1