Add FedEx tracking functionality and configuration to the code
- Add FedEx configuration to config.ini - Add FedEx tracking module to dependencies - Implement FedEx tracking functionality - Add FedEx as a supported carrier in the tracker
This commit is contained in:
parent
07619ff4a8
commit
01a0d1dc35
4 changed files with 59 additions and 2 deletions
|
@ -1,3 +1,7 @@
|
|||
[KeyDelivery]
|
||||
key = api_key
|
||||
secret = api_secret
|
||||
|
||||
[FedEx]
|
||||
key = api_key
|
||||
secret = api_secret
|
||||
|
|
|
@ -21,6 +21,7 @@ dependencies = [
|
|||
"pykeydelivery",
|
||||
"postat",
|
||||
"glsapi",
|
||||
"fedextrack",
|
||||
"sqlalchemy",
|
||||
"python-dateutil",
|
||||
]
|
||||
|
|
|
@ -9,8 +9,6 @@ from typing import Optional, Tuple, Never
|
|||
from .database import Database
|
||||
from ..trackers.base import BaseTracker
|
||||
|
||||
from pykeydelivery import KeyDelivery
|
||||
|
||||
|
||||
class Tracker:
|
||||
loop_interval = 60
|
||||
|
|
54
src/trackbert/trackers/fedex.py
Normal file
54
src/trackbert/trackers/fedex.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
from .base import BaseTracker
|
||||
from ..classes.database import Event
|
||||
|
||||
from fedextrack import FedEx as FedExAPI
|
||||
from dateutil.parser import parse
|
||||
|
||||
import json
|
||||
import logging
|
||||
|
||||
|
||||
class FedEx(BaseTracker):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.api = FedExAPI.from_config("config.ini")
|
||||
|
||||
def get_status(self, tracking_number, carrier):
|
||||
response = self.api.track_by_tracking_number(tracking_number)
|
||||
|
||||
try:
|
||||
all_results = response["output"]["completeTrackResults"][0]["trackResults"]
|
||||
|
||||
all_events = []
|
||||
|
||||
for result in all_results:
|
||||
events = result["scanEvents"]
|
||||
for event in events:
|
||||
all_events.append(event)
|
||||
|
||||
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["date"], reverse=True
|
||||
)
|
||||
|
||||
for event in events:
|
||||
event_time = parse(event["date"]).strftime("%Y-%m-%d %H:%M:%S")
|
||||
event_description = f"{event['scanLocation']['city'], event['scanLocation']['countryCode']} {event['eventDescription']}"
|
||||
|
||||
yield Event(
|
||||
shipment_id=0,
|
||||
event_time=event_time,
|
||||
event_description=event_description,
|
||||
raw_event=json.dumps(event),
|
||||
)
|
||||
|
||||
def supported_carriers(self):
|
||||
return [
|
||||
("fedex", 100),
|
||||
]
|
||||
|
||||
|
||||
tracker = FedEx
|
Loading…
Reference in a new issue