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:
parent
23407f031e
commit
44fe6a28a2
3 changed files with 59 additions and 1 deletions
|
@ -5,3 +5,7 @@ secret = api_secret
|
||||||
[FedEx]
|
[FedEx]
|
||||||
key = api_key
|
key = api_key
|
||||||
secret = api_secret
|
secret = api_secret
|
||||||
|
|
||||||
|
[DHL]
|
||||||
|
key = api_key
|
||||||
|
secret = api_secret
|
||||||
|
|
|
@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "trackbert"
|
name = "trackbert"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
authors = [
|
authors = [
|
||||||
{ name="Kumi Mitterer", email="trackbert@kumi.email" },
|
{ name="Kumi Mitterer", email="trackbert@kumi.email" },
|
||||||
]
|
]
|
||||||
|
@ -22,6 +22,7 @@ dependencies = [
|
||||||
"postat",
|
"postat",
|
||||||
"glsapi",
|
"glsapi",
|
||||||
"fedextrack",
|
"fedextrack",
|
||||||
|
"dhltrack",
|
||||||
"sqlalchemy",
|
"sqlalchemy",
|
||||||
"alembic",
|
"alembic",
|
||||||
"python-dateutil",
|
"python-dateutil",
|
||||||
|
|
53
src/trackbert/trackers/dhl.py
Normal file
53
src/trackbert/trackers/dhl.py
Normal 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
|
Loading…
Reference in a new issue