For those people who like to track UPS packages from command line. Finally some relief.
Example output:
1Z1467XF0346666414 Toshiba-2.2-kW Status: In Transit - On Time Scheduled: 08/14/2007 Weight: 13.00 Lbs 1Z1467XF0346662024 Toshiba-3.7-kW Status: In Transit - On Time Scheduled: 08/14/2007 Weight: 15.00 Lbs 1ZA4Y0130396668290 DinoRight Status: In Transit - On Time Scheduled: 08/14/2007 Weight: 12.00 Lbs 1Z2R899R0316660175 Bargainland Status: In Transit 1Z2R899R0316660184 Status: In Transit#!/usr/bin/perl ###################################################################### # perl script for tracking UPS packages. # To run it on command line, you give it a list of UPS tracking numbers # (no spaces inside the numbers). You can append a comment (no spaces # after a "/" character to each tracking number. # # Example: # # ups-track.pl 1Z903475387458375/Newegg 1Z387847878676764/Fred 1Z938983989489898 # # Copyright 2007 Igor Chudov snipped-for-privacy@algebra.com # Released under GNU GPL version 3. # ######################################################################
use strict; use warnings; use vars qw( $ua );
use LWP::UserAgent; use HTTP::Request::Common; use HTML::TreeBuilder;
#use Data::Dumper; $ua = LWP::UserAgent->new;
#$cookies = new HTTP::Cookies( file => "cookies.txt", autosave => 1 );
sub make_tree { my ($html) = @_; my $tree = HTML::TreeBuilder->new; $tree->parse( $html ); return $tree; }
sub get_request { my ($req) = (@_); #$cookies->add_cookie_header($req); my $res = $ua->request($req); if ($res->is_success) { #$cookies->extract_cookies($res); return $res; } else { print STDERR "Failed to execute HTTP request: ", $res->status_line,
print STDERR $res->as_string; }
return undef;
}sub get_webpage { my ($url) = @_; my $req = HTTP::Request->new(GET => $url); my $result = get_request( $req ); if( !$result ) { print STDERR "Failed to get url '$url'.\n"; } return $result; }
my $usage = "USAGE: $0 tracknum";
foreach my $track (@ARGV) { my $comment = ""; $comment = $1 if $track =~ s#/(.*)$##;
my $url = "
my $tree = make_tree( $text );
#$tree->dump;
my @table = $tree->look_down( '_tag', 'table', sub { return $_[0]->as_text =~ /Status:/; } ); #print Dumper( @table ); my $table = pop @table;
#print Dumper( $table ); #exit 0; my $t = $table->as_HTML;
my @rows = $table->content_list; my $item = {}; foreach my $row (@rows) { #print "ROW=$row.\n"; #$row->dump; print "\n================================\n"; next unless ref( $row ); my @cols = $row->content_list; next unless 2 as_text, $cols[1]->as_text);
$key =~ s/^\s+//; $key =~ s/\s+$//; $key =~ s/ +/ /g; $key =~ tr/\x80-\xFF//d; $value =~ s/^\s+//; $value =~ s/\s+$//; $value =~ s/ +/ /g; $value =~ tr/\x80-\xFF//d; #print "$key=>$value.\n"; next unless $key =~ /(.*):$/; $key = $1; $item->{$key} = $value; }
print "$track"; print sprintf( " %19s", $comment ); print " Status: $item->{Status}" if defined $item->{Status}; print " Scheduled: $item->{'Scheduled Delivery'}" if defined $item->{'Scheduled Delivery'}; print " Weight: $item->{Weight}" if defined $item->{Weight}; print "\n";
if( 0 ) { foreach my $k (sort keys %$item ) { print "\t\t$k ==> $item->{$k}\n"; } print "\n"; } }