eto ang gamit ko, naka-cron lang para lagi fresh ang copy ko.
pcx pricelist downloader...
#!/usr/bin/perl -w
use strict;
use File::Copy;
BEGIN{
$ENV{'http_proxy'} = 'http://<proxyserver>:<port>/';
}
#temp file management
my $pcxpltemp = "/tmp/pcxpl";
unlink $pcxpltemp || die "unable to delete temp file [$!]\n" if(-e $pcxpltemp);
#repository folder management
my $pcxplrepo = "~/pcxpl";
mkdir $pcxplrepo || die "unable to create repository folder [$1]\n" if(!-d $pcxplrepo);
#get the file from web
system "wget http://www.pcx.com.ph/pricelist/pricelist.asp -O $pcxpltemp"
|| die "error downloading file";
#traverse file for the download location
my $fh; my $file2dl = "";
if(open($fh, $pcxpltemp)){
my $line;
while($line = <$fh>){
if($line =~ /a href="pcx\/pcx_(.+?)\.zip" class/i){
$file2dl = $1;
last; #found it! no need to go further
}
}
close $fh;
} else {
die "unable to open temp file for parsing[$!]\n";
}
#check existing repository
my @activefiles = glob("$pcxplrepo/*$file2dl*.zip");
#copy & paste -> laziness is sometimes a virtue
my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
$month++;
my $year = 1900 + $yearOffset;
my $dtoday = sprintf("%04d-%02d-%02d", $year, $month, $dayOfMonth);
my $newfile = 0;
if($#activefiles < 0){
# new file
system "wget http://www.pcx.com.ph/pricelist/pcx/pcx_$file2dl.zip -O $pcxplrepo/$dtoday-$file2dl.zip"
|| die "error downloading [$file2dl] - [$!]\n";
$newfile = 1;
} else {
if(!-e "pricelist.pdf" && !-e "pricelist.xls"){
# get the last file on the list
system "unzip " . $activefiles[$#activefiles];
}
print "----------------------------------------------------------------\n";
foreach my $fil (@activefiles){
print "found file: $fil\n";
}
}
if($newfile == 1){
unlink "pricelist.pdf" if(-e $pcxpltemp);
unlink "pricelist.xls" if(-e $pcxpltemp);
system "unzip $pcxplrepo/$dtoday-$file2dl.zip";
}
move "pcx_$file2dl.pdf", "pricelist.pdf" if(-e "pcx_$file2dl.pdf");
move "pcx_$file2dl.xls", "pricelist.xls" if(-e "pcx_$file2dl.xls");
edit: removed troublesome part. =)