tracking UPS packages from command line

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 ...


Go Back   Usenet Forums > Linux Forums > Linux General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-11-2007
Ignoramus22443
 
Posts: n/a
Default tracking UPS packages from command line

#!/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";
}
}
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:11 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0