Skip to content

Instantly share code, notes, and snippets.

@chjj
Created July 15, 2016 20:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chjj/b75cb25138d0643e1db6c18830713dab to your computer and use it in GitHub Desktop.
Save chjj/b75cb25138d0643e1db6c18830713dab to your computer and use it in GitHub Desktop.
# Track fedex/ups/usps/iparcel/upsmi packages.
track-package() {
local open track fedex ups usps len url i \
urls upsmi iparcel iparcel_ usps_ dhl ontrac
if test -z "$1"; then
return 1
fi
if test "$1" = 'open'; then
open=1
shift
fi
#fedex='https://www.fedex.com/Tracking?action=track&language=english&ascend_header=1&cntry_code=us&initial=x=y&tracknumbers=%s'
fedex='https://www.fedex.com/fedextrack/?tracknumbers=%s'
ups='http://wwwapps.ups.com/WebTracking/processInputRequest?tracknum=%s'
usps='https://tools.usps.com/go/TrackConfirmAction!input.action?tLabels=%s'
usps_='https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1=%s'
# iparcel doesn't seem to work, visit page submit and check request body to test:
iparcel='https://tracking.i-parcel.com/secure/track.aspx?txtTrack=%s&cmdTrack=track'
iparcel_='https://tracking.i-parcel.com/secure/track.aspx?ctl00$ContentPlaceHolder1$txtTrack=%s&ctl00$ContentPlaceHolder1$cmdTrack=track'
upsmi='http://www.ups-mi.net/packageID/PackageID.aspx?PID=%s'
dhl='http://webtrack.dhlglobalmail.com/?trackingnumber=%s'
ontrac='http://www.ontrac.com/trackingres.asp?tracking_number=%s'
i=0
urls=()
for track in "$@"; do
url=''
if grep -q '^[usfmio]\+:' <<< "$track"; then
if grep -q '^f:' <<< "$track"; then
len=12
elif grep -q '^u:' <<< "$track"; then
len=18
elif grep -q '^us:' <<< "$track"; then
len=22
elif grep -q '^i:' <<< "$track"; then
len=17
elif grep -q '^um:' <<< "$track"; then
len=30
elif grep -q '^o:' <<< "$track"; then
len=15
fi
track=$(sed 's/^[usfmio]\+://' <<< "$track")
else
len=$(echo -n "$track" | wc -m)
fi
if test $len -eq 12; then
url=$(sed "s/%s/${track}/" <<< "$fedex")
# Was this before we added i-parcel:
# elif test $len -eq 18 || grep -q '[A-Z]' <<< "$track"; then
elif test $len -eq 18; then
url=$(sed "s/%s/${track}/" <<< "$ups")
elif test $len -eq 22; then
url=$(sed "s/%s/${track}/" <<< "$usps")
elif test $len -eq 17; then
url=$(sed "s/%s/${track}/" <<< "$iparcel")
elif test $len -eq 30; then
# USPS sometimes uses 30-digit numbers now?
# NOTE: DHL uses 30-digit numbers
# url=$(sed "s/%s/${track}/" <<< "$upsmi")
url=$(sed "s/%s/${track}/" <<< "$dhl")
elif test $len -eq 15 && grep -q '^C' <<< "$track"; then
url=$(sed "s/%s/${track}/" <<< "$ontrac")
elif test $len -eq 13; then
# Most likely a usps package from china:
if grep -q '^L.*CN$' <<< "$track"; then
url=$(sed "s/%s/${track}/" <<< "$usps")
fi
fi
if test -z "$url"; then
echo 'Unknown tracking number.' >& 2
continue
fi
urls[i++]=$url
done
for url in "${urls[@]}"; do
echo "$url"
done
if test -n "$open"; then
if test -n "$BROWSER"; then
"$BROWSER" "${urls[@]}"
elif which xdg-open > /dev/null 2>& 1; then
xdg-open "${urls[@]}"
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment