#!/usr/bin/perl
use LWP::UserAgent;
use HTML::LinkExtor;
use URI::URL;
use Image::Magick;

@timeData = localtime(time);
open ($log, ">>", "/home/superkuh/tests/oncedailywtf.txt") or die "Can't open oncedailywtf.txt to write.\n$!\n";
print $log join(' ', @timeData);
print $log "\n";

my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
$ua->agent("Opera/9.80 (X11\; Linux x86_64\; U\; en) Presto/2.9.168 Version/11.50");
my @ns_headers = (
   'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, 
        image/pjpeg, image/png, */*',
   'Accept-Charset' => 'iso-8859-1,*,utf-8',
   'Accept-Language' => 'en-US',
);

my $spacedirpath = 'home/superkuh/www/spaceweather';
my $useragent = "--user-agent=\"Opera/9.80 (X11\; Linux x86_64\; U\; en) Presto/2.9.168 Version/11.50\"";

`wget $useragent "http://iswa.gsfc.nasa.gov:8080/IswaSystemWebApp/iSWACygnetStreamer?timestamp=2038-01-23%2000:44:00&window=-1&cygnetId=40" --output-document=/$spacedirpath/magnetopause_standoff.gif`;
`wget $useragent "http://iswa.gsfc.nasa.gov:8080/IswaSystemWebApp/iSWACygnetStreamer?timestamp=2038-01-23+00%3A44%3A00&window=-1&cygnetId=261" --output-document=/$spacedirpath/heliosphere_density.gif`;
`wget $useragent "http://iswa.gsfc.nasa.gov:8080/IswaSystemWebApp/iSWACygnetStreamer?timestamp=2038-01-23+00%3A44%3A00&window=-1&cygnetId=267" --output-document=/$spacedirpath/heliosphere_velocity.gif`;
`wget $useragent "http://iswa.gsfc.nasa.gov:8080/IswaSystemWebApp/iSWACygnetStreamer?timestamp=2038-01-23+00%3A44%3A00&window=-1&cygnetId=271" --output-document=/$spacedirpath/heliosphere_dynpressure.gif`;
`wget $useragent "http://iswa.gsfc.nasa.gov:8080/IswaSystemWebApp/iSWACygnetStreamer?timestamp=2038-01-23%2000:44:00&window=-1&cygnetId=295" --output-document=/$spacedirpath/PFSS_magfootprints.gif`;
`wget $useragent "http://ips.ucsd.edu/fast/current/earth_remoteview_n_movie.gif" --output-document=/$spacedirpath/IPS_density_movie.gif`;
`wget $useragent "http://ips.ucsd.edu/fast/current/earth_remoteview_v_movie.gif" --output-document=/$spacedirpath/IPS_velocity_movie.gif`;



my $postfixed = windpostfix();
windmovie("http://www.lmsal.com/forecast/windmovie/",$postfixed,"$spacedirpath/wind1",'lmsal_heliosphere_wind.gif');
NoRH_Events("http://solar.nro.nao.ac.jp/norh/html/event/");


sub NoRH_Events {
	my ($url) = @_;
	my $response = $ua->get("$url", @ns_headers);
	if ($response->is_success) {
		my $html = $response->decoded_content;
		my @htmluh = split(/\n/, $html);
		print "$htmluh[21]\n";
		$htmluh[21] =~ /<a href=\"(.+)\">/;
		my $url2 = $1;
		#print "url2: $url2\n";
		if ($url2) { 
			# 20110803_0431/norh20110803_0431.html
			# (\d{8}_\d{4})\/norh.+\.html
			$url2 =~ /(\d{8}_\d{4})\/norh.+\.html/;
			my $baseurl = $1;
			#print "baseurl: $baseurl\n";
			$url2 = "http://solar.nro.nao.ac.jp/norh/html/event/" . $url2;
			#print "url2again: $url2\n";
			if ($baseurl) { 
				my @images;
				sub callback {
					my($tag, %attr) = @_;
					return if $tag ne 'img';
					push(@images, values %attr);
				}
				$p = HTML::LinkExtor->new(\&callback);
				$res = $ua->request(HTTP::Request->new(GET => $url2),
                      				sub {$p->parse($_[0])});
				my $base = $res->base;
				@images = map { $_ = url($_, $base)->abs; } @images;
				#print join("\n", @images), "\n";
				#print "\# " . scalar(@images) . " lines in \@images\n";
				my $n = 0;
				foreach my $image (@images) {
					$ua->default_header('Referer' => "$url2");
 					my $response = $ua->get($image, @ns_headers);
					my $result = $response->status_line;
					my $content = $response->content();
						print ("$image - $result - $n\n");
						if ($result =~ /200/) {
							open (FILE, ">/$spacedirpath/NoRH_$n.png") or warn "Could not open output!: $!";
							binmode (FILE);
							print FILE $content;
							close FILE;
							$n++;
							print "Sleeping $delay\n";
							sleep $delay;	
						} else {
							$n++;
						}
					}	
			} else { 
				warn "Can't figure out baseurl from url2: $url2\n"; 
				print $log "Can't figure out baseurl from url2: $url2\n";
			}
		} else { 
			warn "no regex match for a url at line 21 of the html.\n"; 
			print $log "no regex match for a url at line 21 of the html.\n";
		}
	} else { 
		warn "The $url doesn't seem to exist?\n"; 
		print $log "The $url doesn't seem to exist?\n";
	}
}


sub windmovie {
	my ($url,$postfix,$tempdir,$outname) = @_;
	my @windimages;

	print "starting windmovie for $postfix as $outname...\n";

	my $response = $ua->get("$url", @ns_headers);
	if ($response->is_success) {
		my $html = $response->decoded_content;
		my @htmluh = split(/\n/, $html);
		foreach my $line (@htmluh) {
			# <a href="d2f000_20110811_062734.gif">
			# <a href=\"(.+_062734\.gif)\">
			if ($line =~ /<a href=\"(.+$postfix)\">/) {
				my $image = $1;			
				push(@windimages, "$image");
				#print scalar(@windimages) . ": $url$image\n";	
			}
		}

		if (@windimages) {
			foreach my $image (@windimages) {
				$ua->default_header('Referer' => 'http://www.lmsal.com/forecast/windmovie/windmovie.html');
 				my $response = $ua->get("$url$image", @ns_headers);
				my $result = $response->status_line;
				my $content = $response->content();
				print ("$url$image - $result\n");
				if ($result =~ /200/) {
					open (FILE, ">/$tempdir/$image") or warn "Could not open output!: $!";
					binmode (FILE);
					print FILE $content;
					close FILE;
				} else { warn "unable to grab file \$url\$image: $url$image\n"; }
			}

			makeGIF($outname,$tempdir,@windimages);

		} else { warn "nothing in \@windimages"; print $log "nothing in \@windimages\n"; }
		


	} else { warn "can't get the url: $url to load\n"; print $log "can't get the url: $url to load\n"; }	
}

sub windpostfix {
	my $postfix;
	my $url = 'http://www.lmsal.com/forecast/windmovie/windmovie.html';
	$ua->default_header('Referer' => 'http://www.lmsal.com/forecast/');
	my $response = $ua->get("$url", @ns_headers);
	if ($response->is_success) {
		my $html = $response->decoded_content;
		# urls[0]=url_path+"/d2f000_20110811_062734.gif";
		# urls\[0\]=url_path\+\"\/.+_\d+(_\d+\.gif)\"\;
		if ($html =~ /urls\[0\]=url_path\+\"\/.+_\d+(_\d+\.gif)\"\;/) {
			$postfix = $1;
			$postfix =~ s/\./\\\./;
			print "match: $postfix\n";		
		} else { 
			warn "no match, unable to get the postfix";
		}

	} else { 
		$postfix = "_062734\.gif"; 
		warn "couldn't get the page to parse, \$url:$url\n"; 
	}
	return $postfix;
}

sub makeGIF {
	my ($outname, $tempdir, @images) = @_;
	print "Making animated GIF\nout: /$spacedirpath/$outname, tempdir: $tempdir, images: " . scalar(@images) . "\n"; 
	my $hundredths_of_second = 10;
	my ($anigif);
	$anigif = new Image::Magick;
	foreach my $image (@images) {
		#print "/$tempdir/$image\n";
		$anigif->Read("/$tempdir/$image");
	}
	$anigif->Write(delay => $hundredths_of_second, loop => 0, filename => "/$spacedirpath/$outname");
}

