2023-08-24 11:31:07 +00:00
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
import time
|
2023-08-24 12:46:54 +00:00
|
|
|
import importlib
|
2023-08-24 11:31:07 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Optional, Tuple, Never
|
|
|
|
|
|
|
|
from .database import Database
|
2023-08-24 12:46:54 +00:00
|
|
|
from trackers.base import BaseTracker
|
2023-08-24 11:31:07 +00:00
|
|
|
|
|
|
|
from pykeydelivery import KeyDelivery
|
|
|
|
|
2023-08-24 14:05:46 +00:00
|
|
|
|
2023-08-24 11:31:07 +00:00
|
|
|
class Tracker:
|
|
|
|
def __init__(self):
|
|
|
|
logging.basicConfig(
|
|
|
|
format="%(asctime)s %(levelname)s: %(message)s",
|
|
|
|
level=logging.DEBUG,
|
|
|
|
datefmt="%Y-%m-%d %H:%M:%S",
|
|
|
|
)
|
|
|
|
|
2023-08-24 12:46:54 +00:00
|
|
|
self.find_apis()
|
|
|
|
|
|
|
|
def find_apis(self):
|
|
|
|
logging.debug("Finding APIs")
|
|
|
|
|
|
|
|
self.apis = []
|
|
|
|
|
|
|
|
for api in Path(__file__).parent.parent.glob("trackers/*.py"):
|
|
|
|
if api.name in ("__init__.py", "base.py"):
|
|
|
|
continue
|
|
|
|
|
|
|
|
logging.debug(f"Found API {api.stem}")
|
|
|
|
|
|
|
|
module = importlib.import_module(f"trackers.{api.stem}")
|
|
|
|
|
|
|
|
if "tracker" in module.__dict__:
|
|
|
|
tracker = module.tracker
|
|
|
|
logging.debug(f"Found tracker {api.stem}")
|
|
|
|
try:
|
|
|
|
carriers = tracker.supported_carriers()
|
|
|
|
api = tracker()
|
|
|
|
|
|
|
|
for carrier, priority in carriers:
|
|
|
|
self.apis.append((carrier, priority, api))
|
|
|
|
except:
|
|
|
|
logging.exception(f"Error loading tracker {name}")
|
2023-08-24 14:05:46 +00:00
|
|
|
|
2023-08-24 12:46:54 +00:00
|
|
|
def query_api(self, tracking_number: str, carrier: str) -> list:
|
|
|
|
logging.debug(f"Querying API for {tracking_number} with carrier {carrier}")
|
|
|
|
|
|
|
|
for api_carrier, _, api in sorted(self.apis, key=lambda x: x[1], reverse=True):
|
|
|
|
if api_carrier == "*" or api_carrier == carrier:
|
2023-08-24 14:05:46 +00:00
|
|
|
logging.debug(
|
|
|
|
f"Using API {api.__class__.__name__} for {tracking_number} with carrier {carrier}"
|
|
|
|
)
|
2023-08-24 12:46:54 +00:00
|
|
|
return list(api.get_status(tracking_number, carrier))
|
|
|
|
|
2023-08-24 14:05:46 +00:00
|
|
|
def notify(
|
|
|
|
self,
|
|
|
|
title: str,
|
|
|
|
message: str,
|
|
|
|
urgency: str = "normal",
|
|
|
|
timeout: Optional[int] = 5000,
|
|
|
|
) -> None:
|
2023-08-24 11:31:07 +00:00
|
|
|
logging.debug(f"Sending notification: {title} - {message}")
|
|
|
|
|
|
|
|
command = [
|
2023-08-24 14:05:46 +00:00
|
|
|
"notify-send",
|
|
|
|
"-a",
|
|
|
|
"trackbert",
|
|
|
|
"-u",
|
|
|
|
urgency,
|
|
|
|
"-i",
|
|
|
|
str(Path(__file__).parent.parent / "assets" / "parcel-delivery-icon.webp"),
|
|
|
|
]
|
2023-08-24 11:31:07 +00:00
|
|
|
|
|
|
|
if timeout:
|
|
|
|
command += ["-t", str(timeout)]
|
|
|
|
|
|
|
|
command = command + [title, message]
|
|
|
|
|
|
|
|
try:
|
|
|
|
subprocess.run(command)
|
|
|
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
logging.warning("notify-send not found, not sending notification")
|
|
|
|
|
|
|
|
def start_loop(self) -> Never:
|
|
|
|
logging.debug("Starting loop")
|
2023-08-24 12:46:54 +00:00
|
|
|
|
2023-08-24 11:31:07 +00:00
|
|
|
while True:
|
|
|
|
for shipment in self.db.get_shipments():
|
2023-08-24 14:05:46 +00:00
|
|
|
if not shipment.carrier:
|
|
|
|
logging.warning(
|
|
|
|
f"Shipment {shipment.tracking_number} has no carrier, skipping"
|
|
|
|
)
|
|
|
|
continue
|
2023-08-24 11:31:07 +00:00
|
|
|
|
2023-08-24 14:05:46 +00:00
|
|
|
logging.debug(
|
|
|
|
f"Checking shipment {shipment.tracking_number} with carrier {shipment.carrier}"
|
|
|
|
)
|
2023-08-24 11:31:07 +00:00
|
|
|
|
2023-08-24 14:05:46 +00:00
|
|
|
latest_known_event = self.db.get_latest_event(shipment.id)
|
2023-08-24 11:31:07 +00:00
|
|
|
|
2023-08-24 14:05:46 +00:00
|
|
|
events = self.query_api(shipment.tracking_number, shipment.carrier)
|
2023-08-24 12:48:21 +00:00
|
|
|
events = sorted(events, key=lambda x: x.event_time, reverse=True)
|
2023-08-24 11:31:07 +00:00
|
|
|
|
|
|
|
if latest_known_event:
|
2023-08-24 14:05:46 +00:00
|
|
|
logging.debug(
|
|
|
|
f"Latest known event for {shipment.tracking_number}: {latest_known_event.event_description} - {latest_known_event.event_time}"
|
|
|
|
)
|
2023-08-24 11:31:07 +00:00
|
|
|
else:
|
2023-08-24 14:05:46 +00:00
|
|
|
logging.debug(f"No known events for {shipment.tracking_number}")
|
2023-08-24 11:31:07 +00:00
|
|
|
|
2023-08-24 14:05:46 +00:00
|
|
|
logging.debug(
|
|
|
|
f"Latest upstream event for {shipment.tracking_number}: {events[0].event_description} - {events[0].event_time}"
|
|
|
|
)
|
2023-08-24 11:31:07 +00:00
|
|
|
|
|
|
|
latest = True
|
|
|
|
|
|
|
|
for event in events:
|
2023-08-24 14:05:46 +00:00
|
|
|
if (
|
|
|
|
latest_known_event is None
|
|
|
|
or event.event_time > latest_known_event.event_time
|
|
|
|
):
|
|
|
|
event.shipment_id = shipment.id
|
2023-08-24 12:46:54 +00:00
|
|
|
self.db.write_event(event)
|
2023-08-24 11:31:07 +00:00
|
|
|
|
2023-08-24 14:05:46 +00:00
|
|
|
logging.info(
|
|
|
|
f"New event for {shipment.tracking_number}: {event.event_description} - {event.event_time}"
|
|
|
|
)
|
|
|
|
self.notify(
|
|
|
|
f"New event for {shipment.description or shipment.tracking_number}",
|
|
|
|
event.event_description + " - " + event.event_time,
|
|
|
|
urgency="critical" if latest else "normal",
|
|
|
|
)
|
2023-08-24 11:31:07 +00:00
|
|
|
|
|
|
|
latest = False
|
|
|
|
|
|
|
|
time.sleep(300)
|
|
|
|
|
|
|
|
def start(self):
|
2023-08-24 14:05:46 +00:00
|
|
|
self.db = Database("sqlite:///trackbert.db")
|
2023-08-24 11:31:07 +00:00
|
|
|
self.notify("Trackbert", "Starting up")
|
2023-08-24 14:05:46 +00:00
|
|
|
self.start_loop()
|