Skip to content

Commit

Permalink
make ups a bit more resiliant
Browse files Browse the repository at this point in the history
  • Loading branch information
iiPythonx committed Oct 13, 2024
1 parent 1f4761d commit fb9cadb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion usps/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.8.2"
__version__ = "0.8.3"
19 changes: 14 additions & 5 deletions usps/tracking/ups.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
"arrived at facility": "At Facility"
}

# Headers that need to exist for UPS to respond
# I'm not sure why they check *these* headers, but ¯\_(ツ)_/¯
UPS_HEADERS = {
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "en-US,en;q=0.5",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
"Sec-GPC": "1",
"User-Agent": USER_AGENT,
}

# Main class
class UPSTracking:
_session: Session | None = None
Expand All @@ -46,15 +58,12 @@ def track_package(cls, tracking_number: str) -> Package:

try:
if not cls._session.cookies:
cls._session.get("https://www.ups.com/track", headers = {"User-Agent": USER_AGENT}, timeout = 5)
cls._session.get("https://www.ups.com/track", headers = UPS_HEADERS, timeout = 5)

response = cls._session.post(
"https://webapis.ups.com/track/api/Track/GetStatus?loc=en_US",
json = {"Locale": "en_US", "TrackingNumber": [tracking_number]},
headers = {
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "en-US,en;q=0.5",
"User-Agent": USER_AGENT,
headers = UPS_HEADERS | {
"X-XSRF-TOKEN": cls._session.cookies["X-XSRF-TOKEN-ST"]
},
timeout = 5
Expand Down

0 comments on commit fb9cadb

Please sign in to comment.