Perl Script to track UPS-Packages

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 = "

formatting link
$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 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 to
Ignoramus22443
Loading thread data ...

08/14/2007 Weight: 13.00 Lbs
08/14/2007 Weight: 15.00 Lbs
08/14/2007 Weight: 12.00 Lbs

this is worse than the webpage.

Reply to
Cydrome Leader

some relief.

Scheduled: 08/14/2007 Weight: 13.00 Lbs

Scheduled: 08/14/2007 Weight: 15.00 Lbs

Scheduled: 08/14/2007 Weight: 12.00 Lbs

But with a cron script, you can get it to send you updates. Thanks iggy.

Wes

Reply to
Wes

some relief.

Scheduled: 08/14/2007 Weight: 13.00 Lbs

Scheduled: 08/14/2007 Weight: 15.00 Lbs

Scheduled: 08/14/2007 Weight: 12.00 Lbs

That's precisely what I am doing Wes. I run a check-update script every hour at 12 minutes to send me updates. Thanks.

i
Reply to
Ignoramus22443

Care to share that?

Wes

Reply to
Wes

I have a script called keep-an-eye. This script runs whatever command it is given and saves its output. If the output changes compared to last time it ran before, it sends me an email and uses "wall" to notify me.

This way I keep an eye on, say, my own ebay auctions, as well as on what I earned through commission junction, pagerank of my websites, prices of my stocks (checked once daily), UPS tracking, my ebay seller feedback, my favorite ebay searches (I can search ebay through command line), etc. Whatever I ilke and can check.

How often I run them depends on what makes sense.

You run it as

keep-an-eye.sh k56_frame_motors ebay-search.pl --nobids "electric motor k56"

this would run

ebay-search.pl --nobids "electric motor k56"

and notify you of changes under heading "k56_frame_motors change detected".

Normally you would run it from cron.

Basically I program in whatever I like to check compulsively, so that now the computer checks it compulsively instead of me.

i ###################################################################### #!/bin/sh

NAME=$1; shift COMMAND=$@

FILE=/tmp/eye.$NAME

if [ "x$1" = "x--reset" ] ; then shift COMMAND=$@ $COMMAND > $FILE exit 0 fi

COMMAND=$@

if $COMMAND > $FILE.tmp 2>>$FILE.error ; then # We succeeded if [ -f $FILE ]; then diff $FILE $FILE.tmp > $FILE.diff if [ "$?" = 1 ] ; then ( echo Watching $name: change detected. echo ================================== cat $FILE.diff echo ================================== cat $FILE.tmp ) | Mail -s "WATCH: Change in $NAME on `date`" `whoami` echo Change in $NAME on `date` | wall fi fi rm -f $FILE.diff mv $FILE.tmp $FILE else # We failed, do nothing echo nothing done, error code $? >> $FILE.error fi

Reply to
Ignoramus22443

It works fine using perl/w2k/cygwin.

Token Metalworking Content: Tracking my 4 1/2 MT to 5C adaptor that is on its way to me.

After having success with that I decided again to try to get net::ebay to work.

I get "Can't locate object method "new" via package "Net::eBay" at /usr/bin/ebay-search.pl line 21."

Reading your webpage I see I need to register as a developer and get credentials. I tried doing that using my current ebay username but a different password that fit their rules.

I can log back into developer.ebay.com fine but when I try to generate a sandbox key it sends me to a login page where it refuses to recognize me using either my developer password or my regular ebay password.

Any ideas?

Wes

Reply to
Wes

Looks like you do not have it installed.

Wes, all I remember about getting the ID is that it was a real PITA -- not because of bureaucratic hoops to jump through, but just because it was a complicated process. You need to get yourself "self certified", I only remember that. Check ebay's developer message boards, they are full of people trying to register.

I sell through command line only, it is a big productivity helper.

i
Reply to
Ignoramus10724

Hmmm. Wonder if anyone has ported Perl to the iPhone yet, this would be a nice app to have in my pocket. Website works, yeah, but this would be a nice little standalone. Cron could even update it for me every (n) minutes so it's always updated. Hmmm...

Reply to
Dave Hinz

some relief.

Is there cron on iphone?

i
Reply to
Ignoramus28804

Seems to be, I get this:

-sh-3.2# uname -a Darwin Dave Hinz's iPhone 9.0.0d1 Darwin Kernel Version 9.0.0d1: Fri Jun

22 00:38:56 PDT 2007; root:xnu-933.0.1.178.obj~1/RELEASE_ARM_S5L8900XRB iPhone1,1 Darwin

-sh-3.2# ls -als /usr/sbin/cron

52 -rwxr-xr-x 1 root wheel 51792 Jul 23 22:48 /usr/sbin/cron

