#!/usr/bin/perl use strict; use WWW::Mechanize; use LWP::ConnCache; use HTTP::Cookies; use POSIX; sub Debug($) { print join(' ', @_) . "\n" } my $dataDir = "."; # Save to the current directory. my $webUsername = 'your_web_username'; my $webPassword = 'your_web_password'; my $upsAccount = '99X99X'; # Your 6-digit UPS account number (or however many digits it is these days) # start 364 days ago. 365 breaks, so don't try it! my $start_date = strftime("%D", localtime(time() - 3600*24* 364 )); my $end_date = strftime("%D", localtime(time())); $dataDir = $ARGV[0] if (@ARGV); my $m = WWW::Mechanize->new( cookie_jar => HTTP::Cookies->new( file => $dataDir . '/cookies.txt', ignore_discard => 1, autosave => 1) ); $m->conn_cache(LWP::ConnCache->new(total_capacity => 3)); $m->agent_alias('Windows IE 6'); $m->get('https://www.ups.com/viewbill/invoices?loc=en_US&appid=BILLING'); if ($m->content =~ /type=.?password/si) { $m->get('https://www.ups.com/one-to-one/login'); Debug "logging in\n"; $m->submit_form( with_fields => { idCheckRadio => 'hasID', 'uid' => $webUsername, 'password' => $webPassword, }); $m->get('https://www.ups.com/viewbill/invoices?loc=en_US&appid=BILLING'); } else { Debug "already logged in\n"; } $start_date =~ s!/(\d\d)$!/20$1!; $end_date =~ s!/(\d\d)$!/20$1!; $m->post('https://www.ups.com/viewbill/invoices', { accountNumber => $upsAccount, start_date => $start_date, end_date => $end_date, search => 'Search', } ); my $content = $m->content; $content =~ s/\s+/ /gs; while ($content =~ m!.*?(\d\d/\d\d/\d\d\d\d)(.*?) !g) { my ($date, $c) = ($1, $2); $date =~ s!/!-!g; while ($c =~ m!viewBillJavaScript.viewInvoiceDownloadPopup\(([^)]+)\)!g) { my $dloadArgs = $1; my @args = split(/[,']+/, $dloadArgs); # args[2] is the filename extension. # args[1] is the FileIdentifier for download. # the rest of the args are meaningless to us, but look if you like. # Oh yes, and I'm using the date as the filename, since UPS doesn't give us a filename. my $fn = "$dataDir/$date.$args[2]"; my $url = "https://filexfer.ups.com/billinghostapp?FileIdentifier=$args[1]"; if ( ! -s $fn ) { #Debug "\npulling $fn\n via $url"; $m->get($url); my $out; open($out, ">$fn"); if ($out) { Debug("writing $fn"); print $out $m->content; close($out) } else { Debug("unable to open file $fn: $!"); } } else { Debug("$fn already exists."); } } } exit 0;