Skip to content

Commit ea24832

Browse files
committed
more step mappings, log unknown mappings, allow unknown time
1 parent 18d7d8a commit ea24832

File tree

5 files changed

+24
-15
lines changed

5 files changed

+24
-15
lines changed

usps/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.9.2"
1+
__version__ = "0.9.3"

usps/__main__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def ordinal(day: int) -> str:
6262
location_max = len(max(package.steps, key = lambda _package: len(_package.location)).location)
6363
for step in package.steps[:10]:
6464
location_block = f"[yellow]{step.location}[/]{' ' * (location_max - len(step.location))}"
65-
con.print(f"\t[cyan]{step.details}[/]\t{location_block}\t[bright_blue]{get_delta(step.location, step.time)}[/]")
65+
con.print(f"\t[cyan]{step.details}[/]\t{location_block}\t[bright_blue]{get_delta(step.location, step.time) if step.time else ''}[/]")
6666

6767
print()
6868

usps/tracking/__init__.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
# Typing
1212
@dataclass
1313
class Step:
14-
details: str
15-
location: str
16-
time: datetime
14+
details: str
15+
location: str
16+
time: datetime | None
1717

1818
@dataclass
1919
class Package:
20-
expected: list[datetime] | None
21-
last_status: str | None
22-
state: str
23-
steps: list[Step]
24-
service: str | None
20+
expected: list[datetime] | None
21+
last_status: str | None
22+
state: str
23+
steps: list[Step]
24+
service: str | None
2525

2626
# Global exceptions
2727
class StatusNotAvailable(Exception):

usps/tracking/ups.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def track_package(cls, tracking_number: str) -> Package:
9494
cls.__map_milestone_name((step["milestoneName"] or {"name": step["activityScan"]})["name"]),
9595
step["location"].replace("United States", "US").upper() if "," in step["location"] else "",
9696
datetime.strptime(f"{step['gmtDate']} {step['gmtTime']}", "%Y%m%d %H:%M:%S").replace(tzinfo = LOCAL_TIMEZONE) +\
97-
timedelta(hours = int(step["gmtOffset"].split(":")[0])) if step["gmtDate"] else datetime.now()
97+
timedelta(hours = int(step["gmtOffset"].split(":")[0])) if step["gmtDate"] else None
9898
)
9999
for step in data["shipmentProgressActivities"]
100100
],

usps/tracking/usps.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,19 @@
3838
"arriving on time": "Package On Time",
3939
"accepted at usps origin facility": "Accepted",
4040
"accepted at usps destination facility": "Accepted",
41+
"acceptance": "Accepted",
4142
"package acceptance pending": "Arrived",
4243
"in/at mailbox": "Delivered",
4344
"garage / other door / other location at address": "Delivered",
4445
"left with individual": "Delivered",
46+
"front door/porch": "Delivered",
4547
"redelivery scheduled for next business day": "Rescheduled",
4648
"available for pickup": "Available",
4749
"reminder to schedule redelivery of your item": "Reminder",
48-
"arriving late": "Arriving Late"
50+
"arriving late": "Arriving Late",
51+
"processed through facility": "Processed",
52+
"processed through usps facility": "Processed",
53+
"origin post is preparing shipment": "Preparing"
4954
}
5055

5156
# Main class
@@ -120,14 +125,18 @@ def track_package(cls, tracking_number: str) -> Package:
120125
if details.lower() == "reminder to schedule redelivery of your item":
121126
location = "SCHEDULE REDELIVERY"
122127

128+
detail_mapping = details.split(", ")[-1].lower()
129+
if detail_mapping not in USPS_STEP_DETAIL_MAPPING:
130+
print(f"Missing step mapping! Post this on GitHub: \"{detail_mapping}\" / \"{details}\"")
131+
123132
steps.append(Step(
124-
USPS_STEP_DETAIL_MAPPING.get(details.split(", ")[-1].lower(), "Unknown")
125-
if "expected delivery" not in details.lower() else "Delivering",
133+
USPS_STEP_DETAIL_MAPPING.get(detail_mapping, "Unknown")
134+
if "expected delivery" not in detail_mapping.lower() else "Delivering",
126135
location or "",
127136
datetime.strptime(
128137
time,
129138
"%B %d, %Y, %I:%M %p" if ":" in time else "%B %d, %Y"
130-
)
139+
) if time.strip() else None
131140
))
132141

133142
postal_product = tree.css_first(".product_info > li:first-child")

0 commit comments

Comments
 (0)