-sh-3.2# crontab -l

-sh-3.2# crontab -e crontab: /usr/bin/vi: No such file or directory crontab: "/usr/bin/vi" exited with status 1

-sh-3.2#

So, it's got cron but I don't have a crontab, and no vi. Easy enough to write the file with cat in the shell directly, or since the iphone is running sshd, I can just scp whatever I want over there. Nate True is doing amazing things. I've got apache and a wiki running on mine, and just for the hell of it, a Nintendo Entertainment System emulator (no sound yet though). Some of the apps are releasing updates several times a _day_.

Reply to
Dave Hinz

Dave, to say that I am highly impressed, would be a huge understatement!!!

i
Reply to
Ignoramus28804

Heh. I can't take credit for any of it, just installing hacks that have been released days ago. It's a BSD box, from the way the syntax behaves and the init sequence. Once the shell and sshd are on there, you can deploy whatever you want to it just like any other Unix box. Seems to me once we have Perl going, if I can use your eBay and UPS tools we can get some nice standalone apps on there, what license have you released these under?

Reply to
Dave Hinz

According to Dave Hinz :

You need to set the environment variable EDITOR to point to a plain text editor which you *do* have, if you don't have vi.

Or you need to add vi -- if you know how to use it.

Beware that some versions of cron, such as the one in Solaris 10 will not update just because you've changed the file. The "crontab -e" command also sends the proper signal to cron itself to force it to re-read the crontab. It complies an internal table to make its operation more efficient.

:-)

Enjoy, DoN.

Reply to
DoN. Nichols

Hi Guys:

I'm going through the current topics to hopefully suggest a way to tag valid RCM traffic.

Add RCM: on the beginning of the title line - BEFORE the obligatory OT, OK?

Then we can filter on that tag.

Also need to add a filter for Re: RCM: to get the reply lines.

Hang tight - keep posting.

CaveLamb

Hey Ig!

Can you write something that will identify these guys and run some rebar up their collective ISPs?

I'd much appreciate it.

Richard

Reply to
cavelamb himself

some relief.

Why not just track by email? I used to use this some of the time and a quick check shows that it is still available:

=== UPS Track by E-mail

How To Track by E-mail You can track up to 25 UPS shipments at a time by sending your tracking numbers in an e-mail message. Follow these steps:

Open your e-mail client and create an e-mail message addressed to snipped-for-privacy@ups.com. If you are tracking a single tracking number, enter it as the subject line or within the body of the e-mail message. To track multiple shipments by e-mail, you can enter up to

25 UPS tracking numbers in the body of an e-mail message. There is no need to create a subject for the message. Send the message.

A detailed tracking response will be automatically returned to you.

Note: This feature is not available for freight shipment tracking. ===

See:

formatting link
No where near as much fun as creating your own custom little applet for it though :)

Reply to
Leon Fisk

formatting link

Also CC snipped-for-privacy@ups.com as they switch between using the address you mention above and this one at times for some reason... So I simply e-mail BOTH!

Reply to
Joe AutoDrill

(snip)

Yup. Or, just use cat > filename as Crom intended.

I. Beg. Your. Pardon. Sir.

HUP is your friend, yup.

Reply to
Dave Hinz

It's slow, lags by an hour or two vs. the website, last time I compared.

Exactly. An app, not an applet, but the point of it is only partly to get the functionality.

Reply to
Dave Hinz

Yep -- *if* you can get the cron daemon to re-read the crontab afterwards. I was not with Solaris 10. I *had* to have it signaled by the crontab(1) program. "kill -HUP" did *not* work. the man page says (in part):

====================================================================== cron only examines crontab or at command files during its own process initialization phase and when the crontab or at command is run. This reduces the overhead of checking for new or changed files at regularly scheduled intervals. ======================================================================

Solaris 10 has done some weird things to the daemon handling. I don't know what signal cron actually wants to see to re-read, and it is not documented in the man page. (Well ... Solaris 10 is now open source, so I guess that I could dig through that. :-) But the crontab(1) program in Solaris 10 is the *only* way to *remove* a crontab -- with the "-r" option.

You -- or whoever else is following this thread. I try to cover more than just the two of us.

Not sufficient with the latest versions of Solaris 10. It must require some other signal instead. I was having a problem with a new crontab which had just been copied in there not being read, even with a SIGHUP aimed at cron, so what I had to do was to copy it elsewhere, and then use "crontab filename" to re-add the contents to the list of crontabs -- in the same place it was before. Until I did that, the crontab(1) program kept claiming that I had no crontab (for root). :-)

Of course, the iphone presumably is BSD-based, and at least OpenBSD still says to use SIGHUP for reloading the crontabs.

Enjoy, DoN.

Reply to
DoN. Nichols

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.