#!/usr/local/bin/perl -w use strict; use Mail::Box::Manager; use HTML::TreeBuilder; use Proc::Background; use DBI; use DBD::mysql; #~ use lib '/usr/local/lib/perl5/site_perl/5.10.0/'; my $dataid; my $query; my $query_handle; my $data_type; my $format; my $cmd; my $mailspool = "/var/spool/mail/thor"; my $tmpfldr = "/home/thor/tmp"; my $mgr = Mail::Box::Manager->new; my $folder = $mgr->open(folder => $mailspool, access => 'rw'); my @HAS; #~ DBI variables my $host = "localhost"; my $database = "thor"; my $user = "thor"; my $pw = "*******"; my $port = 22; my $dsn = "DBI:mysql:database=$database;host=$host;port=$port"; my $db = DBI->connect($dsn, $user, $pw,{'PrintError' => 1, 'RaiseError' => 1}) or die "Unable to connect: $DBI::errstr\n"; if (defined($folder)){ my $emails = $folder->messages; print "There are $emails in $mailspool\n\nSearching For HAS URLs...\n"; foreach my $message ($folder->messages) { my $subject = $message->get('Subject'); if ($subject =~ /^HAS\sData\sRequest:\sHAS\d{9}\sCompleted/){ #~ print "$subject\n"; my $body = $message->body; my @body = split(/\n/,$body); foreach my $string (@body){ next if $string !~ /^http:/; #~ print "$string\n\n"; push(@HAS,$string); } } #~ Need Root access to delete $message->delete; } $folder->close; if (my $hasnum = @HAS == 0) { print "\nThere are No Messages in the folder: $mailspool\n"; exit; } print "HAS URLs Stored: @HAS\n"; foreach my $HAS (@HAS){ my @hrefs; #~ Get the HAS number my @HAS_break = split(/\//,$HAS); #~ Search Database for HAS number #~ Retreive Data Type $query = "select datatype from radar_data where hasid ='$HAS_break[5]' limit 1;"; $query_handle = $db->prepare($query); $query_handle->execute(); $query_handle->bind_columns(\$data_type); $query_handle->fetch(); #~ $data_type = to_int($data_type); #~ print "$data_type\n"; #~ Update status to 2 $query = "select status, dataid from radar_data where datatype=$data_type and hasid ='$HAS_break[5]';"; $query_handle = $db->prepare($query); $query_handle->execute(); while(my @row = $query_handle->fetchrow_array()) { #ERROR HERE #~ Update only available data to 'Email Received' status if ($row[0] eq '1') { $query = "update radar_data set status=2 where dataid=$row[1] and datatype=$data_type and hasid ='$HAS_break[5]';"; $query_handle = $db->prepare($query); $query_handle->execute(); } } print "Data type for $HAS: Level $data_type\n"; $format = "/thordata1/nexrad/NIDS" if $data_type eq '3'; $format = "/thordata1/Level2" if $data_type eq '2'; #~ print "$data_type\n"; print "Using Directory: $format\n"; #~ Create HAS directory $cmd = "mkdir $format/$HAS_break[5]"; print "$cmd\n"; system($cmd); #~ Download HAS files $cmd = "wget $HAS -O $tmpfldr/index.html"; print "$cmd\n"; system($cmd); #~ Parse the html from NCDC my $tree = HTML::TreeBuilder->new_from_file("$tmpfldr/index.html") or die "Cant build tree: $!\n"; my @anchors = $tree->look_down(_tag => q{a}); #~ print "@anchors\n"; foreach my $anchor (@anchors) { if ($anchor->as_text =~ /\.tar/) { push @hrefs, $anchor->attr(q{href}); print "File Available: ",$anchor->as_text,"\n"; } } #~ Download Files foreach my $hrefs (@hrefs) { $cmd = "wget $HAS$hrefs -O $format/$HAS_break[5]/$hrefs"; print "$cmd\n"; system($cmd); } #~ Update status to 3 $query = "select status, dataid from radar_data where datatype=$data_type and hasid ='$HAS_break[5]';"; $query_handle = $db->prepare($query); $query_handle->execute(); while(my @row = $query_handle->fetchrow_array()) { #~ Update only available data to 'Downloaded' status if ($row[0] eq '2') { $query = "update radar_data set status=3 where dataid=$row[1] and datatype=$data_type and hasid ='$HAS_break[5]';"; $query_handle = $db->prepare($query); $query_handle->execute(); } } #~ Delete temp file/html $cmd = "rm -rf /research/SSRG/ThOR/tmp/index.html"; print "$cmd\n"; system($cmd); #~ Start a background process and pass the data type to it to mine the downloaded data my $command = "nohup /usr/local/bin/perl /home/thor/Programs/testing/has-miner-0.1.pl $HAS_break[5] $format $data_type"; my $proc = Proc::Background->new($command); print "Miner Started: ",$proc->pid,"\n" if defined $proc; } }else{ print "\nThere are No Messages in the folder: $mailspool\n"; } $db->disconnect(); exit;