2013-02-19 13:07:18 -08:00
package DDG::Goodie::DHL ;
2014-08-20 11:45:33 -07:00
# ABSTRACT: track a package through DHL.
2013-02-19 13:07:18 -08:00
2015-02-22 12:09:29 -08:00
use strict ;
2013-02-19 13:07:18 -08:00
use DDG::Goodie ;
zci is_cached = > 1 ;
zci answer_type = > "dhl" ;
2013-03-10 14:02:42 -07:00
primary_example_queries 'DHL 123456789' ;
secondary_example_queries 'tracking 1234567891' ;
description 'Track a package from DHL' ;
name 'DHL' ;
2013-03-22 14:39:01 -07:00
icon_url "/i/www.dhl.com.ico" ;
2013-03-10 14:02:42 -07:00
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/DHL.pm' ;
category 'ids' ;
topics 'special_interest' ;
2015-01-07 10:37:42 -08:00
attribution github = > [ 'https://github.com/duckduckgo' , 'duckduckgo' ] ;
2013-03-10 14:02:42 -07:00
2013-02-19 13:07:18 -08:00
# Regex for usps.
my $ dhl_qr = qr/dhl/ io ;
my $ tracking_qr = qr/package|track(?:ing|)|num(?:ber|)|\#/ i ;
triggers query_nowhitespace_nodash = > qr /
^ $ dhl_qr . * ? ( [ \ d ] { 9 , } ) $|
^ ( [ \ d ] { 9 , } ) . * ? $ dhl_qr $|
^ ( ? : $ tracking_qr | $ dhl_qr | ) * ( [ \ d ] { 10 } ) ( ? : $ tracking_qr | $ dhl_qr | ) * $
/ xo ;
handle query_nowhitespace_nodash = > sub {
# If a Canada Post package number (2 for exclusively).
my $ is_dhl = 0 ;
# Tracking number.
my $ package_number = '' ;
# Exclsuive trigger.
if ( $ 1 || $ 2 ) {
$ package_number = $ 1 || $ 2 ;
$ is_dhl = 2 ;
}
elsif ( $ 3 ) {
$ package_number = $ 3 ;
my $ checksum = 0 ;
my @ chars = split ( // , $ package_number ) ;
my $ length = scalar ( @ chars ) ;
my $ char_count = 0 ;
my $ odd_sum = 0 ;
my $ even_sum = 0 ;
foreach my $ char ( @ chars ) {
$ char_count + + ;
if ( $ char_count % 2 == 0 ) {
$ even_sum += $ char ;
}
else {
$ odd_sum += $ char ;
}
}
$ even_sum *= 1 ;
$ odd_sum *= 1 ;
# $checksum = ($odd_sum+$even_sum) % 7;
# $checksum = 10-$checksum if $checksum;
$ checksum = join ( '' , @ chars [ 0 .. $ length - 2 ] ) % 7 ;
if ( $ checksum eq $ chars [ - 1 ] ) {
$ is_dhl = 1 ;
}
}
if ( $ is_dhl ) {
2013-03-10 14:02:42 -07:00
return $ package_number , heading = > "DHL Shipment Tracking" , html = > "Track this shipment at <a href='http://www.dhl-usa.com/content/us/en/express/tracking.shtml?brand=DHL&AWB=$package_number'>DHL</a>." ;
2013-02-19 13:07:18 -08:00
}
return ;
} ;
2014-08-20 11:45:33 -07:00
1 ;