Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Package Tracking: Add Fedex, UPS, USPS Regex Triggers (#3211)
Browse files Browse the repository at this point in the history
* Add more names to carriers.yml

* Port Fedex, UPS, USPS regex triggers to Spice

* trigger and handle with query_nowhitespace_nodash, update tests

* remove single use variable

* Cleanup Fedex, UPS, USPS regex. Limit tracking code length

* Add Parcelforce
  • Loading branch information
moollaza authored and pjhampton committed Apr 5, 2017
1 parent 049c3a7 commit 2ee508d
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 19 deletions.
81 changes: 68 additions & 13 deletions lib/DDG/Spice/PackageTracking.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package DDG::Spice::PackageTracking;

use strict;
use DDG::Spice;
use Text::Trim;
use YAML::XS 'LoadFile';
Expand All @@ -11,23 +12,77 @@ spice wrap_jsonp_callback => 1;

spice to => 'https://api.packagetrackr.com/ddg/v1/track/simple?n=$1&api_key={{ENV{DDG_SPICE_PACKAGETRACKR_API_KEY}}}';

my $carriers = LoadFile(share('carriers.yml'));
my @triggers = ('package', 'track package', 'shipping status', 'package tracking', 'track');
my $triggers_re = join "|", @triggers;
my $carriers_re = join "|", @$carriers;
my @carriers = sort { length $b <=> length $a } @{LoadFile(share('carriers.yml'))};
my $triggers_re = qr/package|parcel|track(ing)?|num(ber)?|shipping status|\#/ix;
my $carriers_re = join "|", @carriers;
my $strip_re = qr/$carriers_re|$triggers_re/ix;

triggers startend => @$carriers, @triggers;

handle remainder_lc => sub {
return unless $_;
s/\b$triggers_re\b//g;
s/\b$carriers_re\b//g;
### Regex triggers for queries containing carrier names or words related to pacakge tracking
# Carrier names
triggers query_nowhitespace_nodash => qr/$carriers_re/ix;

# Package words
triggers query_nowhitespace_nodash => qr/$triggers_re/i;

### Regex triggers for queries only containing a tracking number

# UPS
# Soure: https://www.ups.com/content/ca/en/tracking/help/tracking/tnh.html
# To Do: Some additional formats exist
triggers query_nowhitespace_nodash => qr/
^1Z[0-9A-Z]{16}$|
^\d{9,12}$|
^T\d{10}$
/xi;

# Fedex
# Source: https://www.trackingex.com/fedex-tracking.html
# https://www.trackingex.com/fedexuk-tracking.html
# https://www.trackingex.com/fedex-poland-domestic-tracking.html
triggers query_nowhitespace_nodash => qr/
^\d{12,22}$
/xi;

# USPS
triggers query_nowhitespace_nodash => qr/
^\d{9,30}$|
^[A-Z]{2}\d{9}US$|
^\d{20,30}$
/xi;

# Parcelforce
# Source: http://www.parcelforce.com/help-and-advice/sending-worldwide/tracking-number-formats
# Note: May need to restrict pattern #3 if overtriggering
# https://github.com/duckduckgo/zeroclickinfo-goodies/issues/3900
triggers query_nowhitespace_nodash => qr/
^[A-Z]{2}\d{7}$|
^[A-Z]{4}\d{10}$|
^[A-Z]{2}\d{9}[A-Z]{2}$|
^\d{12}$
/xi;


handle query_nowhitespace_nodash => sub {
# strip words
s/$strip_re//ixg;
trim($_);

return unless $_;

# ignore Microsoft knowledge base codes and Luhn Check queries
# e.g. KB2553549
return if m/^kb|luhn/i;

# ignore strings of same number (e.g. 0000 0000 0000)
return if m/^(\d)\1+$/;

# remainder should be numeric or alphanumeric, not alpha
return if m/^[a-z\s]+$/;
# ignore us zipcodes
return if m/^[0-9]{5}+$/;
return unless m/^[a-z0-9\-]{6,}$/;
return if m/^[A-Z]+$/i;

# remainder should be 6-30 characters long
return unless m/^[A-Z0-9]{6,30}$/i;

return $_;
};

Expand Down
2 changes: 2 additions & 0 deletions share/spice/package_tracking/carriers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- dynamex
- ensenda
- fedex
- federal express
- gso
- hongkong post
- india post
Expand All @@ -29,6 +30,7 @@
- tnt
- toll
- ups
- united states postal service
- universal postal union
- usps
- yodel
Expand Down
90 changes: 84 additions & 6 deletions t/PackageTracking.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,117 @@ spice is_cached => 1;

ddg_spice_test(
[qw( DDG::Spice::PackageTracking)],

# Generic
'shipping status C11422907783469' => test_spice(
'/js/spice/package_tracking/c11422907783469',
'/js/spice/package_tracking/C11422907783469',
call_type => 'include',
caller => 'DDG::Spice::PackageTracking'
),
'package C11422907783469' => test_spice(
'/js/spice/package_tracking/c11422907783469',
'/js/spice/package_tracking/C11422907783469',
call_type => 'include',
caller => 'DDG::Spice::PackageTracking'
),
'ontrac C11422907783469' => test_spice(
'/js/spice/package_tracking/c11422907783469',
'/js/spice/package_tracking/C11422907783469',
call_type => 'include',
caller => 'DDG::Spice::PackageTracking'
),
'ontrac package C11422907783469' => test_spice(
'/js/spice/package_tracking/c11422907783469',
'/js/spice/package_tracking/C11422907783469',
call_type => 'include',
caller => 'DDG::Spice::PackageTracking'
),
'C11422907783469 ontrac package' => test_spice(
'/js/spice/package_tracking/c11422907783469',
'/js/spice/package_tracking/C11422907783469',
call_type => 'include',
caller => 'DDG::Spice::PackageTracking'
),

# Fedex
'fedex 9241990100130206401644' => test_spice(
'/js/spice/package_tracking/9241990100130206401644',
call_type => 'include',
caller => 'DDG::Spice::PackageTracking'
),
'178440515632684' => test_spice(
'/js/spice/package_tracking/178440515632684',
call_type => 'include',
caller => 'DDG::Spice::PackageTracking'
),
'9612804882227378545377' => test_spice(
'/js/spice/package_tracking/9612804882227378545377',
call_type => 'include',
caller => 'DDG::Spice::PackageTracking'
),

# UPS
'ups 1Z0884XV0399906189' => test_spice(
'/js/spice/package_tracking/1Z0884XV0399906189',
call_type => 'include',
caller => 'DDG::Spice::PackageTracking'
),
'1Z0884XV0399906189' => test_spice(
'/js/spice/package_tracking/1Z0884XV0399906189',
call_type => 'include',
caller => 'DDG::Spice::PackageTracking'
),

# USPS
'EA 000 000 000 US' => test_spice(
'/js/spice/package_tracking/EA000000000US',
call_type => 'include',
caller => 'DDG::Spice::PackageTracking'
),
'usps 7000 0000 0000 0000 0000' => test_spice(
'/js/spice/package_tracking/70000000000000000000',
call_type => 'include',
caller => 'DDG::Spice::PackageTracking'
),
'usps 37346365253153' => test_spice(
'/js/spice/package_tracking/37346365253153',
call_type => 'include',
caller => 'DDG::Spice::PackageTracking'
),

# Parcelforce / Royal Mail
'parcelforce track PBTM8041434001' => test_spice(
'/js/spice/package_tracking/PBTM8041434001',
call_type => 'include',
caller => 'DDG::Spice::PackageTracking'
),
'royal mail track parcel QE001331410GB' => test_spice(
'/js/spice/package_tracking/QE001331410GB',
call_type => 'include',
caller => 'DDG::Spice::PackageTracking'
),
'PBTM8237263001' => test_spice(
'/js/spice/package_tracking/PBTM8237263001',
call_type => 'include',
caller => 'DDG::Spice::PackageTracking'
),

# Bad Queries
70000000000000000001 => undef,
'what is 70000000000000000000' => undef,
'luhn 1234554651' => undef,
'KB2553549' => undef,
'0000 0000 0000' => undef,

# Too long
'fedex 123456789 123456789 123456789 1234' => undef,

# Too short
'usps 12345' => undef,

# TODO: Display input form prompting for tracking number?
'fedex package tracking' => undef,
'fedex package tracker' => undef,
'package tracking' => undef,
'package tracking online' => undef,

'fedex' => undef,
'fedex website' => undef,
'fedex website' => undef
);

Expand Down

0 comments on commit 2ee508d

Please sign in to comment.