feat: Add DHL configuration and implementation

This commit adds the DHL configuration and implementation to the project.

- Added DHL configuration to the config file.
- Updated the project version to 0.2.1 in the pyproject.toml file.
- Created a new file for the DHL tracker implementation.
- Implemented the DHL tracker functionality to get shipment events.
- Added DHL to the list of supported carriers.
This commit is contained in:
Kumi 2023-08-29 10:47:52 +02:00
parent 23407f031e
commit 44fe6a28a2
Signed by: kumi
GPG key ID: ECBCC9082395383F
3 changed files with 59 additions and 1 deletions

View file

@ -5,3 +5,7 @@ secret = api_secret
[FedEx]
key = api_key
secret = api_secret
[DHL]
key = api_key
secret = api_secret

View file

@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "trackbert"
version = "0.2.0"
version = "0.2.1"
authors = [
{ name="Kumi Mitterer", email="trackbert@kumi.email" },
]
@ -22,6 +22,7 @@ dependencies = [
"postat",
"glsapi",
"fedextrack",
"dhltrack",
"sqlalchemy",
"alembic",
"python-dateutil",

View file

@ -0,0 +1,53 @@
from .base import BaseTracker
from ..classes.database import Event
from dhltrack import DHL as DHLAPI
from dateutil.parser import parse
import json
import logging
class DHL(BaseTracker):
def __init__(self, *args, **kwargs):
self.api = DHLAPI.from_config("config.ini")
def get_status(self, tracking_number, carrier):
response = self.api.track(tracking_number)
try:
all_events = response["shipments"][0]["events"]
logging.debug(f"Got events for {tracking_number}: {len(all_events)}")
except KeyError:
logging.error(f"Error getting events for {tracking_number}: {all_events}")
return
events = sorted(
all_events, key=lambda x: x["timestamp"], reverse=True
)
for event in events:
event_time = parse(event["timestamp"]).strftime("%Y-%m-%d %H:%M:%S")
try:
event_locality = f"[{event['location']['address']['addressLocality']}] "
except KeyError:
event_locality = ""
event_description = f"{event_locality}{event['description']}"
yield Event(
shipment_id=0,
event_time=event_time,
event_description=event_description,
raw_event=json.dumps(event),
)
def supported_carriers(self):
return [
("dhl", 100),
]
tracker = DHL