This is a discussion on tracking UPS packages from command line within the Linux General forums, part of the Linux Forums category; #!/usr/bin/perl ################################################## #################### # perl script for tracking UPS packages. # To run it on command line, you give it a list ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
#!/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 ichudov@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 = "http://wwwapps.ups.com/WebTracking/processInputRequest?sort_by=status&tracknums_displ ayed=1&TypeOfInquiryNumber=T&loc=en_US&InquiryNumb er1=$track&track.x=0&track.y=0"; my $text = get_webpage( $url )->as_string; 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 <= @cols; my ($key, $value) = ($cols[0]->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"; } } |