00001 #!/usr/bin/perl -w
00002 #
00003 # Animated satellite map grabber for Environment Canada.
00004 #
00005 # This script downloads satellite map data from the Environment Canada
00006 # website. It uses the lists of JPEG images supplied by the page at
00007 # http://www.weatheroffice.gc.ca/satellite/index_e.html.
00008 #
00009 # The bulk of the code in this script was originally authored by
00010 # Lucien Dunning (ldunning@gmail.com).
00011
00012 use strict;
00013 use Getopt::Std;
00014 use LWP::Simple;
00015 use Date::Manip;
00016 use ENVCANMapSearch;
00017 our ($opt_v, $opt_t, $opt_T, $opt_l, $opt_u, $opt_d);
00018
00019 my $name = 'ENVCAN-Animated-Map';
00020 my $version = 0.3;
00021 my $author = 'Joe Ripley';
00022 my $email = 'vitaminjoe@gmail.com';
00023 my $updateTimeout = 10*60;
00024 my $retrieveTimeout = 30;
00025 my @types = ('amdesc', 'updatetime', 'animatedimage');
00026 my $dir = "./";
00027
00028 getopts('Tvtlu:d:');
00029
00030 if (defined $opt_v) {
00031 print "$name,$version,$author,$email\n";
00032 exit 0;
00033 }
00034
00035 if (defined $opt_T) {
00036 print "$updateTimeout,$retrieveTimeout\n";
00037 exit 0;
00038 }
00039 if (defined $opt_l) {
00040 my $search = shift;
00041 ENVCANMapSearch::AddSatSearch($search);
00042 ENVCANMapSearch::AddSatClassSearch($search);
00043 ENVCANMapSearch::AddImageTypeSearch($search);
00044 foreach my $result (@{ENVCANMapSearch::doSearch()}) {
00045 print "$result->{entry_id}::($result->{satellite_class}) $result->{satellite} $result->{image_type}\n";
00046 }
00047 exit 0;
00048 }
00049
00050 if (defined $opt_t) {
00051 foreach (@types) {print; print "\n";}
00052 exit 0;
00053 }
00054
00055 if (defined $opt_d) {
00056 $dir = $opt_d;
00057 }
00058
00059 my $loc = shift;
00060
00061 if (!defined $loc || $loc eq "") {
00062 die "Invalid usage";
00063 }
00064
00065 # Get map info
00066 ENVCANMapSearch::AddAniSearch($loc);
00067 my $results = ENVCANMapSearch::doSearch();
00068 my $desc = $results->[0]->{satellite};
00069
00070 # Get HTML and find image list
00071 my $response = get $results->[0]->{animated_url};
00072 die unless defined $response;
00073
00074 my @image_list;
00075 my $size;
00076 my $base_url = "http://www.weatheroffice.gc.ca";
00077 my $file = $loc;
00078 my $path = "$dir/$file-";
00079
00080 # Get list of images (at most 10)
00081 foreach my $line (split(/\n/, $response)) {
00082 if ($line =~ /theImagesComplete\[\d*\] \= \"(.*)\"\;/) {
00083 push (@image_list, $1);
00084 if ($#image_list >= 10) { shift @image_list; }
00085 }
00086 }
00087
00088 # Download map files, if necessary (maps are stale after 15 minutes)
00089 my $i = 0;
00090 foreach my $image (@image_list) {
00091 my $getImage = 1;
00092 if (-f $path . $i) {
00093 my @stats = stat(_);
00094 if ($stats[9] > (time - 900)) { $i++; next; }
00095 }
00096
00097 getstore($base_url . $image, $path . $i);
00098 $i++;
00099 }
00100
00101 # Determine image size
00102 if (!$size) {
00103 use Image::Size;
00104 my ($x, $y) = imgsize("${path}0");
00105 $size = "${x}x$y" if ($x && $y);
00106 }
00107
00108 print "amdesc::$desc\n";
00109 printf "animatedimage::${path}%%1-$i%s\n", ($size && "-$size" || '');
00110 print "updatetime::Last Updated on " . UnixDate("now", "%b %d, %I:%M %p %Z") . "\n";