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 "http://wwwapps.ups.com/WebTracking/processInputRequest?sort_by=status&tracknums_displayed=1&TypeOfInquiryNumber=T&loc=en_US&InquiryNumber1 =$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-> = $value; }
print "$track"; print sprintf( " %19s", $comment ); print " Status: $item->" if defined $item->; print " Scheduled: $item->{'Scheduled Delivery'}" if defined $item->{'Scheduled Delivery'}; print " Weight: $item->" if defined $item->; print "\n";
if( 0 ) { foreach my $k (sort keys %$item ) { print "\t\t$k ==> $item->\n"; } print "\n"; } }
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

this is worse than the webpage.
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

But with a cron script, you can get it to send you updates. Thanks iggy.
Wes
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

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
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

Care to share that?
Wes
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

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
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

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
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

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
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

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...
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

Is there cron on iphone?
i
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload
On Sun, 12 Aug 2007 09:55:14 -0500, 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_.
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

Dave, to say that I am highly impressed, would be a huge understatement!!!
i
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload
On Sun, 12 Aug 2007 11:37:12 -0500, 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?
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

    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.
--
Email: < snipped-for-privacy@d-and-d.com> | Voice (all times): (703) 938-4564
(too) near Washington D.C. | http://www.d-and-d.com/dnichols/DoN.html
  Click to see the full signature.
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload
(snip)

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

I. Beg. Your. Pardon. Sir.

HUP is your friend, yup.
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

    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.
--
Email: < snipped-for-privacy@d-and-d.com> | Voice (all times): (703) 938-4564
(too) near Washington D.C. | http://www.d-and-d.com/dnichols/DoN.html
  Click to see the full signature.
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

I'm still trying to reach an uneasy truce with Sol10. And we've got, er, about 600 of the beasties to support, maybe more.

So a crontab entry to run crontab to check the crontab then. Yikes. I think I just b0rked by brane.

Annoying, that. I like using sed to edit things, I don't like to have to use svcadmin before and after but (shrug) whatever.

I know...just wanted to, er, express something or other.

Interesting. Must play with that tomorrow, learning new stuff every day. Which, 17 years into playing with Unix of one flavor of another, explains why I LOVE MY JOB. Even when (ahem) I have a DBA camping in my cube for the last week with something that turns out to not be my problem, like I said from the get-go. Ahem. Hi Gary.

That'd be my assumption, haven't tested it yet just having found Tetris.app and been somewhat distracted. It's really amazing to have a Unix box in my pocket, gotta say that. It even works for phone calls, oddly enough.
Dave
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

    :-) There are quite a few strange things that they have introduced -- along with some very *nice* things, like zfs (man zvf & zpool). A lot easier to use than the meta* way to do software RAID on the Suns.

    :-)
    I had copied a root crontab file from another system (to save some work :-) and I wanted to add something else to it and was getting "crontab: can't open your crontab file." So, I moved it to a new location, and just typed:
    crontab path-to-file
and it copied it into the crontab (where it had been before), but now it admitted that it existed. :-)
    Interestingly enough, I keep an account with the same UID and GID as root, but with a different shell by default, and when I try
    crontab -l username
I get:
    crontab: you must be super-user to access another user's crontab     file
and if I su to root, I *still* get that. :-)

    Well ... I've found that I can use crontab to disable a user's crontab by simply commenting out the active lines. Not sure whether you could set EDITOR to sed and use that, but it is worth a try.
    [ ... ]

    :-)
    I must admit that the unix in a pocket is about the only reason that *I* would consider getting an iphone -- and it wouldn't even need to make phone calls. :-) I'm still using a B&W cell phone that just barely knows how to do texting -- and I don't even use it for that -- just for the occasional phone call. (And somehow, it seems to have gotten onto someone's list of fax numbers, so it tends to get several calls a month before I wake up -- no number in the caller ID, and only rarely have I been awake and out here where the phone lives in its charger when a call came in, which is how I know that they think that it is a fax.
    Enjoy,         DoN.
P.S.    The newsgroup is pretty quiet for the moment, with everybody     hiding from the sporge flood. :-)
--
Email: < snipped-for-privacy@d-and-d.com> | Voice (all times): (703) 938-4564
(too) near Washington D.C. | http://www.d-and-d.com/dnichols/DoN.html
  Click to see the full signature.
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload
I have been re-reading crontab from crontab for quite a while now.
I also synchronize all configs (boot scripts, scripts, aliases, notes, etc etc) and keep them in CVS, to automatically commit and update all machines every night.
i
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

We're still using ODS/Solaris Disk Suite on nearly everything, which meets our needs. But, our Unix High Guru is actively pursuing zfs and all things related. As the lead of the Unix run team, I look forward to it.

It's OK, happens all the time lately.

Good to know.

Hrrrrm, so it knows you're not the real UID 0? what if you type # id

OK, how about su - root

Easier to just load the vi binary on the iPhone. No escape key though. Hrrrrmf.

Gary left me alone all day, the biggest prod cluster in the biz did silly things and I got to deal with that. He was nice enough to recognize the relative importance.

Thing is, everything the darn thing does, it does _VERY_ well. Had it for a week, thought "You know, version 2.0 would be cool if they'd add live traffic conditions to the maps app", then went into the maps app, clicked on the little car icon (which I figured meant driving directions) and there it is, red/yellow/green traffic conditions. Talk about a killer feature that they completely don't advertise.

What sporge flood? Filters are your friend.
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

Polytechforum.com is a website by engineers for engineers. It is not affiliated with any of manufacturers or vendors discussed here. All logos and trade names are the property of their respective owners.