Bump version to 0.2.4, add DPD tracker, and fix sorting of events.

This commit is contained in:
Kumi 2023-08-30 12:14:38 +02:00
parent ad44b1e1cc
commit b12a7f71c5
Signed by: kumi
GPG key ID: ECBCC9082395383F
3 changed files with 47 additions and 3 deletions

View file

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

View file

@ -130,7 +130,7 @@ class Tracker:
logging.exception(f"Error querying API for {shipment.tracking_number}")
return
events = sorted(events, key=lambda x: x.event_time, reverse=True)
events = sorted(events, key=lambda x: x.event_time)
if not events:
logging.debug(f"No events found for {shipment.tracking_number}")
@ -154,7 +154,7 @@ class Tracker:
):
event.shipment_id = shipment.id
self.db.write_event(event)
self.notify_event(shipment, event, event == events[0])
self.notify_event(shipment, event, event == events[-1])
def start_loop(self) -> Never:
logging.debug("Starting loop")

View file

@ -0,0 +1,43 @@
from .base import BaseTracker
from ..classes.database import Event
import json
from datetime import datetime
from dpdtrack.classes.api import DPD as DPDAPI
class DPD(BaseTracker):
def __init__(self, *args, **kwargs):
pass
def get_status(self, tracking_number, carrier):
api = DPDAPI()
status = api.tracking(tracking_number)
events = status["data"][0]["lifecycle"]["entries"]
for event in events:
if "depotData" in event and event["depotData"] is not None:
event_location = f"[{', '.join(event['depotData'])}] "
else:
event_location = ""
event_timestamp = datetime.strptime(event["datetime"], "%Y%m%d%H%M%S")
event_time = event_timestamp.strftime("%Y-%m-%d %H:%M:%S")
yield Event(
shipment_id=0,
event_time=event_time,
event_description=f"{event_location}{event['state']['text']}",
raw_event=json.dumps(event),
)
def supported_carriers(self):
return [
("dpd", 100, "DPD (Austria)"),
]
tracker = DPD